Hotkey binding working

This commit is contained in:
Michael Webster 2013-03-09 19:43:27 -05:00
parent c6f19e56bd
commit 58ee643264
2 changed files with 21 additions and 25 deletions

View File

@ -35,9 +35,15 @@ import capi
gdk = CDLL("libgdk-x11-2.0.so.0") gdk = CDLL("libgdk-x11-2.0.so.0")
class GlobalKeyBinding(): class GlobalKeyBinding(GObject.GObject, threading.Thread):
__gsignals__ = {
'activate': (GObject.SignalFlags.RUN_LAST, None, ()),
}
def __init__(self): def __init__(self):
GObject.GObject.__init__ (self)
threading.Thread.__init__ (self)
self.setDaemon (True)
self.keymap = capi.get_widget (gdk.gdk_keymap_get_default()) self.keymap = capi.get_widget (gdk.gdk_keymap_get_default())
self.display = Display() self.display = Display()
@ -58,24 +64,21 @@ class GlobalKeyBinding():
def grab(self, key): def grab(self, key):
accelerator = key accelerator = key
keyval, modifiers = Gtk.accelerator_parse(accelerator) keyval, modifiers = Gtk.accelerator_parse(accelerator)
print keyval, modifiers if not accelerator or (not keyval and not modifiers):
# if not accelerator or (not keyval and not modifiers): self.keycode = None
# self.keycode = None self.modifiers = None
# self.modifiers = None return False
# print "what"
# return
count = c_int() count = c_int()
array = (KeymapKey * 10)() array = (KeymapKey * 10)()
keys = cast(array, POINTER(KeymapKey)) keys = cast(array, POINTER(KeymapKey))
self.keycode = gdk.gdk_keymap_get_entries_for_keyval(hash(self.keymap), keyval, byref(keys), byref(count)) gdk.gdk_keymap_get_entries_for_keyval(hash(self.keymap), keyval, byref(keys), byref(count))
#self.keycode= self.keymap.get_entries_for_keyval(keyval)[0].keycode self.keycode = keys[0].keycode
print keys[0].keycode
self.modifiers = int(modifiers) self.modifiers = int(modifiers)
catch = error.CatchError(error.BadAccess) catch = error.CatchError(error.BadAccess)
for ignored_mask in self.ignored_masks: for ignored_mask in self.ignored_masks:
mod = modifiers | ignored_mask mod = modifiers | ignored_mask
result = self.root.grab_key(keys[0].keycode, mod, True, X.GrabModeAsync, X.GrabModeSync, onerror=catch) result = self.root.grab_key(self.keycode, mod, True, X.GrabModeAsync, X.GrabModeSync, onerror=catch)
self.display.sync() self.display.sync()
if catch.get_error(): if catch.get_error():
return False return False
@ -89,7 +92,6 @@ class GlobalKeyBinding():
return [x for x in xrange(mask+1) if not (x & ~mask)] return [x for x in xrange(mask+1) if not (x & ~mask)]
def idle(self): def idle(self):
print "caught"
self.emit("activate") self.emit("activate")
return False return False
@ -100,7 +102,6 @@ class GlobalKeyBinding():
self.running = True self.running = True
wait_for_release = False wait_for_release = False
while self.running: while self.running:
print "running"
event = self.display.next_event() event = self.display.next_event()
self.current_event_time = event.time self.current_event_time = event.time
if event.detail == self.keycode and event.type == X.KeyPress and not wait_for_release: if event.detail == self.keycode and event.type == X.KeyPress and not wait_for_release:
@ -113,12 +114,11 @@ class GlobalKeyBinding():
elif event.detail == self.keycode and wait_for_release: elif event.detail == self.keycode and wait_for_release:
if event.type == X.KeyRelease: if event.type == X.KeyRelease:
wait_for_release = False wait_for_release = False
GObject.idle_add(self.idle) GLib.idle_add(self.idle)
self.display.allow_events(X.AsyncKeyboard, event.time) self.display.allow_events(X.AsyncKeyboard, event.time)
else: else:
self.display.allow_events(X.ReplayKeyboard, event.time) self.display.allow_events(X.ReplayKeyboard, event.time)
def stop(self): def stop(self):
self.running = False self.running = False
self.ungrab() self.ungrab()

View File

@ -25,12 +25,8 @@ except Exception, e:
print e print e
sys.exit( 1 ) sys.exit( 1 )
from keybinding import GlobalKeyBinding
GObject.threads_init() GObject.threads_init()
keybinder = GlobalKeyBinding()
gtk = CDLL("libgtk-x11-2.0.so.0") gtk = CDLL("libgtk-x11-2.0.so.0")
gdk = CDLL("libgdk-x11-2.0.so.0") gdk = CDLL("libgdk-x11-2.0.so.0")
@ -549,10 +545,9 @@ class MenuWin( object ):
self.bind_hot_key() self.bind_hot_key()
def onBindingPress(self): def onBindingPress(self, binder):
print "press"
try: try:
if self.mainwin.window.flags() & Gtk.VISIBLE: if self.mainwin.window.get_visible():
self.mainwin.window.hide() self.mainwin.window.hide()
self.mainwin.toggle.set_active(False) self.mainwin.toggle.set_active(False)
else: else:
@ -694,10 +689,11 @@ class MenuWin( object ):
self.sizeButton() self.sizeButton()
def bind_hot_key (self): def bind_hot_key (self):
return
try: try:
keybinder.grab( self.hotkeyText ) self.keybinder = keybinding.GlobalKeyBinding()
keybinder.connect("activate", self.onBindingPress) self.keybinder.grab( self.hotkeyText )
self.keybinder.connect("activate", self.onBindingPress)
self.keybinder.start()
# Binding menu to hotkey # Binding menu to hotkey
print "Binding to Hot Key: " + self.hotkeyText print "Binding to Hot Key: " + self.hotkeyText