Responding to Keyboard Events
<< previous | Chapter 9. Graphical User Interface | next >>

## Responding to Keyboard Events

Catching keyboard events lets a simulation execute certain actions in response to user keypresses. To catch keyboard events, simply implement methods in your simulation with names like `catch-key-`X`-down` and `catch-key-`X`-up`, where `X` is any letter or number, uppercase or lowercase.

In the event that the key you want to catch is not a letter or number, then the method names described above may not work, due to conflicts with the steve language. If you tried to catch the space bar, for example, using a method named "`catch-key- -up`", breve would get confused—the same is true of most punctuation as well. In such cases, you can still catch the event using the hexadecimal ASCII equivalent for the character. In the case of the space bar, the valid breve method name would be `catch-key-0x20-down`. Consult an ASCII table for more information. Don't forget to add `0x` in front of hexadecimal values!

In addition to catching regular ASCII characters, similar methods may be used to catch the arrow keys:

    catch-key-left-down
    catch-key-left-up
    catch-key-right-down
    catch-key-right-up
    catch-key-up-down
    catch-key-up-up
    catch-key-down-down
    catch-key-down-up

When a key is pressed down, only one call to the "down" method is made (as opposed to repeated calls until the button is released). And likewise, only a single call to the "up" method is made. If you wish for an action to continue for as long as a key is pressed down, then you should have the "down" callback set a flag or call a method which will trigger a continuous event, then use the "up" callback to deactivate the event.

<< previous | Up | next >>
Chapter 9. Graphical User Interface | Table of Contents | Displaying a Dialog Box