A. Using the Keyboard - Keyboard Handlers If you want to use other keys on the keyboard or allow commands in your game , you would use a keyboard handler. Do a "Dictionary" search on "key" and you will see many useful keyboard handlers to detect almost any key or combination of keys from the keyboard.
We will look at the most commonly used ones: 1. keyDown message handler Handles most of the letter keys on the keyboard. (It does not give the arrowkeys) on keyDown x // x contains the key e.g. "n" if you pressed the n, "d" if you pressed the d, etc end keyDown example on keyDown x if x is not a number then answer "numbers only please" end if end keyDown 2. keysDown() message function Handles a number of keys pressed (and cases when the user holds the key down too long). Since this is a function and not a message, it can be used inside another message handler. if keysdown() contains 119 then A good example would be using the keysdown() function to determine which key was pressed for a 2-player game. All the keys have numbers and every key on the keyboard can be identified. Player 1 will use the arrow keys which have the following number codes: left = 65361 up = 65362 right = 65363 down = 65364 Player 2 will use the keys: ASD and W which are on the left side of a keyboard: left (A) = 97 up (W) = 119 right (D) = 100 down (S) = 115 3. rawKeyDown message handler Handles more keys but gives the ASCII value instead of the face value on rawKeyDown x // x contains the key e.g. "110" if you pressed the n, "100" if you pressed the d, '65361' for the left arrow key, etc end rawKeyDown
B. Seeing what the handlers return for each key on the keyboard To see the number for a key, add this code to the card script: on updateScreen
put keysdown( ) into msg
send updateScreen to me in 20 millisec
end updateScreen
then add a button on the card: on mouseUp updateScreen end mouseUp
As you press every key on the keyboard, this will show the return value of each key as you press it, in the msgbox (This is one of the icons on the Edit Bar of LiveCode).
Take out that line of code when you have noted the numbers of all the keys that you need to handle.
|