Credits To Wanna-Pizza Developer
Easily bind global keyboard shortcuts in your Flet applications with a flexible, event-driven HotKey system. Supports Ctrl, Shift, Alt, and multi-modifier combinations such as:
ctrl+fctrl+shift+dalt+shift+xctrl+alt+shift+s
Designed for clarity, extensibility, and clean integration with any Flet project.
- ✔️ Simple API:
manager.add("ctrl+f", callback) - ✔️ Support for multiple modifiers
- ✔️ Detailed event object (
HotKeyEvent) - ✔️ Works anywhere in the page (global keyboard listener)
- ✔️ Lightweight and dependency-free
- ✔️ Supports multiple hotkeys at once
- ✔️ Clean parsing logic for hotkey strings
Copy the HotKey folder into your project directory.
from HotKey import *from HotKey import *
from flet import *
def main(page: Page):
manager = HotKeysManager(page)
text = Text("Hello World")
alert = AlertDialog(
title=Text("Change Text Value"),
content=Column(
controls=[
TextField(
value=text.value,
on_change=lambda e: (
setattr(text, "value", e.control.value),
page.update()
),
multiline=True,
autofocus=True
),
Row(
controls=[
ElevatedButton(
text="Change Text",
on_click=close_alert
)
],
alignment=MainAxisAlignment.END
)
],
tight=True
)
)
page.overlay.append(alert)
def close_alert():
alert.open = False
page.update()
# -------------------------
# callback with EVENT
# -------------------------
def change_text_value(event: HotKeyEvent):
print("Hotkey:", event.hotkey)
print("Key pressed:", event.key)
print("Modifiers:", event.modifiers)
print("Time:", event.timestamp)
alert.open = not alert.open
page.update()
# Adding the hoykey combination to manager
manager.add("ctrl+f", change_text_value)
page.add(text)
app(target=main)Your callback receives a HotKeyEvent instance containing:
| Attribute | Description |
|---|---|
page |
The Flet Page object |
hotkey |
The hotkey string that was triggered ("ctrl+f") |
key |
The actual key pressed ("f") |
modifiers |
A set of modifiers like {"ctrl", "shift"} |
timestamp |
Time when the hotkey fired |
The system supports:
ctrlshiftalt- Any normal key (letters, numbers, F-keys)
Example valid combinations:
ctrl+a
ctrl+shift+e
alt+v
alt+shift+f1
ctrl+alt+shift+p
Order does not matter — shift+ctrl+p is equivalent to ctrl+shift+p.
manager.add("ctrl+n", new_file)
manager.add("ctrl+shift+s", save_as)
manager.add("alt+q", quit_app)manager.remove("ctrl+f")manager.add("ctrl+shift+x", lambda e: print("Triggered!"))- Hotkeys work only when the page is focused.
- Hotkeys can conflict with browser/system shortcuts.
- On mobile platforms, hotkeys may not be supported.
Feel free to extend or improve the HotKey system. PRs and suggestions are welcome!