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> )
    self.acceptOnce( <event name> , <Function> )
    self.acceptOnce( <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: You can see these results for yourself using `messenger.toggleVerbose()` 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", "3", "[", etc.
not
     "A", "B", "#", "{"
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", "2-repeat", "[-repeat"
4. The key up event is named for the key + "-up" e.g.
     "a-up", "2-up", "[-up"
5. All key events (including "-up") have a corresponding time event labeled
    "time-" + <key name>
Here is an example of time reading in code:
    class ReadKeys(DirectObject.DirectObject):
      def __init__(self):
        self.accept('time-a-repeat', self.printRepeat)

      def printRepeat(self,a):
            print "repeat a",a
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" "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",
    "control", "alt", "lcontrol", "lalt", "space", "ralt", "rcontrol"
Note that "print_screen" is not available on Windows, (and some other OS'es) because it is used by the system. If you want to be able to catch this key, you need to find some way to prevent the system from catching the key first. (however, "print_screen-up" is still available in most cases) 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 control 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. The modifier compound events can be turned off to allow regular events to occur by calling:
    base.mouseWatcherNode.setModifierButtons(ModifierButtons())
    base.buttonThrowers[0].node().setModifierButtons(ModifierButtons())
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
Please, note then when the Panda window is minimized or Panda3D loses focus somehow else, "-up" event is sent for all keys. Read this forum thread to learn more: http://panda3d.net/phpbb2/viewtopic.php?t=4630
<<prev | top | next>> |