Canvas keyUp

How to detect a keys combination on a Canvas keyUp event?

The code detects either “R” or OptionKey but no way of detect both.
if Keyboard.OptionKey then delta is -60 but then key is not R and, vice versa if R is checked first

Var delta As Integer
delta = 60
If Keyboard.OptionKey Then
  delta = - 60
End If

If key = "R" Then
  RotateDegrees = RotateDegrees + delta
  If RotateDegrees > 300 Or RotateDegrees < 0 Then
    RotateDegrees = 0
  End If
End If

DegreeLabel.Text = RotateDegrees.ToString
Refresh

see my example

canvas-cmdandR.xojo_binary_project.zip (6.7 KB)

@RudolfJ
i have only luch with shift key at windows 11.
somehow keys are in use by os or nvidia.

Function KeyDown(key As String) Handles KeyDown as Boolean
  Var delta As Integer = TextField1.Text.Val
  Var degree As Double = 3.14 / 180
  If key = "R" Then
    If Keyboard.AsyncShiftKey Then delta = -delta
    Var w As Integer =(s.Rotation/degree)+delta
    If w >360 Then w = w -360
    s.Rotation = w * degree
    canvas1.refresh
  End If
  
End Function

Thanks Markus

The anomalies are:

The code on KeyUP event does not work but the same code on KeyDown it works.
Using the Keyboard.AsyncOptionKey does not work
Using the Keyboard.AsyncCommandKey (or Keyboard.CommandKey) does work

So, command key plus R on keyDown event works. (should be a reason for…)

i am a fan of screen icons or context menu instead of key combinations.
or i would use r for rotate modus and then left/right cursor intuitively,
same for m = move and then cursor keys.

1 Like

If you have the MBS Plugins, the NSEventMonitorMBS class can probably help you.

Your keyboard certainly doesn’t produce a “R” when you press option-r. For instance, mine produces a ® symbol. Therefore, it’s logical to expect key to not be “r”, but rather the actual symbol (like any other press would make).

One trick that works for “R” (but wouldn’t for some other keys) is to check the physical key. It turns out that “R” is at the same location for all layouts (which wouldn’t be the case for “y”, for instance), so you can use “if keyboard.AsyncKeyDown(15) then” to check for the “r” key.

Other advices already given would work too, like adding an events monitor, but keyboard.AsyncKeyDown is a built-in solution.

thanks Arnaud, I definitely will use command key and keyDown event

If key = "R" Then
  If Keyboard.CommandKey Then delta = - 60

You could record the state of the optionkey and the key pressed in the DOWN event, and use them when the UP event happens.

1 Like

Fine. In this case, just make sure you won’t have a menu item with Cmd-R as the shortcut, even in the future.

Definitively looks weird to me. It shouldn’t.