Detecting optionKey et all in a textArea

A user has requested the ability to create a test with certain Mac modifier keys as the correct answer. For instance, the correct answer to question one might be Cmd.-Option-shift. I thought that, using the Keyboard.Async… commands this would be easy. So far I’ve drawn a blank. Holding down the modifier keys does not, of course, trigger a keydown event. Using Keyboard.AsyncOption for instance alters the letter (i.e. “a” ) being entered but does not trigger a conditional looking for that modifier key. Any ideas as to how to code this much appreciated.

If you’re a MonkeyBread plugins user, there is NSEventMonitorMBS (and an example in the Cocoa folder). The same could be achieved through declares. Doesn’t look like MacOSLib has the classes…

Look at Keyboard in the LR. Modifier keys are tracked by the boolean Keyboard.CommandKey, OptionKey and AsyncShiftKey which you can monitor in a timer and trigger action according to the need.

Here is a quick way of having a method in which you get both regular keys and modifiers as you would in Keydown.

  • Add a method called “myKeys”

Sub myKeys(key as string) End Sub

  • Add a 100 ms multiple timer to your page, with in Action :

[code]Sub Action()
static oldoption as boolean = keyboard.Optionkey
static oldcommand as boolean = keyboard.AsyncCommandKey
static oldshift as boolean = keyboard.ShiftKey

if oldoption = false and keyboard.Optionkey = true then
myKeys(“option”)
oldoption = true
elseIf keyboard.Optionkey = false then
oldoption = false
end if

if oldcommand = false and keyboard.AsyncCommandKey = true then
myKeys(“command”)
oldcommand = true
elseIf keyboard.AsyncCommandKey = false then
oldcommand = false
end if

if oldshift = false and keyboard.ShiftKey = true then
myKeys(“shift”)
oldshift = true
elseIf keyboard.ShiftKey = false then
oldshift = false
end if
End Sub
[/code]

  • In the TextArea Keydown event add :

Function KeyDown(Key As String) As Boolean mykeys(key) End Function

Now any press on Option, Command or Shift will appear in myKeys(key) as “option”, “command” or “shift”, and you will get the normal keys the way you do in the regular keydown event. So you will be able to effectively manage modifiers as regular keys, by checking the value of key.

This can be optimized but the principle is here.

Thank you Michel et al. I did arrive at a timer, not the keydown event, as the solution here.