Personal tools
Document Actions

Keyboard emulation gadget

Up to Tux gadgets

Keyboard emulation gadget

Posted by Olivier Samyn at March 24. 2008

I just wanted to control an OpenOffice.org presentation using the tux buttons.


But no way to do it using the current gadgets.


So, instead of creating a specific one, I just made a gadget that let you send Xorg keyboard events for every tux button.


With OpenOffice.org I just map the keys to left wing = page down, right wing = page up and head = F5...

If you want to try it:

http://www.oleastre.be/tuxdroid/keyboard_emu.tgf




Any feedback is welcome.




Re: Keyboard emulation gadget

Posted by Snouffelaire at March 25. 2008

Finally !!! Someone who had the courage to dive into Xlib !!! I must admit I've tried a bit and didn't understand a thing about this python module... Now I have something to work on !! I'm writing a script to bind all sort of things to the remote buttons (increasing/decreasing volume, using amarok, watching IMAP mail accounts, playing scenes, all configurable via txt files) and I was desperatly in need of such a possibility : sending keyboard events. If I find the time to do it, I'll adapt your code to my script, if you don't mind of course.




Thank you so much !


Re: Keyboard emulation gadget

Posted by Olivier Samyn at March 25. 2008

Oups... I forgot to mention the gadget dependencies:

- Gtk >= 2.10.0 (to be able to configure the shortcuts)

- python-xlib to be able to send keyboard events. (http://python-xlib.sourceforge.net/)


Using python-xlib, it's really easy to send an XEvent, here is a simple example on how to send a keyPress event:

<code>

from Xlib.display import Display
from Xlib import X
from Xlib.protocol import event
import time

display = Display()
focus = display.get_input_focus().focus

keycode = 94
state = 0

keyevt = event.KeyPress(
    detail=keycode,
    time=X.CurrentTime,
    root=display.screen().root,
    window=focus,
    child=X.NONE,
    root_x=1,
    root_y=1,
    event_x=1,
    event_y=1,
    state=state,
    same_screen=1
    )
focus.send_event(keyevt)

</code>


The only trick is to find the correct keycode and state combination... but this can be done with gtk or simply by running xev and watching incoming events.


Re: Keyboard emulation gadget

Posted by Snouffelaire at March 25. 2008

Is there a way to send key combinations like Ctrl+Alt+Left or so ?


Re: Keyboard emulation gadget

Posted by Olivier Samyn at March 26. 2008
Yes of course. Ctrl and Alt keys are modifiers that just sets some bytes to on in the "state" variable.
On my belgian keyboard, state=12 and keycode=100.

If you use this keyboard_emu gadget, configure it with your preffered keys and the do something like:
>  unzip -p /opt/tuxdroid/apps/tux_manager/gadgets/keyboard_emu.tgf settings.xml
it will show you the  content of the settings file. At the end, you will see some parameters starting with head, lwing or rwing.

keycode is the code parameter and state is the mods one.

if you use xev, you will see the following output for the same key event:

KeyRelease event, serial 30, synthetic NO, window 0x3600001,
    root 0x187, subw 0x0, time 3977074346, (116,131), root:(118,183),
    state 0x1c, keycode 100 (keysym 0xff51, Left), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False


As you can see, state i s 0x1c (ie 12 in hex) and keycode is 100.

Be carefull with this as the keycode depends on the keyboard used.
There are some utilities functions that can convert a keycode to a keysymbol.

Have a look at keycode_to_keysym and keysym_to_keycode in the python-xlib documentation.
http://python-xlib.sourceforge.net/doc/html/python-xlib_16.html

(but I never used them so...)

Powered by Ploneboard
You are here: Home Forum Tux gadgets Keyboard emulation gadget

Powered by Plone CMS, the Open Source Content Management System