You are to interface a keyboard to the microprocessor. The keyboard generates a 1-bit control signal, READY, which goes high when a key is pressed and then goes low when the ASCII character of the key is read by the microprocessor through the port at the address $6000.
The package pynput.keyboard
contains classes for controlling and monitoringthe keyboard.
Use pynput.keyboard.Controller
like this:
Use pynput.keyboard.Listener
like this:
A keyboard listener is a threading.Thread
, and all callbacks will beinvoked from the thread.
Call pynput.keyboard.Listener.stop
from anywhere, or raisepynput.keyboard.Listener.StopException
or return False
from a callbackto stop the listener.
Starting a keyboard listener may be subject to some restrictions on yourplatform.
On Mac OSX, one of the following must be true:
On Windows, virtual events sent by other processes may not be received.This library takes precautions, however, to dispatch any virtual eventsgenerated to all currently running listeners of the current process.
pynput.keyboard.
Controller
[source]¶A controller for sending virtual keyboard events to the system.
InvalidCharacterException
[source]¶The exception raised when and invalid character is encountered inthe string passed to Controller.type()
.
Its first argument is the index of the character in the string, and thesecond the character.
Controller.
InvalidKeyException
[source]¶The exception raised when and invalid key
parameter is passed toeither Controller.press()
or Controller.release()
.
Its first argument is the key
parameter.
Controller.
alt_gr_pressed
¶Whether altgr is pressed.
Controller.
alt_pressed
¶Whether any alt key is pressed.
Controller.
ctrl_pressed
¶Whether any ctrl key is pressed.
Controller.
modifiers
¶The currently pressed modifier keys.
Only the generic modifiers will be set; when pressing eitherKey.shift_l
, Key.shift_r
or Key.shift
, onlyKey.shift
will be present.
Use this property within a context block thus:
This ensures that the modifiers cannot be modified by another thread.
Controller.
press
(key)[source]¶Presses a key.
A key may be either a string of length 1, one of the Key
members or a KeyCode
.
Strings will be transformed to KeyCode
usingKeyCode.char()
. Members of Key
will be translated totheir value()
.
Parameters: | key – The key to press. |
---|---|
Raises: |
|
Controller.
pressed
(*args, **kwds)[source]¶Executes a block with some keys pressed.
Parameters: | keys – The keys to keep pressed. |
---|
Controller.
release
(key)[source]¶Releases a key.
A key may be either a string of length 1, one of the Key
members or a KeyCode
.
Strings will be transformed to KeyCode
usingKeyCode.char()
. Members of Key
will be translated totheir value()
.
Parameters: | key – The key to release. If this is a string, it is passed to |
---|---|
Raises: |
|
Controller.
shift_pressed
¶Whether any shift key is pressed, or caps lock is toggled.
Controller.
touch
(key, is_press)[source]¶Calls either press()
or release()
depending on the valueof is_press
.
Features. You can set a custom scan time and the alerting mechanism will notify the server administrator if any warnings are detected.
Parameters: |
|
---|
Controller.
type
(string)[source]¶Types a string.
This method will send all key presses and releases necessary to typeall characters in the string.
Parameters: | string (str) – The string to type. |
---|---|
Raises InvalidCharacterException: | |
if an untypable character isencountered |
pynput.keyboard.
Listener
(on_press=None, on_release=None)[source]¶A listener for keyboard events.
Instances of this class can be used as context managers. This is equivalentto the following code:
This class inherits from threading.Thread
and supports all itsmethods. It will set daemon
to True
when created.
Parameters: |
|
---|
running
¶Whether the listener is currently running.
start
()¶Start the thread’s activity.
It must be called at most once per thread object. It arranges for theobject’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on thesame thread object.
stop
()¶Stops listening for mouse events.
When this method returns, no more events will be delivered.
wait
()¶Waits for this listener to become ready.
pynput.keyboard.
Key
[source]¶A class representing various buttons that may not correspond toletters. This includes modifier keys and function keys.
The actual values for these items differ between platforms. Some platformsmay have additional buttons, but these are guaranteed to be presenteverywhere.
alt
= <Key.f1: 0>¶A generic Alt key. This is a modifier.
alt_gr
= <Key.f1: 0>¶The AltGr key. This is a modifier.
alt_l
= <Key.f1: 0>¶The left Alt key. This is a modifier.
alt_r
= <Key.f1: 0>¶The right Alt key. This is a modifier.
backspace
= <Key.f1: 0>¶The Backspace key.
caps_lock
= <Key.f1: 0>¶The CapsLock key.
cmd
= <Key.f1: 0>¶A generic command button. On PC platforms, this corresponds to theSuper key or Windows key, and on Mac it corresponds to the Commandkey. This may be a modifier.
cmd_l
= <Key.f1: 0>¶The left command button. On PC platforms, this corresponds to theSuper key or Windows key, and on Mac it corresponds to the Commandkey. This may be a modifier.
cmd_r
= <Key.f1: 0>¶The right command button. On PC platforms, this corresponds to theSuper key or Windows key, and on Mac it corresponds to the Commandkey. This may be a modifier.
ctrl
= <Key.f1: 0>¶A generic Ctrl key. This is a modifier.
ctrl_l
= <Key.f1: 0>¶The left Ctrl key. This is a modifier.
ctrl_r
= <Key.f1: 0>¶The right Ctrl key. This is a modifier.
delete
= <Key.f1: 0>¶The Delete key.
down
= <Key.f1: 0>¶A down arrow key.
end
= <Key.f1: 0>¶The End key.
enter
= <Key.f1: 0>¶The Enter or Return key.
esc
= <Key.f1: 0>¶The Esc key.
f1
= <Key.f1: 0>¶The function keys. F1 to F20 are defined.
home
= <Key.f1: 0>¶The Home key.
insert
= <Key.f1: 0>¶The Insert key. This may be undefined for some platforms.
left
= <Key.f1: 0>¶A left arrow key.
menu
= <Key.f1: 0>¶The Menu key. This may be undefined for some platforms.
num_lock
= <Key.f1: 0>¶The NumLock key. This may be undefined for some platforms.
page_down
= <Key.f1: 0>¶The PageDown key.
page_up
= <Key.f1: 0>¶The PageUp key.
pause
= <Key.f1: 0>¶The Pause/Break key. This may be undefined for some platforms.
print_screen
= <Key.f1: 0>¶The PrintScreen key. This may be undefined for some platforms.
right
= <Key.f1: 0>¶A right arrow key.
scroll_lock
= <Key.f1: 0>¶The ScrollLock key. This may be undefined for some platforms.
shift
= <Key.f1: 0>¶A generic Shift key. This is a modifier.
shift_l
= <Key.f1: 0>¶The left Shift key. This is a modifier.
shift_r
= <Key.f1: 0>¶The right Shift key. This is a modifier.
space
= <Key.f1: 0>¶The Space key.
tab
= <Key.f1: 0>¶The Tab key.
up
= <Key.f1: 0>¶An up arrow key.
pynput.keyboard.
KeyCode
(vk=0, char=None, is_dead=False)[source]¶from_char
(char)[source]¶Creates a key from a character.
Parameters: | char (str) – The character. |
---|---|
Returns: | a key code |
from_dead
(char)[source]¶Creates a dead key.
Parameters: | char – The dead key. This should be the unicode characterrepresenting the stand alone character, such as '~' forCOMBINING TILDE. |
---|---|
Returns: | a key code |
from_vk
(vk, **kwargs)[source]¶Creates a key from a virtual key code.
Parameters: |
|
---|---|
Returns: | a key code |
join
(key)[source]¶Applies this dead key to another key and returns the result.
Joining a dead key with space (''
) or itself yields the non-deadversion of this key, if one exists; for example,KeyCode.from_dead('~').join(KeyCode.from_char(''))
equalsKeyCode.from_char('~')
andKeyCode.from_dead('~').join(KeyCode.from_dead('~'))
.
Parameters: | key (KeyCode) – The key to join with this key. |
---|---|
Returns: | a key code |
Raises ValueError: | |
if the keys cannot be joined |