|  | Home If you're new to Python and VPython: Introduction A VPython tutorial Pictures of 3D objects What's new Classic VPython web site VPython license Python web site |  | # Keyboard Interactions # As with mouse events, you can wait for various kinds of keyboard or mouse events: scene.waitfor('keydown') # wait for keyboard key press scene.waitfor('keyup') # wait for keyboard key release scene.waitfor('click keydown') # click or keyboard scene.waitfor('click') # wait for a click scene.waitfor('mousedown') # wait for mouse button press scene.waitfor('mouseup') # wait for mouse button release scene.waitfor('mousemove') # wait for mouse to be moved scene.waitfor('mousedown mousemove') # either event The event 'keydown' or 'keyup' occurs when you press or release a key on the keyboard (if you hold down a key, you may get multiple 'keydown' events due to repeats). You can obtain a package of information about the event that caused the end of the wait: box() ev = scene.waitfor('click keydown') if ev.event == 'click': print('You clicked at', ev.pos) else: print('You pressed key '+ev.key) Handling events with callbacks Here is a test routine using callbacks that lets you type text into a label. prose = label() # initially blank text def keyInput(evt): s = evt.key if len(s) == 1: prose.text += s # append new character elif s == 'delete' and len(prose.text) > 0: prose.text = prose.text[:-1] # erase letter scene.bind('keydown', keyInput) We define a "function" named "keyInput". Then we "bind" this function to 'keydown' events occurring in the canvas named "scene". Whenever VPython detects that a 'keydown' event has occurred, VPython calls the bound function, which in this case adds the input to the text of the label object. Note that evt.ctrl, evt.alt, and evt.shift are True if the corresponding key is down at the time of the event (evt.shift is also True if Caps Lock is in effect). This operation is called a "callback" because with scene.bind you register with VPython that you want to be called back any time there is a 'keydown' event. Here are the built-in events that you can specify in a bind operation: Keyboard: keydown, keyup Mouse: click, mousedown, mousemove, mouseup Other: redraw, draw_complete The event 'keydown' or 'keyup' occurs when you press or release a key on the keyboard. The events 'click', 'mousedown', 'mousemove', and 'mouseup' are discussed in the mouse section. A 'redraw' event occurs just before the 3D scene is redrawn on the screen, and a 'draw_complete' event occurs just after the redrawing (these event have rather technical uses such as timing how often redrawings occur, or how much time they take). You can bind more than one event to a function. The following will cause the callback function to be executed whether you click with the mouse or press a key on the keyboard: scene.bind('click keydown', myFunction) With the following statement, click event will no longer be sent to myFunction: scene.unbind('click', myFunction) The example program eventHandlers.py illustrates the callback method for handling many kinds of events. Pausing for keyboard or mouse input Often you want to pause for either mouse or keyboard input which you can do by executing scene.waitfor('click keydown').