Skip to content

Commit

Permalink
adds keyboard activation flag
Browse files Browse the repository at this point in the history
  • Loading branch information
c00lc0de committed Oct 29, 2021
1 parent 3deb87f commit 50c5a90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,6 @@ def handle_keys(e):

keyboard = ui.keyboard(handle_keys)
ui.label('Key events can be caught globally by using the keyboard element.')
ui.checkbox('Track key events').bind_value_to(keyboard.view, 'active')

ui.run(port=8080)
16 changes: 11 additions & 5 deletions nicegui/elements/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@

class KeyboardView(CustomView):

def __init__(self, handle_keys: Callable):
def __init__(self, handle_keys: Callable, active: bool = True):
super().__init__('keyboard', __file__, activeJSEvents=['keydown', 'keyup', 'keypress'])
self.allowed_events = ['keyboardEvent']
self.style = 'display: none'
self.initialize(temp=False, on_keyboardEvent=handle_exceptions(provide_arguments(handle_keys, 'key_data', 'event_type')))
self.active = active

def execute_when_active(*args):
if self.active:
handle_exceptions(provide_arguments(handle_keys, 'key_data', 'event_type'))(*args)

self.initialize(temp=False, on_keyboardEvent=execute_when_active)


class Keyboard(Element):

def __init__(self, handle_keys: Callable = None):
def __init__(self, handle_keys: Callable = None, active: bool = True):
"""
Keyboard
Adds global keyboard event tracking.
:param handle_keys: callback to be executed when keyboard events occur.
:param active: boolean flag indicating whether the callback should be executed or not
"""

super().__init__(KeyboardView(handle_keys=handle_keys))
super().__init__(KeyboardView(handle_keys=handle_keys, active=active))

0 comments on commit 50c5a90

Please sign in to comment.