[已解决 - 提供的示例包含答案!] 我正在尝试实现一个以全屏模式启动并且不允许任何用户输入(无论是鼠标还是键盘)的程序,因为它只会在插入 USB 棒或 CD 时对 UDEV 信号使用react。 我想防止用户放入键盘/鼠标并在工作站上做一些奇怪的事情。 唯一需要键盘的情况是,当管理员插入键盘并按下 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 中,如果按下了 CTRL-T,我会获取所有 xlib 排队的事件并检查我的函数“handle_event”。如果是这样,我打开我的终端并取消抓取我的键盘(不起作用):
self.scanws_client.display.ungrab_keyboard(1, X.CurrentTime)
谁能告诉我为什么我不能松开我那愚蠢的键盘? (这个问题是 cookie 提供的;))
最佳答案
您是否刷新了对 X 服务器的调用? Xlib 是异步的并缓冲所有请求,直到您填充缓冲区、进行需要服务器响应的调用或调用 XFlush(C API 接口(interface),不确定 python 绑定(bind)名称)。
关于python - 阻止键盘但获得全局热键 XLib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029949/