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

st.set_page_config(page_title="Hotkeys Demo")

# Activate early so the manager is hidden from the first frame
hotkeys.activate([
    hotkeys.hk("palette", "k", meta=True),                # Cmd+K (macOS)
    hotkeys.hk("palette", "k", ctrl=True),                # Ctrl+K (Windows/Linux)
    hotkeys.hk("save", "s", ctrl=True, prevent_default=True), # Ctrl+S (block browser save)
    hotkeys.hk("down", "ArrowDown"),
], key="global")

st.title("Hotkeys demo")

if hotkeys.pressed("palette"):
    st.success("Open palette")

if hotkeys.pressed("save"):
    st.success("Saved!")

if hotkeys.pressed("down"):
    st.write("Move selection down")

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.3.0.tar.gz (13.4 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.3.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_hotkeys-0.3.0.tar.gz.

File metadata

  • Download URL: streamlit_hotkeys-0.3.0.tar.gz
  • Upload date:
  • Size: 13.4 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.3.0.tar.gz
Algorithm Hash digest
SHA256 d58d7c557c1b41bf797b5b6dfd71594cd129333eda0b4cf0b1cc2b0477896e5c
MD5 e6e345997d8b91c05daa7deaf8384deb
BLAKE2b-256 c04c20a16889f5a10efb61073498c6af40898d6d1091135e9b90b0587859cb15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for streamlit_hotkeys-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1990d5b34c2d33a9bb214ec0cdb8d1b096582a8348d6c6b69f03ae818ac10c57
MD5 08cb12558b985abe526582f4656d04bb
BLAKE2b-256 00d581819da8dbf1a3e849d1120bf583907bea345fefd2ca9a44fa9b8e1e5ca7

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