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.
Perhaps a hidden input field with autofocus on ?
You would have several callbacks listening to this input and executing code accordingly.
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 Keyboard component in dash-extensions,
import dash
import dash_html_components as html
import json
from dash.dependencies import Output, Input
from dash_extensions import Keyboard
app = dash.Dash()
app.layout = html.Div([Keyboard(id="keyboard"), html.Div(id="output")])
@app.callback(Output("output", "children"), [Input("keyboard", "keydown")])
def keydown(event):
return json.dumps(event)
if __name__ == '__main__':
app.run_server()
@emilhe Thanks for pointing out dash-extensions - lots of good stuff there! Some overlap with functionality we're already working on (eg flattened callback args https://github.com/plotly/dash/pull/1180) and other things we haven't started on but have discussed. In particular Trigger https://github.com/plotly/dash/issues/1054 is something we're very interested in adding to Dash itself, and Keyboard would be a great addition to dash-core-components. You're of course welcome to continue developing these features separately under dash-extensions but if you're interested to PR the more generally-applicable features into Dash or dash-core-components directly, we'd be glad to help 馃槑
@alexcjohnson Thanks! As the components mature, i think it would be good (in particular for new users), if they were moved to dash-core-components. The Trigger component would probably be a good place to start (it is pretty simple), but i feel (based on community feedback) that the functionality provided by the Download and ServersideOutput components are even more needed. When i find some time, i'll take a look at the contribution guidelines :)
Most helpful comment
You could try the
Keyboardcomponent in dash-extensions,https://pypi.org/project/dash-extensions/