Panda3D Manual: Keyboard Support
  <<prev top next>>     

Panda3D has keyboard support built in. Keyboard presses send Events. Each keyboard key will send an event when it is first pressed down, when it is released, and one repeatedly while its pressed.

The events can be accepted with the following code:

self.accept( <event name> , <Function> )
self.accept( <event name> , <Function> , <parameters list> )


<event name> is a string that labels the event.
<Function> is a python function to be called when the event is sent.
<parameters list> is a python list of parameters to use to call <Function>.


The <event name> that a key sends is fairly predictable base on these rules:

1. Keys that type a character are named that character. It is always lowercase even with shift or caps lock (Shift and other modifiers are explained below.)

e.g.

"a", "b", "c", "[", and "]"

not

"A", "B", "C", "{", and "}"

2. The key down event is named for the key.

3. As of 1.3.0 The keyboard autorepeat is named for the key + "-repeat" e.g.

"a-repeat", "b-repeat", "[-repeat"

4. The key up event is named for the key + "-up" e.g.

"a-up", "b-up", "[-up"

5. All key events (including "-up") have a corresponding time event labeled

"time-" + <key name>

that send a time argument corresponding to the time that event was fired

6. Keys that don't type a character are labeled as follows:

"escape", "f"+"1-12" (e.g. "f1","f2",..."f12"), "print_screen-up" (no down.) "scroll_lock"

"backspace", "insert", "home", "page_up", "num_lock"
"tab", "delete", "end", "page_down"
"caps_lock", "enter", "arrow_left", "arrow_up", "arrow_down", "arrow_right"
"shift", "lshift", "rshift",
"conrol", "alt", "lcontrol", "window-event"(no up?), "lalt", "space", "ralt", "rcontrol"

7. Some physical keys are distinguishable from the events that they fire, and some are not. The modifier keys distinguish between left and right, but send a neutral event as well. (e.g. the left shift key sends both "lshift" and "shift" events when pressed) Save for "num_lock", "*", and "+" the numpad keys are indistinguishable from the main keyboard counterparts. (e.g. when Num Lock is on the both the numpad and keyboard 1 keys send "1")

8. Keys pressed in combination with modifiers send an additional event. The name is the modifier appended before the key and separated with a dash in the order shift conrol alt e.g.:

"shift-a" "shift-control-alt-a" "shift-alt-a"

These compound events don't send a "time-" event. If you need one, use the "time-" event sent by one of the keys in the combination.

9. You can see these results for yourself using messenger.toggleVerbose()

Here are some examples in code:

self.accept('k' , self.__spam )#calls the function __spam() on the k key event.
self.accept('k-up', self.__spam, [eggs, sausage, bacon,] )#calls __spam(eggs,sausage,bacon)
self.accept('escape' , sys.exit )#exit on esc
self.accept('arrow_up' , self.spamAndEggs )#call spamAndEggs when up is pressed
self.accept('arrow_up-repeat, self.spamAndEggs )#and at autorepeat if held
self.accept('arrow_up-up' self.spamAndEggs )#calls when the up arrow key is released

  <<prev top next>>