How to use "arrow" keys on the keyboard as an interactive element.
 
 
 

Although we can click on a sprite (and code the result with a "on mouseUp me" handler), we cannot key on a sprite. "Keying" on a sprite (unlike mouse-clicking on a sprite) really doesn't make sense. The sprite is "deaf" to the user tapping on keys. This means that you cannot put scripting for keystrokes in a script attached to a sprite.

However, the Director movie itself IS hearing those keystrokes.

Therefore, the way that you control the result is by testing for keystrokes in either one of 2 places:

  1. You can make a "movie" script (remember "behavior" scripts attach to sprites and "movie" scripts are just scripts that appear in a cast member window, affecting the whole movie rather than just one sprite).
  2. You can put lingo into the frame script (that script in the frame channel that "pauses" the movie with the command "go to the frame").

No matter if you make a "movie" script or use the frame script, you write the Lingo code in the same way. Here I am going to assume that you are putting it in your frame script:


If you want VERY responsive arrow keys put this in the same script window as your frame script. (Overwrite the script that is already there with this new script). When you run the movie hold down the arrow key (don't just tap on it).

on exitFrame me
 if the keycode = 125 then
   --right arrow key
  sprite(1).locV = sprite(1).locV + 4
 else
  if the keycode = 126 then
   -- left arrow key
   sprite(1).locV = sprite(1).locV - 4
  else
    if the keycode = 124 then
   --down arrow key
    sprite(1).locH = sprite(1).locH + 4
   else
    if the keycode = 123 then
   --up arrow key
    sprite(1).locH = sprite(1).locH - 4
    end if
   end if
  end if
 end if
go to the frame
end

Remember that the higher the number that you add or subtract from the location value (locV, locH), the faster things go.


If you want a LESS responsive arrow key try this next version. Again, when you run the movie hold down the arrow key (don't just tap on it).:

on exitFrame me
 go to the frame
end

on keydown me
 if the keycode = 125 then
   --right arrow key
  sprite(1).locV = sprite(1).locV + 4
 else
  if the keycode = 126 then
   -- left arrow key
  sprite(1).locV = sprite(1).locV - 4
  else
   if the keycode = 124 then
    --down arrow key
   sprite(1).locH = sprite(1).locH + 4
   else
    if the keycode = 123 then
   --up arrow key
    sprite(1).locH = sprite(1).locH - 4
    end if
   end if
  end if
 end if
end

The keycode is used because that is the only way to identify arrow keys. You could use letter keys to make your sprite move. Then you could write something like:

if the key = "a" then
sprite(1).locV = sprite(1).locV + 4

... etc. etc.

The arrow keys are good because they literally point in directions that you want your sprites to move. But other keys could be substituted. The Lingo Dictionary, available in the Help menu, can give you more info as you need it.