A cross-platform hotkey module.
Project description
PyHotKey
Description
PyHotKey is a cross-platform hotkey module for Python. Based on "pynput".
Installation
pip install PyHotKey
Usage
Import:
from PyHotKey import Key, keyboard_manager as manager
Register hotkey:
# Register hotkey (multiple keys)
hotkey_id1 = manager.register_hotkey(func1, [Key.ctrl_l, Key.alt_l, 'z'])
# Register hotkey (single key)
hotkey_id2 = manager.register_hotkey(func2, [Key.caps_lock], 2, func2_arg1, func2_arg2=1)
# Unregister hotkey by key list
r1 = manager.unregister_hotkey_by_keys([Key.ctrl_l, Key.alt_l, 'z'])
# Unregister hotkey by hotkey id
r2 = manager.unregister_hotkey_by_id(hotkey_id2)
Recording hotkey:
# The callback function for recording hotkey
# You can use "key_list" to register hotkey
def callback(key_list):
print(key_list)
# Start recording a hotkey with multiple keys
manager.start_recording_hotkey_multiple(callback)
# Start recording a hotkey with single keys
manager.start_recording_hotkey_single(callback)
# Stop recording hotkey
manager.stop_recording()
PS: For more usage check the example on GitHub.
Controlling keyboard
# Press
manager.press(Key.space)
# Release
manager.release('z')
# Tap (press and release)
manager.tap('x')
# Do something while holding down certain keys
with manager.pressed(Key.ctrl, Key.shift) as r:
if r:
do_something()
# Type a string
manager.type('Xpp521')
PS: If you are recording hotkey, these apis won't work.
Other APIS
# Print all hotkeys
print(manager.hotkeys)
# Print currently pressed keys
print(manager.pressed_keys)
# Print recording state
print(manager.recording)
# Strict mode (for hotkeys with multiple keys)
# The pressed keys must be strictly equal to the hotkey
manager.strict_mode = False
# TTL: time to live (for hotkeys with multiple keys)
# When a key is pressed for more than TTL seconds,
# it will be removed from the currently pressed key list
manager.ttl = 7
# Interval: the max interval time between each press (for hotkeys with single key)
manager.interval = 0.5
Keyboard Listener
# Print keyboard listener's running state
print(manager.running)
# Stop keyboard listener
# When stopped, hotkey related functions won't work
manager.stop()
# Start keyboard listener
# You can restart the listener after stopping it
manager.start()
PS: Generally, you may not use these apis.
Logger:
# Turn on the logger
manager.logger = True
# Set a file for logging ("append" mode)
manager.set_log_file('Loggggggg.log', 'a')
Release Note
v1.4.0 - 2022 Reborn
After 3 years I'm back with the new "PyHotKey".
Changes:
- Fixed a lot of bugs.
- Now you can record hotkey and control keyboard.
- Real cross platform this time.
- And more convenient apis...
Check "README.md".
v1.3.3
Bug Fixes
- Combination hot key: Fix the keystroke value error of combination hot key.
Refactor
- Simplify README.md.
v1.3.2
Bug Fixes
- Log path: fix the default log path overwrites the custom log path when setting "manager.logger = True".
Refactor
- Adjust code structure.
- Rewrite README.md.
v1.3.1
- Delete a deprecated class.
- Replace root logger with a separate logger.
- Rename property "hot_keys" to "hotKeyList".
- Change documents and some code comments.
v1.3.0
- Currently, users can customize the log path.
- Optimize code.
v1.2.0
- Add logger.
- Optimize code.
- Attempt to fix a potential bug.
v1.1.1
- Remove log message.
v1.1.0
- Currently, the trigger function supports arguments.
- No longer need to call manager.start() manually.
- Fix multiple type hot key bug.
v1.0
- The first version.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
PyHotKey-1.4.0.tar.gz
(13.5 kB
view hashes)
Built Distribution
PyHotKey-1.4.0-py3-none-any.whl
(15.9 kB
view hashes)