IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Description of Objects in VPython

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.

Here is a test routine that let's you type text into a label:

 

prose = label() # initially blank text

while 1:

    if scene.kb.keys: # is there an event waiting to be processed?

        s = scene.kb.getkey() # obtain keyboard information

        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 one letter

        elif s == 'shift+delete':

            prose.text = '' # erase all the text

 

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

You can also input a line of text from the keyboard by highlighting the output window (where print statements are displayed), using the standard Python function raw_input(). The statement text = raw_input() accepts a line of typing ending with Enter and sets text to the input, not including the end-of-line. The statement text = raw_input('Type something: ') prompts for the input before you type.