Keyboard hotkeys for Streamlit (Ctrl/Cmd/Alt/Shift combos) with edge-triggered events.
Project description
Streamlit Hotkeys
Keyboard hotkeys for Streamlit - capture Ctrl/Cmd/Alt/Shift + key anywhere in your app and trigger Python once per press (edge-triggered). Uses a single invisible manager component.
Important: call activate(...) as early as possible in each page. Activation injects CSS that collapses the manager iframe, which avoids layout flicker.
Installation
pip install streamlit-hotkeys
Quick start
import streamlit as st
import streamlit_hotkeys as hotkeys
hotkeys.activate([
hotkeys.hk("palette", "k", meta=True, help="Open palette (mac)"),
hotkeys.hk("palette", "k", ctrl=True, help="Open palette (win/linux)"),
hotkeys.hk("save", "s", ctrl=True, prevent_default=True, help="Save"),
hotkeys.hk("show_legend", "?", shift=True, help="Show shortcuts"),
], key="global")
def save():
st.success("Saved!")
if hotkeys.pressed("save"):
st.info("Thank you for saving")
hotkeys.on_pressed("save", save)
@hotkeys.on_pressed("palette")
def open_palette():
st.info("Palette opened")
@st.dialog("Keyboard Shortcuts")
def _shortcuts_dialog():
hotkeys.legend()
if hotkeys.pressed("show_legend"):
_shortcuts_dialog()
st.write("Press Cmd/Ctrl+K, Ctrl+S or Shift+?")
Features
- Single invisible manager (one iframe for the whole page)
- Activates early and auto-collapses its iframe to avoid layout flicker
- Edge-triggered events (per-id seq; no sticky booleans)
- Bind single keys or modifier combos (
ctrl,alt,shift,meta) - Reuse the same
idacross multiple bindings (e.g., Cmd+K or Ctrl+K →palette) prevent_defaultto block browser shortcuts (e.g., Ctrl/Cmd+S)- Layout-independent physical keys via
code="KeyK"/code="Digit1" ignore_repeatto suppress repeats while a key is held- Built-in legend: add
help="..."inhk(...)and callhotkeys.legend() - Multi-page friendly; use
key=for multiple independent managers - Optional
debug=Trueto log matches in the browser console
API
hk(...) - define a binding
hk(
id: str,
key: str | None = None, # for example, "k", "Enter", "ArrowDown"
*,
code: str | None = None, # for example, "KeyK" (if set, 'key' is ignored)
alt: bool | None = False, # True=require, False=forbid, None=ignore
ctrl: bool | None = False,
shift: bool | None = False,
meta: bool | None = False,
ignore_repeat: bool = True,
prevent_default: bool = False,
help: str | None = None, # text shown in the legend (optional)
) -> dict
activate(*bindings, key="global", debug=False) -> None
Register bindings and render the single manager. Accepts hk(...) dicts, a list of them, or a mapping id -> spec.
pressed(id, *, key="global") -> bool
Return True exactly once when the binding id fires.
legend(*, key="global") -> None
Render a grouped shortcuts list (merges multiple bindings that share the same id, and shows the first non-empty help string per id).
Shortcuts Legend Example
import streamlit as st
import streamlit_hotkeys as hotkeys
hotkeys.activate({
"palette": [
{"key": "k", "meta": True, "help": "Open command palette"},
{"key": "k", "ctrl": True},
],
"save": {"key": "s", "ctrl": True, "prevent_default": True, "help": "Save document"},
}, key="global")
@st.dialog("Keyboard Shortcuts")
def _shortcuts():
hotkeys.legend()
if hotkeys.pressed("palette"):
_shortcuts()
Notes and limitations
- Browsers reserve some shortcuts. Use
prevent_default=Trueto keep the event for your app when allowed. - Combos mean modifiers + one key. The platform does not treat two non-modifier keys pressed together (for example,
A+S) as a single combo. - The page must have focus; events are captured at the document level.
Similar projects
- streamlit-keypress - Original "keypress to Python" component by Sudarsan.
- streamlit-shortcuts - Keyboard shortcuts for buttons and widgets; supports multiple bindings and hints.
- streamlit-keyup - Text input that emits on every keyup (useful for live filtering).
- keyboard_to_url - Bind a key to open a URL in a new tab.
Credits
Inspired by streamlit-keypress by Sudarsan. This implementation adds a multi-binding manager, edge-triggered events, modifier handling, preventDefault, and KeyboardEvent.code.
Contributing
Issues and PRs are welcome.
License
MIT. See LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_hotkeys-0.4.0.tar.gz.
File metadata
- Download URL: streamlit_hotkeys-0.4.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
775fdb9f5e88a24fd9deb05cf949850f57fe429ae5c248c22f0854eb062e9a06
|
|
| MD5 |
ee9a4126dd0dd3da94cddf38ea387e1e
|
|
| BLAKE2b-256 |
8dff4bfae1dddeffdc8f9a1fc865bc2dbd54284e13fa0aecdb13e89160dc247f
|
File details
Details for the file streamlit_hotkeys-0.4.0-py3-none-any.whl.
File metadata
- Download URL: streamlit_hotkeys-0.4.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d87abf7973dfb33e01e9f9cdda1d4bf2b73a5c0ce5583bcfcb4ad3c94d70a2f
|
|
| MD5 |
cb3d8eb4b6672a46f5bd61ef1383c6df
|
|
| BLAKE2b-256 |
d948f36ff372eda7f54060d68f0e43b91a27bafbcd6688d870a76d0c74071604
|