【问题标题】:Block Keyboard but get global Hotkeys XLib阻止键盘但获取全局热键 XLib
【发布时间】:2011-05-17 11:11:51
【问题描述】:

[已解决 - 提供的示例包含答案!] 我正在尝试实现一个以全屏模式启动并且不允许任何用户输入(无论是鼠标还是键盘)的程序,因为它只会在插入 USB 棒或 cd 时对 UDEV 信号作出反应。 我想防止用户放入键盘/鼠标并在工作站上做一些奇怪的事情。 唯一需要键盘的情况是当管理员插入键盘并按下 CTRL-T 时,我的程序会打开一个终端窗口。

我使用 Debian (Squeeze) + Gnome-Desktop-Environment。

我尝试使用 XLib,它可以很好地抓取所有键盘事件,但是当我的程序打开我的终端时,我无法解开我的键盘(这也很完美),所以用户不能使用 shell,除非键盘 -抓取已关闭。

这里有一些关键的代码片段:

class ScanWSClient(gtk.Window):

   def __init__(self, url):
         # current display
         disp = Display()
         self.display = disp

         gtk.Window.__init__(self)           

         self.terminal_window = None
         self.kb_handler = KeyboardHandler(self).start()

         #self.fullscreen()

         self._browser= webkit.WebView()
         self.add(self._browser)
         self.connect('destroy', gtk.main_quit)

         self._browser.open(url)
         self.show_all()

class KeyboardHandler(threading.Thread):
   def __init__(self, scanws_client):
         super(KeyboardHandler,self).__init__()
         self.running = True
         self.daemon = True          
         self.terminal_window = None
         self.scanws_client = scanws_client #

   def run(self):
         root = self.scanws_client.display.screen().root


         while self.running:
                event = root.display.next_event()
                self.handle_event(event)
                time.sleep(1)

   def handle_event(self,aEvent):
                keycode = aEvent.detail
                state = aEvent.state
                key_type = aEvent.type

                if keycode == 28 and key_type == X.KeyPress:
                      if self.scanws_client.terminal_window == None:
                            self.scanws_client.terminal_window = TerminalWindow(self.scanws_client, "Administrative Shell started...Type  *exit* to return to the locked workstation")
                      else:
                            self.scanws_client.terminal_window.present()
                            self.scanws_client.display.flush() #THIS FLUSH IS NECESSARY TO UNGRAB THE KEYBOARD LATER ON!!!
                      self.scanws_client.display.ungrab_keyboard(1, X.CurrentTime) 
                      print "Key: %s / Mask: %s / Type: %s" % (keycode, state, key_type)

                print self.scanws_client.terminal_window

在我的线程化 KeyboardHandler 中,我获取所有 xlib 排队的事件并使用我的函数“handle_event”检查是否按下了 CTRL-T。如果是这样,我打开我的终端并松开我的键盘(不起作用):

self.scanws_client.display.ungrab_keyboard(1, X.CurrentTime)

谁能告诉我为什么我无法解开我的傻键盘? (这个问题是 cookie 提供的;))

【问题讨论】:

    标签: python xlib


    【解决方案1】:

    您是否刷新了对 X 服务器的调用? Xlib 是异步的,会缓冲所有请求,直到您填满缓冲区、进行需要服务器响应的调用或调用 XFlush(C API 接口,不确定 python 绑定名称)。

    【讨论】:

    • 完美运行。但是在 GrabModeAsync 中,我的 CTRL-T 输入和终端弹出窗口之间有一点延迟......但我认为这是硬件事件和 xlib 处理之间的文档中提到的保留......我更新了我的例子,所以每个人都知道如何解决它..谢谢
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多