Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Pictures of 3D objects

Choose a 3D object:

Work with 3D objects:

Windows, Events, & Files:

Vector operations

Graphs

factorial/combin

What's new in Visual 5

VPython web site
Visual license
Python web site
Math module (sqrt etc.)
Numpy module (arrays)

# Keyboard Interactions

If scene.kb.keys is nonzero, one or more keyboard events have been stored, waiting to be processed.

Executing key = scene.kb.getkey() obtains a keyboard input and removes it from the input queue. If there are no events waiting to be processed, getkey() waits until a key is pressed.

If len(key) == 1, the input is a single printable character such as 'b' or 'B' or new line ('\n') or tab ('\t'). Otherwise key is a multicharacter string such as 'escape' or 'backspace' or 'f3'. For such inputs, the ctrl, alt, and shift keys are prepended to the key name. For example, if you hold down the shift key and press F3, key will be the character string 'shift+f3', which you can test for explicitly. If you hold down all three modifier keys, you get 'ctrl+alt+shift+f3'; the order is always ctrl, alt, shift.

Multicharacter names include delete, backspace, page up, page down, home, end, left, up, right, down, numlock, scrlock, f1, f2, f3, f4, f5, f6, f7, f8. Windows and Linux also have f9, f11, f12, insert.

Here is a test routine that lets you type text into a label:

from visual import *
prose = label() # initially blank text
while True:
if scene.kb.keys: # event waiting to be processed?
s = scene.kb.getkey() # get keyboard info
if len(s) == 1:
prose.text += s # append new character
elif ((s == 'backspace' or s == 'delete') and
len(prose.text)) > 0:
prose.text = prose.text[:-1] # erase letter
elif s == 'shift+delete':
prose.text = '' # erase all text

Note that mouse events also provide information about the ctrl, alt, and shift keys, which may be used to modify mouse actions.