KeyDown event - distinguish special keys from typed characters

Is there any way to reliably distinguish typed characters from special keys in KeyDown events?

For example, when I press F5 I get the character ‘Ì’ (capital i accent grave, &hCC in Windows-1252 encoding).
I get exactly the same Key-parameter if I type the character (using the accent-grave dead-key followed by ‘I’)

Maybe, the KeyDown event is not even the correct one to handle, what I do. What I want to implement is a quick search feature where you type into a sorted listbox and the listbox gets positioned to the first item matching your text. the search string gets reset after a timeout. It shall work as the same feature does in e.g. Windows Explorer.

I would strongly prefer a cross-platform Xojo solution. If none such exists, how do people deal with this?

I do not consider Keyboard.AsyncKeyDown an acceptable solution because

  • it is asynchronous to the event I am processing
  • It works with scan codes and thus checks physical keys on the keyboard, which is fundamentally different from what needs to get processed in the KeyDown event, which may be the result of a single keypress followed by a key release, or followed by same tome (autorepeat) or may be the result of several keys presses simultanously (like SHIFT + a giving A) or a succession of the aforementioned as in my example case above, generating Ì
  • it works with scan codes which are hardware dependent. The keyboard driver is supposed to deal with those.
  • it does not tell me what is (or actually was!) pressed. I needed to scan many physical keys one by one

In my case I need to consume all characters (that’s basically potentially the entire Unicode range) and need to pass on anything else except backspace and ESC.

The solution needs to work on Mac, Windows and Linux.

Can you please point me in the right direction

In spite of what you say, the only way to reliably detect function keys and special keys is to use the Keyboard class.

You can poll the Keyboard class from the Keydown event.

Then the only built-in Xojo solution is discarded.

I’d tell you to use a mix of both (use the KeyDown event and, inside it, check Keyboard.AsyncKeyDown for special keys (see below)).

Check for it as the first step of your KeyDown event. As computers are so fast, the key will be detected anyway. Store the result in a variable that you can later check, if your KeyDown takes “some times” (which it shouldn’t anyway).

That’s why I suggest you to use both the KeyDown event (for regular keys) and AsyncKeyDown for special keys. Function keys (among others) don’t produce a meaningful character. Last time I checked, all function keys on Macs produced a character whose ascii is 16, so you can’t differentiate them using only a KeyDown event.

Only characters are hardware-dependent, based on the used locale. Function keys, delete, backspace, arrow keys, esc (…) all will always have the same physical key code. So that’s reliable.

Make a loop from 0 to 127 and check which keys are pressed. This loop is really fast and won’t miss keys. Then, once you’ve stored which keys were pressed, handle them as you want.

Just give it a try; it should convince you.

The only other way you could do what you want would be to capture the event chain, but that requires to escape from the Xojo-only code (which doesn’t provide access to the event itself). The MBS plugin would be a way.