How to detect ALT key pressed

Is it possible to only catch if the ALT key is pressed in macOS?

When using the below code, it only detects when you hit ALT+another key
I need a trigger when only the ALT key is pressed.

If keyboard.AltKey then MsgBox "Option/Alt key" end if

No
It doesnt trigger a Keydown event by itself (nor does shift control or command)

Any solution for this? With declares? MBS?

It is possible in macOS, I did saw this some apps.

Use a timer and poll the AsyncAltKey

And how to poll the AsyncAltKey if it doesn’t trigger as Normal said?

It doesn’t “trigger” an event, but Keyboard.AsyncAltKey will change state. You have to check the value of it (“poll” it) periodically to see when it goes True. A Timer is perfect for that.

to “poll” something is to periodically check to see if it is or is not “true”… the timer set that “period” and its “action” event is what you would use to check the key

If keyboard.AsyncAltKey then
  // handle the keyboard event here....
  MsgBox "keyboard event detected..."
end if

An “event trigger” which as Norman said does NOT happen, would be if the pressing of the Alt key interrupted the normal flow, and provided an event of its own. In this case you have to create your own “event” (ie. using the Timer Action)

OK, that worked fine.
But to get a snappy response I need to put the timer at 5ms and this does load the CPU a bit.

I found something else that may be a better solution (read: does not use CPU load): NSEvent. MBS apparently has this.

Thanks for the suggestions !

[quote=333349:@Christoph De Vocht]I found something else that may be a better solution (read: does not use CPU load): NSEvent. MBS apparently has this.
![/quote]
not sure how that would help… since no event is generated… there would be no NSEvent

At a low level the OS knows whenever a “key” is pressed
Not every one of those is passed along to your Xojo app - presses of just modifier keys are not

So there may be an NSEvent for a press of just a modifier key but there might not be a corresponding KeyDown event in a Xojo app

I’m not sure about Windows & Linux behaviour in this regard

[quote=333349:@Christoph De Vocht]OK, that worked fine.
But to get a snappy response I need to put the timer at 5ms and this does load the CPU a bit.

I found something else that may be a better solution (read: does not use CPU load): NSEvent. MBS apparently has this.

Thanks for the suggestions ![/quote]

5 Ms is 1/200th a second. That is indeed snappy. It takes about 1/30th of a second to press and release a key very quickly… Such modifiers as Alt keys are very rarely hit as fast as, say, “A”, but users tend to hold longer, like they do on shift. You can probably get the same kind of result with 20 ms, which is around 1 tick.

Mac? Win?

Mac: NSEventMonitorMBS or CarbonApplicationEventsMBS classes

Win: WindowsKeyFilterMBS class