Explorar o código

Merge branch 'main' of github.com:zauberzeug/nicegui

Rodja Trappe %!s(int64=2) %!d(string=hai) anos
pai
achega
e18a1094be
Modificáronse 2 ficheiros con 14 adicións e 6 borrados
  1. 2 2
      nicegui/elements/keyboard.js
  2. 12 4
      nicegui/elements/keyboard.py

+ 2 - 2
nicegui/elements/keyboard.js

@@ -3,9 +3,8 @@ export default {
     for (const event of this.events) {
       document.addEventListener(event, (evt) => {
         // https://stackoverflow.com/a/36469636/3419103
-        const ignored = ["input", "select", "button", "textarea"];
         const focus = document.activeElement;
-        if (focus && ignored.includes(focus.tagName.toLowerCase())) return;
+        if (focus && this.ignore.includes(focus.tagName.toLowerCase())) return;
         if (evt.repeat && !this.repeating) return;
         this.$emit("key", {
           action: event,
@@ -25,5 +24,6 @@ export default {
   props: {
     events: Array,
     repeating: Boolean,
+    ignore: Array,
   },
 };

+ 12 - 4
nicegui/elements/keyboard.py

@@ -1,4 +1,6 @@
-from typing import Callable, Dict
+from typing import Callable, Dict, List
+
+from typing_extensions import Literal
 
 from ..binding import BindableProperty
 from ..dependencies import register_component
@@ -11,21 +13,27 @@ register_component('keyboard', __file__, 'keyboard.js')
 class Keyboard(Element):
     active = BindableProperty()
 
-    def __init__(self, on_key: Callable, *, active: bool = True, repeating: bool = True) -> None:
-        """
-        Keyboard
+    def __init__(self,
+                 on_key: Callable, *,
+                 active: bool = True,
+                 repeating: bool = True,
+                 ignore: List[Literal['input', 'select', 'button', 'textarea']] = ['input', 'select', 'button', 'textarea'],
+                 ) -> None:
+        """Keyboard
 
         Adds global keyboard event tracking.
 
         :param on_key: callback to be executed when keyboard events occur.
         :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`)
+        :param ignore: ignore keys when one of these element types is focussed (default: `['input', 'select', 'button', 'textarea']`)
         """
         super().__init__('keyboard')
         self.key_handler = on_key
         self.active = active
         self._props['events'] = ['keydown', 'keyup']
         self._props['repeating'] = repeating
+        self._props['ignore'] = ignore
         self.on('key', self.handle_key)
 
     def handle_key(self, msg: Dict) -> None: