A custom widget for returning mouse and keyboard events to Python
ipyevents provides a custom widget for capturing and returning mouse and keyboard events from Jupyter widgets to Python. It enables adding interactive features like keyboard shortcuts, reacting to clicks on images, and handling arbitrary mouse/keyboard events within a Jupyter environment. The current version is 2.0.4, targeting JupyterLab 3 and above. Releases are driven by compatibility with JupyterLab versions and feature additions.
Warnings
- breaking ipyevents versions 2.0.0 and higher are only built for JupyterLab 3 and up. If you are using JupyterLab 2, you must use ipyevents version 0.9.0 or earlier.
- gotcha When watching keyboard events, ipyevents will grab the focus of the browser on the source widget. This prevents key presses from being passed to the notebook's default handlers (e.g., for cell navigation or commands).
- gotcha High-frequency events like 'mousemove' or 'wheel' can generate a large volume of messages between the frontend and backend, potentially leading to performance issues.
- gotcha The `prevent_default_action` property may not function correctly for 'wheel' events, meaning the browser's default scroll behavior might still occur.
Install
-
pip install ipyevents
Imports
- Event
from ipyevents import Event
Quickstart
from ipywidgets import Label, HTML, display
from ipyevents import Event
l = Label('Click or type on me!')
l.layout.border = '2px solid red'
h = HTML('Event info')
d = Event(source=l, watched_events=['click', 'keydown', 'mouseenter'])
def handle_event(event):
lines = [f'{k}: {v}' for k, v in event.items()]
content = '<br>'.join(lines)
h.value = content
d.on_dom_event(handle_event)
display(l, h)