Search code examples
webkitpygtkkeyeventinject

Python Webkit GTK injecting special keys event


I am trying to dispatch (inject) a GTK keyboard event to some window, in specific the window with a webkit webview inside of it. Problem that I have is with special keys like Backspace, Up, Down etc. For some reason they are not injected properly, meaning that corresponding functionality is not triggered. For other keys it works perfectly. I tried to focus the keyboard on the window and put the window on the top of the window z-index stack, but it doesn't help.

GdkEvent* const event = gdk_event_new(GDK_KEY_PRESS); 
event->key.window = self->window->window; 
event->key.send_event = TRUE; 
event->key.time = GDK_CURRENT_TIME; 
event->key.state = 0; event->key.keyval = key_value; 
event->key.hardware_keycode = 0;
event->key.length = 0; event->key.string = 0; 
event->key.group = 0; 
//key_value is the code for the key. 

This is the C to Python binding part where I just pass a keycode (key_value) to the function. I do the similar thing for key release event (GDK_KEY_RELEASE). In Python part I call those two functions (press and then release) and it works for normal keys like for a, s, d, etc. However, it doesn't work for BackSpace, Enter, Delete etc.


Solution

  • I solved the problem by using the KeyMap object that I get with gtk.gdk.keymap_get_default(). KeyMap has method get_entries_for_keyval(keycode) to which you need to pass just the value of keycode. It returns a triple: hardware_code, group and level. So when I passed the hardware_code and group everything started to work, except arrow keys.