Hi there! I was not pleased with the flexibility of Client Challenge, so I’ve made a better (if I may say so myself) alternative.
It lives on PyPI:
pip install streamlit-hotkeys
You may want to check it out if:
- you need to capure long presses
- have issues with layout shifts
- don’t want to have a shortcut being tied to a widget
Here is the demo for button-like interface:
import streamlit as st
import streamlit_hotkeys as hotkeys
# Activate early (top of the script)
hotkeys.activate([
hotkeys.hk("palette", "k", meta=True), # Cmd+K (mac)
hotkeys.hk("palette", "k", ctrl=True), # Ctrl+K (win/linux)
hotkeys.hk("save", "s", ctrl=True, prevent_default=True), # Ctrl+S
])
st.title("Hotkeys quick demo")
if hotkeys.pressed("palette"):
st.write("Open palette")
if hotkeys.pressed("save"):
st.write("Saved!")
st.caption("Try Cmd/Ctrl+K and Ctrl+S")
And for callback interface (works similar to on_click
):
import streamlit as st
import streamlit_hotkeys as hotkeys
st.title("Multiple callbacks")
hotkeys.activate([
hotkeys.hk("save", "s", ctrl=True, prevent_default=True),
])
def save_doc():
st.write("Saving...")
def toast_saved():
st.toast("Saved!")
# register both; each runs once per key press
hotkeys.on_pressed("save", save_doc)
hotkeys.on_pressed("save", toast_saved)
# you can still branch with pressed() afterwards
if hotkeys.pressed("save"):
st.info("Thank you for saving")
FYI: earlier post was flagged as an ad, sorry for that! I dulled down the language a bit in this version. I really found this component extremely useful in my projects and hope you will as well!
I’ll later link in the comments a multiplayer snake game powered by this component.
Cheers