-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Link Key bindings to buttons #641
Comments
Hey Jovan! You're right, it isn't natively possible to link key presses to interactions with an element. I suppose you could in principle accomplish it via a solara/solara/components/input.py Lines 14 to 36 in 86fd5dd
You can see how we use it (sorry to quote solara/solara/lab/components/chat.py Lines 86 to 90 in 86fd5dd
|
What about putting something like this in solara.lab? from typing import Callable
import solara
show_dialog = solara.reactive(False)
@solara.component_vue("hotkey.vue")
def HotKey(
key: str,
ctrl: bool = False,
shift: bool = False,
meta: bool = False,
prevent_default: bool = False,
event_pressed: Callable[[], None] = None,
): ...
@solara.component
def Page():
with solara.Column() as main:
solara.InputText("No focus")
solara.InputText("No focus")
solara.Button("Show dialog", on_click=lambda: show_dialog.set(True))
with solara.v.Dialog(
v_model=show_dialog.value,
on_v_model=show_dialog.set,
persistent=True,
max_width="500px",
):
with solara.v.Sheet(class_="pa-4") as sheet:
solara.InputText("No focus")
solara.InputText("No focus")
close = solara.Button("Close", on_click=lambda: show_dialog.set(False))
def close_dialog_on_q(widget, event, data):
print("event", event)
# if event.key == "Escape":
show_dialog.set(False)
HotKey(key="q", ctrl=True, event_pressed=lambda ignore: show_dialog.set(False))
HotKey(key="o", event_pressed=lambda ignore: show_dialog.set(True)) // hotkey.vue <template>
<span style="display: none;">key {{key}}</span>
</template>
<script>
module.exports = {
mounted() {
console.log("mounted", this.key)
document.addEventListener('keydown', this.hotkeyPress);
},
destroyed() {
document.removeEventListener('keydown', this.hotkeyPress);
},
methods: {
hotkeyPress: function(e) {
if (e.key === this.key) {
console.log("key", this.key, e.key, e)
if(this.metaKey && !e.metaKey) {
return
}
if(this.ctrl && !e.ctrlKey) {
return
}
if(this.shift && !e.shiftKey) {
return
}
if(this.alt && !e.altKey) {
return
}
if(this.meta && !e.metaKey) {
return
}
if(this. prevent_default) {
e.preventDefault()
}
console.log("pressed", this.key)
this.pressed()
}
}
}
};
</script> @JovanVeljanoski you can use this now in your app, and see if a pattern like this works for you, we can iterate on it and see if we want it in lab based on your feedback. |
Hi, Thanks for these examples! @maartenbreddels that is a nice idea indeed. Works well, and if it was already in Otherwise I like this approach! |
Some other small feedback (can be ignored ofc) |
Made some minor improvements, and made the event handler |
Great! This works very nice! Thanks - feel free to close this. Would be great if something like this is one day in solara(.lab). Cheers! |
Would it be possible to make keys / key-bindings / correspond to button clicks.
I.e. one button can be linked to
Return
, another to arrow-key, etc..The text was updated successfully, but these errors were encountered: