What is AsyncKeyDown used for?

I can find no documentation about when or where to use AsyncKeyDown
Can someone explain it?

As part of the explanation, please tell me how to know it’s not the key in Keydown event.

You just call it, if the key you were interested in happened to be down at the time of the call, you get a True back.

Most will just put it in a quick firing timer and check it in there, whether that’s on a window or application wide depends on what feature you need.

http://documentation.xojo.com/api/hardware/keyboard.html#keyboard-asynckeydown

You can call it in the KeyDown event (if you want to) to test for non-printable keys. For example, to intercept keys like PageUp or PageDown if you don’t what them to do anything. Something like…

If Keyboard.AsyncKeyDown (&h74) Then ' the PageUp key
  Return True
End If

// do stuff with the Key value

@Dale_Arends

No. You have to do “your stuff” BEFORE returning true:

If Keyboard.AsyncKeyDown (&h74) Then ' the PageUp key
  // do stuff with the Key value, eg
  Speak “OW! Please don’t poke me with this key!”
  Return True
End If
1 Like

Quickly said, take this code:

    dim b1,b2 as boolean

    //a long operation here (loop, sleep, slow command, etc.), say it takes 5 seconds or more.
  ` //Start holding the control key while the app is busy in the step above (do not start earlier or later).`

    b1=Keyboard.ControlKey
    b2=Keyboard.AsyncControlKey

If you have started to press control at the long operation line and held it, b1=false and b2=true.

Async is meant for the state “right now” in code, while reports the status when event was invoked by the OS.

3 Likes

I’m not an expert, but async is usually used when you hold a modifier key down and then press another, eg you hold the option key and then press s.

To deal with that you have in the keyboard module async options for the modifier keys, eg AsyncCommandKey, AsyncShiftKey, etc

From the LR:

The Keyboard module is used to determine if particular keys are being pressed. The async version of each keyboard property tells you the immediate state of the key. If you want to know the state of the key when an event was queued, you should use the non-async version of the properties. These properties are updated constantly during code execution.

Try this in a TextField with a second TextArea called TAlog:

Function KeyDown(Key As String) Handles KeyDown as Boolean
  
  If Keyboard.AsyncOptionKey Then
    // handle the keyboard event here....
    
    TAlog.AppendText "OptionKey down" + EndOfLine
    
    If Keyboard.AsynckeyDown(&h00) Then  // key "a" has been pressed
      // do something with this key here
      TAlog.AppendText "and a Key down too" + EndOfLine
      
    End If
    
  End If
 
End Function

Thank you both. I get now when it is useful.

But do not use the Async versions in a KeyDown event. Use Keyboard.OptionKey instead of Keyboard.AsyncOptionKey, etc.

1 Like

There is never a need for AsyncKeyDown inside a KeyDown event. You use AsyncKeyDown inside a timer polling for key presses where there is no KeyDown event available to you.

Markus Winter

22h

@Dale_Arends

No. You have to do “your stuff” BEFORE returning true:

If Keyboard.AsyncKeyDown (&h74) Then ' the PageUp key
  // do stuff with the Key value, eg
  Speak “OW! Please don’t poke me with this key!”
  Return True
End If

Markus, you did not read what I wrote above the code snippet. The code was to explicitly IGNORE a PageUp keypress, not utilize it. Anything else gets passed on down to other code (not supplied).

Nevertheless, it was meant as an illustration, not recommended code. As Tim pointed out, the KeyDown event is an unlikely place for the AsyncKeyDown to be used.

Another use case that at times I have found useful is to check the current state of keys, particularly modifier keys like a shift key, while processing an event that is NOT a KeyXxxx event. For example, from inside a MouseEnter event to check if some key (say the shift key) is held down and if so immediately generate and display a Tooltip like modal letting the user hold down shift and move the mouse around a listbox / grid to very quickly display additional information about various cells.

1 Like

My apologies :pray:

Unless you have a loop in your KeyDown event and you want to know the new state after the loop… But that’s unlikely and would be bad design.

The async versions of the keyboard “readers” are “the real thing”, they read the keys in real time, at that moment, the non-async counterparts are for keyboard events, they have theirs values “frozen” with the values found when the event occurred. So, if you where super fast, pressing Control and W, and releasing Control and W after (in such order, but very fast), you COULD read W without the Control pressed if using the async “read it now”, but reading the non-async you will read the captured state by the event, the “Control pressed” will be there, even if you released it 10 milliseconds before, and currently it’s really not there if you use the async “reader”.

1 Like

Thanks Rick. Very different use.

1 Like