View Single Post
dcarter's Avatar
Posts: 229 | Thanked: 29 times | Joined on May 2006
#239
Originally Posted by jakoleh View Post
Here is my simple python script to execute setxkbmap when keyboard connects. You can test it if it is any use for you. It is very quick and dirty version, but is working for me. Just change those values to correct ones.

Code:
import sys
# import python dbus module
import dbus
# import python dbus GLib mainloop support
import dbus.mainloop.glib
import gobject
import thread, time
import re
import commands

#hildon-im-xkbtool --list
KEYBOARDNAME = "Nokia SU-8W"
#Keyboard mac address
DEVICE = "dev_00_0E_xx_xx_xx_xx"
#setxkbmap 
MODEL = "nokiasu8w"
LAYOUT = "fi"

def connected(*args, **kwargs):    
    bus = dbus.SystemBus()
    iface = dbus.Interface(bus.get_object('org.freedesktop.Notifications',
                                     '/org/freedesktop/Notifications'),
                                     'org.freedesktop.Notifications')
    if args[0] == "Connected":
        if args[1] == False:       
            iface.SystemNoteInfoprint(KEYBOARDNAME + " disconnected")
        else:
            iface.SystemNoteInfoprint(KEYBOARDNAME + " connected")
            time.sleep(2)
            testi = commands.getoutput("hildon-im-xkbtool --list")   
            #ID 3, Name: "keyboardname"
            keybId = re.search("ID (\\d+), Name: \"" + KEYBOARDNAME, str(testi)).group(1)
            commands.getoutput("setxkbmap -device " + keybId + 
                        " -I -I/usr/share/X11/xkb-chinook " +
                        "-rules base -model " + MODEL + 
                        " -layout " + LAYOUT)            
                            
    
def main():            
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    # Get the session bus
    bus = dbus.SystemBus()        
    try:       
        #Get default adapter
        manager = dbus.Interface(bus.get_object("org.bluez",
                        "/"),
                        "org.bluez.Manager")
        #dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.DefaultAdapter     
        adapter = manager.DefaultAdapter()
    # Get the remote interface for the remote object
        interface = dbus.Interface(bus.get_object('org.bluez',
                                   adapter + "/" + DEVICE), 
                                   "org.bluez.Input")  
        interface.connect_to_signal("PropertyChanged", connected)              
    except dbus.DBusException:        
        sys.exit(1)    
    loop = gobject.MainLoop()    
    loop.run()
    
if __name__ == "__main__":
    main()
OK, I apologize in advance,
but to run this script (after entering my own personal variables corresponding to my keyboard) should I create a text file with Leafpad?

If so, then how do I run it and make it always run?

dcarter