Keyboard.AsyncKeyDown acting weird

Hi,

One of my apps allows customising keyboard shortcuts; I started to see weird behaviours, where most of the pressed keys would produce a “h” (the app triggers the Keyboard.AsyncKeyDown function to find pressed keys).

So I made a small test in a blank project, with a timer:

Var pk() As String

for i as Integer=0 to 127
  if Keyboard.AsyncKeyDown(i) then pk.Add i.ToString
next

self.Title=String.FromArray(pk)

Running this, I’m finding that the key 4 (which is indeed “h”) is always reported as pressed, as long as the app is in front. Additional keys I may press are reported correctly, but “4” always remains on even with no key pressed at all.

When the app isn’t frontmost, all is fine (only the actually pressed keys are reported). Also, the keyboard viewer doesn’t show the key as pressed. It looks to me it’s a Xojo issue.

Tested this on Mac OS Monterey.
This is rather puzzling…
Hoping someone could test the code above, please.

Is there any reason why you cannot use Keydown/Keyup instead ?

Yes. With modifier keys, I can’t retrieve the original key. For example, on my keyboard, option+o makes ø. If I get “ø” in the KeyDown event, I must resolve this to Option+o. The “option” is obvious (Keyboard.OptionKey) but for “o”, I must scan the keyboard.

You could resolve that with a table.

You can also check modifier keys in keydown/keyup. So you have the character, and the modifier.

That’s not going to work for characters like ñ

He could build a translation table, which would replace the asynckeydown.

I’d have to make tables for each key of each layout; there must be a simpler method.