-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Keypress callback #723
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
Comments
Perhaps a hidden input field with autofocus on ? I guess you would have to add an extra callback dedicated to clearing the input field everytime it is filled with something as well. |
Seems pretty hacky this way. Wont the input field lose focus as soon as the user starts interacting with other inputs? |
You are right, pretty hacky... |
Oh, I'm a bit surprised. Is there no way one can create callbacks for keyboard events? That seems like a basic interaction pattern. |
You could try the
|
@emilhe Thanks for pointing out |
@alexcjohnson Thanks! As the components mature, i think it would be good (in particular for new users), if they were moved to |
Hmm looks like the >>> from dash_extensions import Keyboard
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Keyboard' from 'dash_extensions' (/Users/layne/.pyenv/versions/aiqc_dev/lib/python3.7/site-packages/dash_extensions/__init__.py) |
Related: |
The https://www.dash-extensions.com/pages/components/event-listener |
Feeling the same. Capturing the keyboard event should have been much easier. I am writing a local application with dash because of the good support of Cytoscape. But maybe it would have been much easier if I simply used QT. |
I've succeeded in applying the from dash import dcc, html, callback, Input, Output, State, ctx, no_update
from dash.exceptions import PreventUpdate
import dash_mantine_components as dmc
from dash_iconify import DashIconify
from dash_extensions import EventListener
# CAPTURE ONLY KEY PRESSES
events = [{'event': 'keydown', 'props': ['key']}]
# THE INPUTS
input_login = EventListener(dmc.TextInput(label='', id='login__input_login', placeholder='Username', radius='lg', icon=[DashIconify(icon='bxs:user', width=20)],
size='md', style={'margin': '0% 1%'}), id='login__input_login_events', events=events)
input_password = EventListener(dmc.PasswordInput(label='', id='login__input_password', placeholder='Password', radius='lg', icon=[DashIconify(icon='bxs:lock-alt', width=20)],
size='md', style={'margin': '0% 1%'}), id='login__input_password_events', events=events)
#======================== MAIN LAYOUT ========================#
def layout():
# there are also other components here, but I've cut them away for clarity...
return html.Div([dmc.LoadingOverlay(dmc.Stack([input_login, input_password],
justify='center', align='stretch', spacing='lg'))],
style={'margin': '10% 30%'})
#======================== CALLBACKS ========================#
@callback(
output={
'hi_user_value': Output('login__hi_user', 'children'),
'alert_err_value': Output('login__alert_error', 'children'),
'alert_err_hidden': Output('login__alert_error', 'hide'),
'div_admin_action_hidden': Output('login__div_admin_actions', 'hidden'),
'div_logged_in_hidden': Output('login__div_logged_in', 'hidden'),
'div_login_hidden': Output('login__div_login', 'hidden')
},
inputs=[
Input('login__input_login_events', 'n_events'), # login input keydown event count
Input('login__input_password_events', 'n_events'), # password input keydown event count
State('login__input_login', 'value'), # login value
State('login__input_password', 'value'), # password value
State('login__input_login_events', 'event'), # login input keydown event
State('login__input_password_events', 'event') # password input keydown event
]
)
def update_layout(login_nevents, password_nevents, in_login, in_password, login_event, password_event):
# get trigger source, return if none
fired_id = ctx.triggered_id
if not fired_id:
raise PreventUpdate
# block updating if any other key was hit BUT the Enter key on either of the two inputs
if (fired_id == 'login__input_password_events' and (not password_nevents or password_event.get('key', '') != 'Enter')) or \
(fired_id == 'login__input_login_events' and (not login_nevents or login_event.get('key', '') != 'Enter')):
raise PreventUpdate
# Enter was pressed on login input
login_return = fired_id == 'login__input_login_events' and login_nevents
# Enter was pressed on password input
passw_return = fired_id == 'login__input_password_events' and password_nevents
if login_return or passw_return:
# do login stuff ...
raise PreventUpdate BTW there's no need to wrap your dash |
@hannahker, how did PS do this for the LBL project? |
The
Then you can attach callbacks using |
Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are most important to our community. If this issue is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. (Please note that we will give priority to reports that include a short reproducible example.) If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson |
I would like to be able to capture keypresses and use them for callbacks. This could be used to make keyboard shortcuts in place of buttons.
The text was updated successfully, but these errors were encountered: