Making Modifier Key Events
April 11, 2003
by Alex Zavatone
Director is generally a very flexible environment. However, the handling of keyboard input leaves something to be desired. As Director currently exists keypresses on most keys generate keydown and keyup events. This is quite useful when the keys pressed are those that generate events but quite the PITA when the keys pressed do not. This ugly fact of like shows itself many times in cases that require key input. My particular need was to have a modifier key trigger a chain of events for the purpose of displaying command key graphics. Since no modifier keys (shift, command, control, alt and option) generate events when pressed, this quickly becomes something far from a piece of cake.
An entertaining thought would be to create an Xtra that would generate lingo events when these keys are pressed. My lack of knowledge of C programming proved to be a significant impediment in that regard. The concept here is to implement a solution at the lowest level possible. With the potential for an Xtra out of the picture, another approach needed to be developed. Director broadcasts events and intercepts them when a handler exists with the same name as the event. Everything starts and runs in Director because of these events and event interceptors. If Director could check for the desired keys at a desired interval, it could then make and broadcast its own brand new event and that event could be intercepted by a movie script.
An example of this concept could be accomplished with an idle script or a timeout as seen below
Movie script:
on Idle
if the controlDown then
ControlDown
end if
if the shiftDown then
ShiftDown
end if
end
on ControlDown
put "ControlDown"
-- do control stuff
end
on ShiftDown
put "ShiftDown"
-- do shift stuff
end
Think of this simple example not as calling a handler when the shift or control keys are pressed but instead broadcasting a movie level event that is intercepted by the movie level handler of the same name. By implementing the key checking at the lowest level possible, either at an idle or timeout interval, the overhead of this constant checking for key states can be kept as small as possible. Though encouraged not to use idle by some of the Director engineers years ago, I still find it useful as idle is supposed to be called only when there is time to do so. Whatever method we choose to perform the check for the keys, the problem here is that we need to send an event on a keypress and release, not all the time the key is held down. A way of solving this is to store the state of the key being pressed or released. To do this on a movie level requires either messy use of globals or another trick originally showed to my be Chris Leske over a year ago, that of movie script properties.
By declaring a property within a movie script, it can be referenced within the script it is defined in by explicitly referring to the script and the property. For example, if movie script 1 has a property called pControlDown, you would access it by reference to the script.property as seen below:
script (1).pControlDown = 0
Now, if a property is set up for each key we wish to store the state of when a key is pressed, we can check and see its current state value and determine if it is already down or up. This way, Director can then only broadcast an up or down event if the key was not in the same state previously.
With all that in mind, putting together a movie script that can broadcast modifier key events proves to be not too difficult. I've included a functional script that works perfectly on the Mac but may need some customization for Windows. It's meant to be in script 1 so if you plan on putting it elsewhere, you should replace the script reference with the number or name of the movie script all the code is in. I'll leave those changes as an exercise for the reader. Enjoy your new key events.
This script is available for download as a Director 8.5 movie in ZIP or SIT formats.
Movie Script: 1, Modifier Key Events
property pCommandDown
property pOptionDown
property pControlDown
property pShiftDown
on idle
if the commandDown then
CommandDown
else
CommandNotDown
end if
if the optionDown then
OptionDown
else
OptionNotDown
end if
if the controlDown then
ControlDown
else
ControlNotDown
end if
if the shiftDown then
ShiftDown
else
ShiftNotDown
end if
end
on ControlDown
if script(1).pControlDown = 0 then
script(1).pControlDown = 1
-- here you can do stuff
put "ControlDown"
end if
end
on ControlNotDown
if script(1).pControlDown = 1 then
script(1).pControlDown = 0
-- here you can do stuff
put "ControlUp"
end if
end
on CommandDown
if script(1).pCommandDown = 0 then
script(1).pCommandDown = 1
-- here you can do stuff
put "CommandDown"
end if
end
on CommandNotDown
if script(1).pCommandDown = 1 then
script(1).pCommandDown = 0
-- here you can do stuff
put "CommandUp"
end if
end
on OptionDown
if script(1).pOptionDown = 0 then
script(1).pOptionDown = 1
-- here you can do stuff
put "OptionDown"
end if
end
on OptionNotDown
if script(1).pOptionDown = 1 then
script(1).pOptionDown = 0
-- here you can do stuff
put "OptionUp"
end if
end
on ShiftDown
if script(1).pShiftDown = 0 then
script(1).pShiftDown = 1
-- here you can do stuff
put "ShiftDown"
end if
end
on ShiftNotDown
if script(1).pShiftDown = 1 then
script(1).pShiftDown = 0
-- here you can do stuff
put "ShiftUp"
end if
end
All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/
Copyright 1997-2024, Director Online. Article content copyright by respective authors.