ipywidgets icon indicating copy to clipboard operation
ipywidgets copied to clipboard

Question: Bind key-press (hotkey) to activate widget event

Open kopperud opened this issue 6 years ago • 5 comments

Hello, I have a question. Let's say I create a simple widget in Jupyter, like the button from the documentation.

from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)

def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)

Now, I want this button to activate whenever I press a certain hotkey on the keyboard, let's say one of the arrow keys. Is there any possibility of doing this? If not, are there any alternative approaches? Packages or documentation I have missed?

Best regards, Bjørn

kopperud avatar Jan 18 '19 14:01 kopperud

The only jupyter widget I know of that has been used to assign arbitrary hotkeys from the user is the https://github.com/mwcraig/ipyevents project.

jasongrout avatar Jan 18 '19 16:01 jasongrout

ipyevents looks great, but unfortunately has this limitation: "The keyboard events are triggered only if the mouse is over the widget."

Is there another work around this issue? It would be great to have a hotkey option to trigger callbacks when defining on_click, observe, etc...

luiztauffer avatar Jun 16 '20 05:06 luiztauffer

Its been several years, just wondering if this was ever worked out?

aabdullah-getguru avatar Apr 20 '23 11:04 aabdullah-getguru

I wonder if it makes sense to make https://github.com/fgr-araujo/vue-shortkey available as a ipywidget (cc @mariobuikhuizen )

That wouldn't make it work with ipywidgets controls (the widgets that ipywidgets ships), but it could work with ipyvuetify.

maartenbreddels avatar Apr 21 '23 10:04 maartenbreddels

This would be very useful, in particular for https://github.com/voila-dashboards/voila.

E.g., one might want to bind Ctrl+Enter to "execute" (I am aware that this hotkey is usually used by Jupyter).

As a workaround, I have been experimenting with display(HTML()) to inject custom JavaScript.

kno10 avatar Apr 21 '24 15:04 kno10