Vizard 7 » Reference » Input Devices » Standard input devices » Keyboard basics
7.6

Keyboard basics

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.

Registering callbacks for all keys

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.')

Registering individual key callbacks

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 ) )

Finding out if a key is being pressed

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> ).

#Create a timer that reports on whether keys are being pressed.
def checkKeys():
    print('all ', viz.key.allDown(['1','2','3']))
    print('any ', viz.key.anyDown(['1','2','3']))
    print('one ', viz.key.isDown('1'))
vizact.ontimer(0.5, checkKeys)

Using keys to navigate through a world

Check out the section on the camera handlers to learn how to define your own keyboard-based navigation.

See also

In this section:

Mouse basics

Joystick basics

Input command table

Other sections:

Camera handlers - Describes how to define viewpoint navigation with whatever input you choose.

Actions- Introduction to the vizact library of actions.

Event Basics

Example scripts:

Keyboard