Class | GlobalHotKeys::KeyBinder |
In: |
globalhotkeys.c
|
Parent: | Object |
rghk provides a simple way to set global hotkeys.
Example:
kb=GlobalHotKeys::KeyBinder.new kb.bind(Gdk::Keyval::GDK_a, GlobalHotKeys::Modifier::CONTROL_MASK){puts 'Binded'} kb.unbind
It probably a good idea to require ‘gtk2’ before ‘rghk’. The librairy rghk was not tested without ruby/gtk2.
Unset a all global hotkeys.
/* * Unset a all global hotkeys. */ VALUE kb_unbind_all(VALUE self) { rb_iterate(rb_each, rb_iv_get(cKeyBinder, "stock"), kb_unbind, Qnil); return Qnil; }
key: a key value. Gdk::Keyval constants can be used.
modifier: a GlobalHotKeys::Modifier.
Set a global hotkey. block will be called when the key and the modifier are hitted.
/* * call-seq: bind(key, modifier) {block} * * key: a key value. Gdk::Keyval constants can be used. * * modifier: a GlobalHotKeys::Modifier. * * Set a global hotkey. * block will be called when the key and the modifier are hitted. */ VALUE kb_bind(VALUE self, VALUE key, VALUE modifier) { GdkWindow *rootwin; Display *xdisplay; uint keycode; /* uint ignored[]={0, RGHK_LOCK_MASK, RGHK_NUM_LOCK_MASK, RGHK_SCROLL_LOCK, RGHK_LOCK_MASK|RGHK_NUM_LOCK_MASK, RGHK_LOCK_MASK|RGHK_SCROLL_LOCK, RGHK_NUM_LOCK_MASK|RGHK_SCROLL_LOCK, RGHK_LOCK_MASK|RGHK_NUM_LOCK_MASK|RGHK_SCROLL_LOCK};*/ uint ignored[]={0, RGHK_LOCK_MASK, RGHK_NUM_LOCK_MASK, RGHK_LOCK_MASK|RGHK_NUM_LOCK_MASK}; uint mod; if (rb_funcall(rb_iv_get(cKeyBinder, "stock"), rb_intern("include?"), 1, self)==Qtrue) rb_raise(rb_eException, "KeyBinder allready binded."); rb_iv_set(self, "@key", key); rb_iv_set(self, "@mod", modifier); rb_iv_set(self, "block", rb_block_proc()); rb_ary_push(rb_iv_get(cKeyBinder, "stock"), self); rootwin=gdk_get_default_root_window();//stock this window in variable once for all? xdisplay=GDK_WINDOW_XDISPLAY(rootwin); keycode=XKeysymToKeycode(xdisplay, FIX2UINT(key)); if (keycode==0) rb_raise(rb_eException, "Invalid key value."); if NIL_P(modifier) mod=0; else mod=FIX2UINT(modifier); int i; for (i=0; i<4 ; i++) { XGrabKey(xdisplay, keycode, mod|ignored[i], GDK_WINDOW_XWINDOW(rootwin), False, GrabModeAsync, GrabModeAsync); } //XGrabKey(xdisplay, keycode, AnyModifier, GDK_WINDOW_XWINDOW(rootwin), False, GrabModeAsync, GrabModeAsync); return Qtrue; }
Unset a global hotkey.
/* * Unset a global hotkey. */ VALUE kb_unbind(VALUE self) { VALUE del, ret; GdkWindow *rootwin; Display *xdisplay; uint keycode, mod; uint ignored[]={0, RGHK_LOCK_MASK, RGHK_NUM_LOCK_MASK, RGHK_LOCK_MASK|RGHK_NUM_LOCK_MASK}; del=rb_funcall(rb_iv_get(cKeyBinder, "stock"), rb_intern("delete"), 1, self); if NIL_P(del) return Qfalse; rootwin=gdk_get_default_root_window(); xdisplay=GDK_WINDOW_XDISPLAY(rootwin); keycode=XKeysymToKeycode(xdisplay, FIX2UINT(rb_iv_get(self, "@key"))); mod=FIX2UINT(rb_iv_get(self, "@mod")); int i; for (i=0; i<4 ; i++) XUngrabKey(xdisplay, keycode, mod|ignored[i], GDK_WINDOW_XWINDOW (rootwin)); return Qtrue; }