Skip to main content

Keyboard hotkeys for Streamlit (Ctrl/Cmd/Alt/Shift combos) with edge-triggered events.

Project description

Streamlit Hotkeys

PyPI Python Versions License Wheel Streamlit Component Downloads

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 id across multiple bindings (e.g., Cmd+K or Ctrl+K → palette)
  • prevent_default to block browser shortcuts (e.g., Ctrl/Cmd+S)
  • Layout-independent physical keys via code="KeyK" / code="Digit1"
  • ignore_repeat to suppress repeats while a key is held
  • Built-in legend: add help="..." in hk(...) and call hotkeys.legend()
  • Multi-page friendly; use key= for multiple independent managers
  • Optional debug=True to 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=True to 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

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

streamlit_hotkeys-0.4.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_hotkeys-0.4.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

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

Hashes for streamlit_hotkeys-0.4.0.tar.gz
Algorithm Hash digest
SHA256 775fdb9f5e88a24fd9deb05cf949850f57fe429ae5c248c22f0854eb062e9a06
MD5 ee9a4126dd0dd3da94cddf38ea387e1e
BLAKE2b-256 8dff4bfae1dddeffdc8f9a1fc865bc2dbd54284e13fa0aecdb13e89160dc247f

See more details on using hashes here.

File details

Details for the file streamlit_hotkeys-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_hotkeys-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d87abf7973dfb33e01e9f9cdda1d4bf2b73a5c0ce5583bcfcb4ad3c94d70a2f
MD5 cb3d8eb4b6672a46f5bd61ef1383c6df
BLAKE2b-256 d948f36ff372eda7f54060d68f0e43b91a27bafbcd6688d870a76d0c74071604

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page