久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <i id='eC4h9'><tr id='eC4h9'><dt id='eC4h9'><q id='eC4h9'><span id='eC4h9'><b id='eC4h9'><form id='eC4h9'><ins id='eC4h9'></ins><ul id='eC4h9'></ul><sub id='eC4h9'></sub></form><legend id='eC4h9'></legend><bdo id='eC4h9'><pre id='eC4h9'><center id='eC4h9'></center></pre></bdo></b><th id='eC4h9'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='eC4h9'><tfoot id='eC4h9'></tfoot><dl id='eC4h9'><fieldset id='eC4h9'></fieldset></dl></div>

      • <bdo id='eC4h9'></bdo><ul id='eC4h9'></ul>
      <tfoot id='eC4h9'></tfoot>
    1. <small id='eC4h9'></small><noframes id='eC4h9'>

      <legend id='eC4h9'><style id='eC4h9'><dir id='eC4h9'><q id='eC4h9'></q></dir></style></legend>

      在 PyQt5 中檢測外部鍵盤事件

      Detect external keyboard events in PyQt5(在 PyQt5 中檢測外部鍵盤事件)
          <tfoot id='qOTCs'></tfoot>
          • <legend id='qOTCs'><style id='qOTCs'><dir id='qOTCs'><q id='qOTCs'></q></dir></style></legend>
              <tbody id='qOTCs'></tbody>
            • <bdo id='qOTCs'></bdo><ul id='qOTCs'></ul>

              <small id='qOTCs'></small><noframes id='qOTCs'>

              1. <i id='qOTCs'><tr id='qOTCs'><dt id='qOTCs'><q id='qOTCs'><span id='qOTCs'><b id='qOTCs'><form id='qOTCs'><ins id='qOTCs'></ins><ul id='qOTCs'></ul><sub id='qOTCs'></sub></form><legend id='qOTCs'></legend><bdo id='qOTCs'><pre id='qOTCs'><center id='qOTCs'></center></pre></bdo></b><th id='qOTCs'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qOTCs'><tfoot id='qOTCs'></tfoot><dl id='qOTCs'><fieldset id='qOTCs'></fieldset></dl></div>
                本文介紹了在 PyQt5 中檢測外部鍵盤事件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                如何在 PyQT5 中實現(xiàn)關(guān)鍵監(jiān)聽器?即使應(yīng)用在后臺,我也想檢測按鍵.

                How can I implement a key listener in PyQT5? I want to detect keypresses even when the app is in background.

                from PyQt5 import QtGui
                from PyQt5.QtWidgets import *
                from PyQt5.QtCore import Qt
                import sys
                
                
                class Window(QWidget):
                    
                    ...
                       
                
                    def keyPressEvent(self, e): # doesnt work when app is in background
                        if e.key() == Qt.Key_F3:
                            print(1)
                        elif e.key() == Qt.Key_F4:
                            print(0)
                
                   ...
                
                        
                App = QApplication(sys.argv)
                App.setStyle('Fusion')
                window = Window()
                sys.exit(App.exec())
                
                
                

                推薦答案

                Qt 只有在其頂層窗口有鍵盤焦點時才能訪問鍵盤事件.如果窗口被最小化或另一個窗口獲得焦點,您將不會收到鍵盤事件.

                Qt can access keyboard events only if any of its top level window has keyboard focus. If the window is minimized or another window takes focus, you will not receive keyboard events.

                唯一的解決方案是使用外部庫,但它們有局限性.

                The only solution is to use an external library, but they have limitations.

                keyboard 模塊似乎不支持 macOS,而 pyinput 可以,但需要該操作系統(tǒng)的 root 訪問權(quán)限.我不知道有任何其他方法可以不受限制地支持所有三個平臺.

                The keyboard module does not seem to support macOS, while pyinput does, but requires root access for that OS. I don't know of any other ways that support all three platforms without limitations.

                在任何情況下,您都不應(yīng)該依賴于對當前按鍵的定時檢查,因為您最終肯定會錯過一些事件.
                雖然通常會使用一個單獨的線程來實現(xiàn)事件偵聽器(通常是阻塞的),但幸運的是在這兩種情況下都有非阻塞系統(tǒng)來調(diào)用回調(diào)函數(shù)(因此您實際上不需要單獨的線程).

                In any case, you should not rely on timed checking of the currently pressed keys, because you'll certainly end up missing some events.
                While normally one would use a separate thread that implements the event listener (which are normally blocking), luckily in both cases there are non blocking systems to call callback functions (so you don't actually need a separate thread).

                以下是使用 keyboard 模塊的基本示例:

                The following is a basic example using the keyboard module:

                from PyQt5 import QtCore, QtWidgets
                import keyboard
                
                class KeyGrabber(QtWidgets.QWidget):
                    def __init__(self):
                        super().__init__()
                        layout = QtWidgets.QVBoxLayout(self)
                        self.button = QtWidgets.QPushButton('start')
                        layout.addWidget(self.button)
                        self.button.setCheckable(True)
                        self.button.toggled.connect(self.setGrabbing)
                
                    def keyboardEventReceived(self, event):
                        if event.event_type == 'down':
                            if event.name == 'f3':
                                print('F3 pressed')
                            elif event.name == 'f4':
                                print('F4 pressed')
                
                    def setGrabbing(self, enable):
                        if enable:
                            self.button.setText('stop')
                            # on_press returns a hook that can be used to "disconnect" the callback
                            # function later, if required
                            self.hook = keyboard.on_press(self.keyboardEventReceived)
                            self.showMinimized()
                        else:
                            self.button.setText('start')
                            keyboard.unhook(self.hook)
                

                這篇關(guān)于在 PyQt5 中檢測外部鍵盤事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設(shè)置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應(yīng)該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  <legend id='vXTMK'><style id='vXTMK'><dir id='vXTMK'><q id='vXTMK'></q></dir></style></legend>

                    <tbody id='vXTMK'></tbody>
                    • <bdo id='vXTMK'></bdo><ul id='vXTMK'></ul>

                      <small id='vXTMK'></small><noframes id='vXTMK'>

                      <tfoot id='vXTMK'></tfoot>

                        <i id='vXTMK'><tr id='vXTMK'><dt id='vXTMK'><q id='vXTMK'><span id='vXTMK'><b id='vXTMK'><form id='vXTMK'><ins id='vXTMK'></ins><ul id='vXTMK'></ul><sub id='vXTMK'></sub></form><legend id='vXTMK'></legend><bdo id='vXTMK'><pre id='vXTMK'><center id='vXTMK'></center></pre></bdo></b><th id='vXTMK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vXTMK'><tfoot id='vXTMK'></tfoot><dl id='vXTMK'><fieldset id='vXTMK'></fieldset></dl></div>

                        • 主站蜘蛛池模板: 国产ts人妖系列高潮 | 亚洲国产小视频 | 国产精品成人一区二区 | 国产精品久久国产精品 | 国产在线不卡 | 99re在线视频| 一区在线免费视频 | 香蕉91| 二区成人 | 亚洲精品成人 | 一区二区精品 | 一区二区三区在线 | 欧美在线一区二区三区 | 日韩欧美国产一区二区三区 | 国产精品美女久久久久久久久久久 | 看一级毛片视频 | 免费一级毛片 | 久久精彩 | 亚洲一区 | 亚洲精品久久久久久首妖 | 黄色综合 | 国产91丝袜在线播放 | 久久亚洲一区 | 成年人在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 色呦呦在线 | 久久福利电影 | 亚洲一区二区视频 | 中文字幕一区二区三区乱码在线 | 中文字幕av一区二区三区 | 另类专区成人 | 超级乱淫av片免费播放 | 亚洲国产成人精品久久 | 亚洲人成人一区二区在线观看 | 一级国产精品一级国产精品片 | 天天干精品 | 国产精品综合视频 | 日日天天 | 91精品国产综合久久久动漫日韩 | 欧美性猛交一区二区三区精品 | 中文字幕精品一区久久久久 |