KeyNet
KeyNet is a simple and powerful Python library for detecting keyboard events (key press, key release) and binding them to custom callbacks.
With KeyNet, you can easily build keyboard-based automation, hotkey triggers, or even interactive applications that react instantly when a key is pressed.
Features
- Detect key press and key release events
- Simple
.on(event, callback)API - Runs in a background thread, so your main app keeps working
- Works out-of-the-box on Windows
- Useful for automation, scripting, and productivity apps
Installation
You can install keynet by:
pip install keynet
Usage/Examples
Example 1 – Detect a single key
from keynet import KeyNet
kn = KeyNet()
def log_key(key):
print(f"[KeyLog] {key}")
kn.on("key_press", log_key)
kn.start()
input("Logging keys... Press Enter to quit\n")
Example 2 hotkey actions
from keynet import KeyNet
kn = KeyNet()
def key_action(key):
if key == "a":
print("You pressed A → Triggering Action 1")
elif key == "b":
print("You pressed B → Triggering Action 2")
elif key == "q":
print("Quit hotkeys (press Enter to exit)")
kn.stop()
kn.on("key_press", key_action)
kn.start()
input("Try pressing A, B, or Q\n")
Example 3 volume control
from keynet import KeyNet
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
kn = KeyNet()
def volume_control(key):
if key == "up":
current = volume.GetMasterVolumeLevelScalar()
volume.SetMasterVolumeLevelScalar(min(1.0, current + 0.1), None)
print(f"Volume UP → {int(volume.GetMasterVolumeLevelScalar() * 100)}%")
elif key == "down":
current = volume.GetMasterVolumeLevelScalar()
volume.SetMasterVolumeLevelScalar(max(0.0, current - 0.1), None)
print(f"Volume DOWN → {int(volume.GetMasterVolumeLevelScalar() * 100)}%")
kn.on("key_press", volume_control)
kn.start()
input("Press UP/DOWN arrow keys to control volume (Enter to quit)\n")
Example 4 - mouse position
from keynet import KeyNet
from pynput import mouse
kn = KeyNet()
mouse_controller = mouse.Controller()
def key_mouse_combo(key):
if key == "c":
pos = mouse_controller.position
print(f"Captured mouse position: {pos}")
elif key == "r":
mouse_controller.position = (100, 100)
print("Moved mouse to (100, 100)")
kn.on("key_press", key_mouse_combo)
kn.start()
input("Press C to capture mouse pos, R to move mouse (Enter to quit)\n")
API Reference
KeyNet Class
KeyNet()
Create a new event detector instance.
Keyboard Event Handlers
@detector.on_key(callback)
Registers a callback that triggers on every key press.
@detector.on_key
def log_key(event):
print(f"Key pressed: {event}")
@detector.on_hotkey(hotkeys: List[Set[str]])
Registers a callback that triggers when a hotkey combination is pressed.
@detector.on_hotkey([{"ctrl", "shift", "x"}])
def hotkey_action(event):
print("CTRL + SHIFT + X detected!")
Mouse Event Handlers
@detector.on_click(callback)
Registers a callback for mouse clicks.
@detector.on_click
def on_click(event):
print(f"Mouse clicked: {event}")
@detector.on_scroll(callback)
Registers a callback for mouse scroll events.
@detector.on_scroll
def on_scroll(event):
print(f"Scrolled: {event}")
System Event Handlers
@detector.on_volume_change(callback)
Registers a callback for system volume changes.
@detector.on_volume_change
def on_volume_change(volume_level):
print(f"Volume changed: {volume_level}")
Running the Detector
detector.start()
Starts listening for all registered events.
detector.start()
detector.stop()
Stops event detection.
detector.stop()
License
Authors
Metadata
Release files for keynet 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| keynet-0.2.2.tar.gz | 5.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| keynet-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / keynet-0.2.2.tar.gz
| Download URL | keynet-0.2.2.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b1e70e9d18d2b352d4160d16c96f8d42e5d57f479aecd661b551e1ca5570b8a6
|
|
BLAKE2b-256 checksum How to use checksums |
0ecb9ffc97eb0353f355702ea9a2e8b0fd85cb7960a11c6372fe144bb5c7367b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / keynet-0.2.2-py3-none-any.whl
| Download URL | keynet-0.2.2-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf80fdc2ae4fffe661ee609fbc7be132b818f35f76ac0cb6582b52dafb0d7d34
|
|
BLAKE2b-256 checksum How to use checksums |
d9791d47fef376572668c2a9c1132671cbc9f354e0bfe6c75bcdc4d9b2b5774f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for keynet 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.