Keyboard
- class quanser.devices.interfaces.Keyboard
- A Python wrapper for the Quanser Devices API interface for the keyboard. - Example - >>> from quanser.devices import Keyboard >>> keyboard = Keyboard() - createBuffer(num_keys)
- Creates a buffer for the keyboard states. - Parameters
- num_keys (int) – The maxiumum number of key states that will be stored in the buffer. 
- Return type
- Returns a buffer for keyboard states supporting the requested number of keys. 
 - Example - Print the state of the Enter, Shift, Ctrl and A keys. Exit the loop if Ctrl+Shift+Enter pressed simultaneously. - >>> from quanser.devices import Keyboard, VirtualKeyCodes >>> import array as arr >>> >>> keyboard = Keyboard() >>> >>> keys = arr.array('i', [VirtualKeyCodes.VK_RETURN, VirtualKeyCodes.VK_SHIFT, VirtualKeyCodes.VK_CONTROL, VirtualKeyCodes.VK_A]) >>> states = keyboard.createBuffer(len(keys)) >>> >>> while True: >>> keyboard.getKeyStates(keys, states) >>> print(f"Return: {states[0]:x} Shift: {states[1]:x} Ctrl: {states[2]:x} A: {states[3]:x}") >>> if states[0] & KeyState.KEY_DOWN and states[1] & KeyState.KEY_DOWN and states[2] & KeyState.KEY_DOWN: # Press Ctrl+Shift+Enter to exit loop >>> break 
 - getKeyState(virtual_key_code)
- Gets the state of the given key. - Parameters
- virtual_key_code (int) – A virtual key code indicating the key whose state is being retrieved. Use the - VirtualKeyCodesclass to use a symbolic name for the virtual key code.
- Return type
- Returns the state of the key. Use the - KeyStateclass to compare values with symbolic names.
 - Example - Exit a loop when the Esc key is pressed. - >>> from quanser.devices import Keyboard, KeyState, VirtualKeyCodes >>> >>> keyboard = Keyboard() >>> >>> while True: >>> state = keyboard.getKeyState(VirtualKeyCodes.VK_ESCAPE) >>> print(f"Escape: {state:x}") >>> if state & KeyState.KEY_DOWN: # Press Esc to exit loop >>> break 
 - getKeyStates(virtual_key_codes, states)
- Gets the state of the given keys. - Parameters
- virtual_key_codes (int[]) – An array of virtual key codes indicating the keys whose states are being retrieved. Use the - VirtualKeyCodesclass to use symbolic names for virtual key codes.
- states (t_key_state[]) – A buffer created using the - createBuffer()method that will be filled with the state of each corresponding key in the virtual_key_codes array. The buffer must be at least as large as the virtual_key_codes array. It should only be created once and then used multiple times for best performance. Use the- KeyStateclass to compare values with symbolic names.
 
 - Examples - Print the state of the Enter, Shift, Ctrl and A keys. Exit the loop if Ctrl+Shift+Enter pressed simultaneously. Use array for the virtual key codes. - >>> from quanser.devices import Keyboard, KeyState, VirtualKeyCodes >>> import array as arr >>> >>> keyboard = Keyboard() >>> >>> keys = arr.array('l', [VirtualKeyCodes.VK_RETURN, VirtualKeyCodes.VK_SHIFT, VirtualKeyCodes.VK_CONTROL, VirtualKeyCodes.VK_A]) >>> states = keyboard.createBuffer(len(keys)) >>> >>> while True: >>> keyboard.getKeyStates(keys, states) >>> print(f"Return: {states[0]:x} Shift: {states[1]:x} Ctrl: {states[2]:x} A: {states[3]:x}") >>> if states[0] & KeyState.KEY_DOWN and states[1] & KeyState.KEY_DOWN and states[2] & KeyState.KEY_DOWN: # Press Ctrl+Shift+Enter to exit loop >>> break - Print the state of the Enter, Shift, Ctrl and A keys. Exit the loop if Ctrl+Shift+Enter pressed simultaneously. Use numpy array for the virtual key codes. - >>> from quanser.devices import Keyboard, KeyState, VirtualKeyCodes >>> import numpy as np >>> >>> keyboard = Keyboard() >>> >>> keys = np.array([VirtualKeyCodes.VK_RETURN, VirtualKeyCodes.VK_SHIFT, VirtualKeyCodes.VK_CONTROL, VirtualKeyCodes.VK_A], dtype=np.int32) >>> states = keyboard.createBuffer(len(keys)) >>> >>> while True: >>> keyboard.getKeyStates(keys, states) >>> print(f"Return: {states[0]:x} Shift: {states[1]:x} Ctrl: {states[2]:x} A: {states[3]:x}") >>> if states[0] & KeyState.KEY_DOWN and states[1] & KeyState.KEY_DOWN and states[2] & KeyState.KEY_DOWN: # Press Ctrl+Shift+Enter to exit loop >>> break