Trapping the Option or Command keys

Sarah Reichelt sarah.reichelt at gmail.com
Mon Jul 27 18:08:33 EDT 2009


> I'm re-writing a hypercard stack in which buttons performed different
> functions if the OptionKey or CommandKey were down. I need the title and
> icon to change on all the buttons when either modifier key is down. Looking
> back in the archives it seems like it was a problem only on Macs. Is there a
> command now that will let me do this? The simplified interface I'm trying to
> re-create will allow a button that is bordering a text field to either read
> the contents of bordering text field with text-to-speech, record the
> student's voice or play back the recorded voice.

As other's have noted, on a Mac, there is no way to detect the
modifier keys being pushed until another key is pushed too.
When I have needed to do this, I set up a polling loop that checks the
state of the modifiers regularly and alters the buttons accordingly
e.g.

command checkModKeys
   if the commandkey is down then
      set the label of btn "Action" to "Play"
   else if the optionkey is down then
      set the label of btn "Action" to "Record"
   else
      set the label of btn "Action" to "Speak"
   end if

   if the pendingmessages contains "checkModkey" is false then
      send "checkModKeys" to me in 20 ticks
   end if
end checkModKeys

All you have to remember then is to send the first checkModKeys when
the card/stack opens and to stop the pending messages when it closes.

I have found that this works well without placing much load on the processing.

Cheers,
Sarah



More information about the use-livecode mailing list