To use keyboard presses within your world you need to register a keyboard callback that will monitor for key presses. There are two main ways to register callbacks. First, you can register a general callback that is triggered by any keyboard events. Second, you can register callbacks for individual keys on the fly. In addition to registering callbacks, you can also spontaneously query whether a key or a set of keys is currently being pressed.
Note: when you're referring to a specific key in the keyboard-related code, use the string associated with that key. For example, for the p key use 'p'. For any special keys that don't have a string associated with them (e.g. the arrow keys), use one of the pop-down flags that begin with viz.KEY_. So, for the down arrow, use viz.KEY_DOWN.
To register a callback for all keys, you can use viz.KEYDOWN_EVENT or viz.KEYUP_EVENT callbacks. Keydown events occur when you press a key and keyup events occur when you release a key. Note that when an event occurs, the identity of the key is returned as an input into the given function (in the example code below 'key' is the key that's been pressed).
#This function will be called when a
given 'key' is pressed.
def onKeyDown(key):
print
key, ' is down.'
if
key == viz.KEY_TAB:
print 'you hit the tab key'
#Register the callback which to call the 'onKeyDown' function.
viz.callback(viz.KEYDOWN_EVENT,onKeyDown)
You can also register one of these callbacks within a class.
#Create an event class.
class Whatever(viz.EventClass):
def
__init__(self):
viz.EventClass.__init__(self)
#Add a
key callback for this class.
self.callback(viz.KEYUP_EVENT,
self.onKeyUp)
def
onKeyUp( self, key ):
print key, ' is up.'
The Vizact actions library has several commands to add keyboard callbacks for individual keys (vizact.onkeydown, vizact.whilekeydown, vizact.onkeyup). For each of these commands, provide the key id and then the command it should call and then any necessary values for the command. (Note: make sure you don't call the command by placing parentheses after it.) To remove a keyboard event use vizact.removeEvent(<event handle>). Using actions to register keyboard callbacks is particularly useful for adding and removing individual keyboard events on the fly.
#Register events that call the viz.clearcolor
command.
redEvent = vizact.onkeydown( 'r', viz.clearcolor, viz.RED )
greenEvent = vizact.onkeydown( 'g', viz.clearcolor, viz.GREEN )
#Remove one of the above events.
vizact.onkeydown( viz.KEY_DELETE, vizact.removeEvent, redEvent
)
Keyboard presses can also be used as a one-shot deal in action sequences such that the sequence waits until the key is pressed to proceed:
ball = viz.add( 'white_ball.wrl' )
#Wait until the spacebar is pressed to continue the queue
of actions.
ball.addAction( vizact.waitkey( '
' ) )
#Add another action.
ball.addAction( vizact.fadeTo( viz.RED, time=3 ) )
To find out if a given key is currently being pressed, use viz.key.isDown( <key> ). To find out if any member of a set of keys is being pressed, use viz.key.anyDown( <set of keys> ). To find out if all members of a set of keys are pressed, use viz.key.allDown( <set of keys> ).
Check out the section on the camera handlers to learn how to define your own keyboard-based navigation.
Camera handlers - Describes how to define viewpoint navigation with whatever input you choose.
Actions- Introduction to the vizact library of actions.