keyboard_documentation.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class KeyboardDocumentation(UiElementDocumentation, element=ui.keyboard):
  4. def main_demo(self) -> None:
  5. from nicegui.events import KeyEventArguments
  6. def handle_key(e: KeyEventArguments):
  7. if e.key == 'f' and not e.action.repeat:
  8. if e.action.keyup:
  9. ui.notify('f was just released')
  10. elif e.action.keydown:
  11. ui.notify('f was just pressed')
  12. if e.modifiers.shift and e.action.keydown:
  13. if e.key.arrow_left:
  14. ui.notify('going left')
  15. elif e.key.arrow_right:
  16. ui.notify('going right')
  17. elif e.key.arrow_up:
  18. ui.notify('going up')
  19. elif e.key.arrow_down:
  20. ui.notify('going down')
  21. keyboard = ui.keyboard(on_key=handle_key)
  22. ui.label('Key events can be caught globally by using the keyboard element.')
  23. ui.checkbox('Track key events').bind_value_to(keyboard, 'active')