After digging around in the source code, it looks like this is not possible.
def record(until='escape'):
"""
Records all keyboard events from all keyboards until the user presses the
given key combination. Then returns the list of events recorded, of type
`keyboard.KeyboardEvent`. Pairs well with
`play(events)`.
Note: this is a blocking function.
Note: for more details on the keyboard hook and events see `hook`.
"""
recorded = []
hook(recorded.append)
wait(until)
unhook(recorded.append)
return recorded
The parameter until
is passed into wait()
. Thus, wait()
must have code to handle an arbitrary key press, which it does not.
def wait(combination=None):
"""
Blocks the program execution until the given key combination is pressed or,
if given no parameters, blocks forever.
"""
wait, unlock = _make_wait_and_unlock()
if combination is not None:
hotkey_handler = add_hotkey(combination, unlock)
wait()
remove_hotkey(hotkey_handler)
Ultimately, there is no source code built to handle something like keyboard.record(until='any')
, so you'll have to find a workaround. Consider checking How do I make python to wait for a pressed key. However, if you need to record the arbitrary key you would have used to stop the recording, then use J-L's workaround:
import keyboard
k = keyboard.read_key() # in my python interpreter, this captures "enter up"
k = keyboard.read_key() # so repeat the line if in the python interpreter