Browse Source

allow disabling repeating keyboard events

Falko Schindler 3 years ago
parent
commit
a49c75cb80
3 changed files with 10 additions and 5 deletions
  1. 2 0
      main.py
  2. 2 1
      nicegui/elements/keyboard.js
  3. 6 4
      nicegui/elements/keyboard.py

+ 2 - 0
main.py

@@ -408,6 +408,8 @@ with example(get_decorator):
     ui.link('Try yet another route!', '/another/route/1')
 
 with example(ui.keyboard):
+    from nicegui.events import KeyEventArguments
+
     def handle_key(e: KeyEventArguments):
         if e.key == 'f' and not e.action.repeat:
             if e.action.keyup:

+ 2 - 1
nicegui/elements/keyboard.js

@@ -1,7 +1,7 @@
 Vue.component("keyboard", {
   template: `<span v-bind:id="jp_props.id" :class="jp_props.classes" :style="jp_props.style"></span>`,
   mounted() {
-    for (const event of this.$props.jp_props.options.activeJSEvents) {
+    for (const event of this.$props.jp_props.options.active_js_events) {
       document.addEventListener(event, (evt) => {
         // https://stackoverflow.com/a/36469636/3419103
         const ignored = ["input", "select", "button", "textarea"];
@@ -15,6 +15,7 @@ Vue.component("keyboard", {
           websocket_id: websocket_id,
         };
         if (evt instanceof KeyboardEvent) {
+          if (evt.repeat && !this.$props.jp_props.options.repeating) return;
           // https://developer.mozilla.org/en-US/docs/Web/Events/keydown   keyup, keypress
           e["key_data"] = {
             action: event,

+ 6 - 4
nicegui/elements/keyboard.py

@@ -7,8 +7,8 @@ from .element import Element
 
 class KeyboardView(CustomView):
 
-    def __init__(self, on_key: Callable):
-        super().__init__('keyboard', __file__, activeJSEvents=['keydown', 'keyup', 'keypress'])
+    def __init__(self, on_key: Callable, repeating: bool):
+        super().__init__('keyboard', __file__, active_js_events=['keydown', 'keyup', 'keypress'], repeating=repeating)
         self.allowed_events = ['keyboardEvent']
         self.style = 'display: none'
         self.initialize(temp=False, on_keyboardEvent=on_key)
@@ -20,6 +20,7 @@ class Keyboard(Element):
                  *,
                  on_key: Optional[Union[Callable, Awaitable]] = None,
                  active: bool = True,
+                 repeating: bool = True,
                  ):
         """
         Keyboard
@@ -27,9 +28,10 @@ class Keyboard(Element):
         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
+        :param active: boolean flag indicating whether the callback should be executed or not (default: True)
+        :param repeating: boolean flag indicating whether held keys should be sent repeatedly (default: True)
         """
-        super().__init__(KeyboardView(on_key=self.handle_key))
+        super().__init__(KeyboardView(on_key=self.handle_key, repeating=repeating))
         self.active = active
         self.key_handler = on_key