Keyboard functions
class keyboard
The keyboard
class is used to collect keyboard responses.
Example:
# Wait for a 'z' or 'x' key with a timeout of 3000 ms
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp, keylist=['z', 'x'], timeout=3000)
start_time = self.time()
key, end_time = my_keyboard.get_key()
exp.set('response', key)
exp.set('response_time', end_time - start_time)
Function list:
- function keyboard.__init__(experiment, keylist=None, timeout=None)
- function keyboard.flush()
- function keyboard.get_key(keylist=None, timeout=None)
- function keyboard.get_mods()
- function keyboard.set_keylist(keylist=None)
- function keyboard.set_timeout(timeout=None)
- function keyboard.show_virtual_keyboard(visible=True)
function keyboard.__init__(experiment, keylist=None, timeout=None)
Constructor. Intializes the keyboard object.
Keys can be identified either by character or name. This is case insensitive. Naming keys using ASCII (integer) key codes is deprecated.
For example:
- The key ‘a’ is represented by ‘a’ and ‘A’.
- The up arrow is represented by ‘up’ and ‘UP’.
- The ‘/’ key is represented by ‘/’, ‘slash’, and ‘SLASH’.
- The spacebar is represented by ‘space’ and ‘SPACE’.
For a complete list of available key names, click on the ‘list available keys’ button in the keyboard_response tab within OpenSesame.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp, keylist=['z', 'm'], timeout=2000)
Arguments:
experiment
– The experiment object.- Type: experiment
Keywords:
keylist
– A list of human-readable keys that are accepted orNone
to accept all keys.- Type: list, NoneType
- Default: None
timeout
– A numeric value specifying a timeout in milliseconds orNone
for no (i.e. infinite) timeout.- Type: int, float, NoneType
- Default: None
function keyboard.flush()
Clears all pending keyboard input, not limited to the keyboard.
Returns:
True if a key had been pressed (i.e., if there was something to flush) and False otherwise.
- Type: bool
function keyboard.get_key(keylist=None, timeout=None)
Collects a single key press.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp, timeout=2000)
response, timestamp = my_keyboard.get_key()
if response == None:
print('A timeout occurred!')
Keywords:
keylist
– A list of human-readable keys that are accepted orNone
to accept all keys.- Type: list, NoneType
- Default: None
timeout
– A numeric value specifying a timeout in milliseconds orNone
for no (i.e. infinite) timeout.- Type: int, float, NoneType
- Default: None
Returns:
A (key, timestamp)
tuple. key
is None if a timeout occurs.
- Type: tuple
function keyboard.get_mods()
Returns a list of keyboard moderators (e.g., shift, alt, etc.) that are currently pressed.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp)
moderators = my_keyboard.get_mods()
if 'shift' in moderators:
print('The shift-key is down!')
Returns:
A list of keyboard moderators. An empty list is returned if no moderators are pressed.
- Type: list
function keyboard.set_keylist(keylist=None)
Sets the list of accepted keys.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp)
my_keyboard.set_keylist( ['z', 'm'] )
Keywords:
keylist
– A list of human-readable keys that are accepted orNone
to accept all keys.- Type: list, NoneType
- Default: None
function keyboard.set_timeout(timeout=None)
Sets a timeout.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp)
my_keyboard.set_timeout(2000)
Keywords:
timeout
– A numeric value specifying a timeout in milliseconds orNone
for no (i.e. infinite) timeout.- Type: int, float, NoneType
- Default: None
function keyboard.show_virtual_keyboard(visible=True)
Shows or hides a virtual keyboard if this is supported by the back-end. This function is only necessary if you want the virtual keyboard to remain visible while collecting multicharacter responses. Otherwise, keyboard.get_key will implicitly shown and hide the keyboard for a single-character response.
This function does nothing for back-ends that do not support virtual keyboards.
Example:
from openexp.keyboard import keyboard
my_keyboard = keyboard(exp)
my_keyboard.show_virtual_keyboard(True)
response1, timestamp2 = my_keyboard.get_key()
response2, timestamp2 = my_keyboard.get_key()
my_keyboard.show_virtual_keyboard(False)
Keywords:
visible
– True if the keyboard should be shown, False otherwise.- Type: bool
- Default: True