在Android中使用kivy请求击键后如何隐藏软键盘?

2024-10-01 11:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用kivy网站的例子为Android创建了一个监听击键字符串的应用程序。它工作得很好,但当我将它与扫描仪和电子秤一起使用时,弹出的软键盘就变得不必要了。

我尝试了所有不同的配置设置,还下载了应用程序空键盘。

更新: 有趣的是,一旦键盘被移除,应用程序就不再接受输入了。为什么会这样?你知道吗

代码:
    def _get_the_keyboard(self):
        self._keyboard = Window.request_keyboard(
            self._keyboard_closed, self, 'text')
        if self._keyboard.widget:
            # If it exists, this widget is a VKeyboard object which you can use
            # to change the keyboard layout.
            pass
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        if self._keyboard != None:
            self._keyboard.unbind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
         # Keycode is composed of an integer + a string
        # If we hit escape, release the keyboard
        if keycode[1] == 'escape':
            keyboard.release()
        elif (keycode[1] == 'shift' or keycode[1] == 'rshift') and text != None and keycode[0] != 44 and keycode[0] != 46:
            self.is_shift = True
        elif self.is_shift and (keycode[1] != 'shift' or keycode[1] != 'rshift'):
            print(keycode[1])
            self.is_shift = False
            if keycode[0] == 46:
                self.key_input = self.key_input + str(chr(62)).upper()
            if keycode[0] == 44:
                self.key_input = self.key_input + str(chr(60)).upper()
            if keycode[0] == 92:
                self.key_input = self.key_input + str(chr(124)).upper()
        elif (keycode[0] > 47 and keycode[0] < 58) or (keycode[0] > 96 or keycode[0] < 123) and keycode[1] != 'enter'\
                and text != None and keycode[1] != 'shift' and keycode[1] != 'rshift' and not self.is_shift and keycode[0] != 44 and keycode[0] != 46 and keycode[0] != 301:
            self.key_input = self.key_input + str(text).upper()
        elif keycode[1] == 'enter':
            plyer.notification.notify(title="Capture Data", message=str(the_key), app_name=self.the.name,
                                              timeout=2,
                                              ticker='New Incoming Notification', toast=True)
            if self.scrn_name == 'firstscanpage':
                if self.ids.emp_first_scan_key.text == "":
                    self.scan_weight_ticker()
                    self.ids.emp_first_scan_key.text = self.key_input
                    self.i = 0

如何在请求击键时禁用或隐藏软键盘on_key_down,而不删除击键侦听器? 每次击键后将键盘绑定到不同的小部件会有帮助吗

解决方法: 空键盘应用程序解决了这个问题。在设置中激活空键盘后,您必须按“空格键”并选择它。再也没有软键盘了!当然这不是最好的解决办法。你知道吗


Tags: andthekeytextselfinputifshift