Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use a notebook with a USA alternative international (former us international) layout and a usb Microsoft Curve keyboard with Brazilian layout when at my desk.

What I would like is to switch layouts automatically when the usb keyboard is detected, instead of going to preferences manually every time I leave the desk. Is there such a way in ubuntu 9.04 ?

share|improve this question
add comment

3 Answers

There are three components to the solution that would let you swap keyboard layouts:

  1. Detect when the keyboard is plugged in/out
  2. Detect the layout on the keyboard
  3. Set the layout of your system to match the keyboard layout.

1. Detect when the keyboard is plugged in/out

All hardware changes are sent on the D-Bus message bus daemon.

D-Bus is a message bus system, a simple way for applications to talk to one another. In addition to interprocess communication, D-Bus helps coordinate process lifecycle; it makes it simple and reliable to code a "single instance" application or daemon, and to launch applications and daemons on demand when their services are needed.

D-Bus supplies both a system daemon (for events such as "new hardware device added" or "printer queue changed") and a per-user-login-session daemon (for general IPC needs among user applications).

You can monitor the events sent on the D-Bus via the dbus-monitor command:

dbus-monitor --system #show all events
dbus-monitor --system --profile 'interface=org.freedesktop.Hal.Manager' # filter: only events sent by Hal
dbus-monitor --system --profile 'interface=org.freedesktop.Hal.Manager, member=DeviceAdded' # filter: only DeviceAdded events sent by Hal

Plug in your keyboard, and watch the messages go by. That should give you an idea of the filter you need to detect a keyboard being plugged in or out. You'll also need to get familiar with the dbus-monitor usage in more detail.

2. Detect the layout of the keyboard

Once the keyboard is plugged into the system, HAL(Hardware Abstraction Layer) configures it. You can retrieve this info using the hal-get-property command once you have identified the key you need. For example, on my system:

$ hal-get-property --udi /org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input --key "input.xkb.layout"
gb
$ hal-get-property --udi /org/freedesktop/Hal/devices/platform_i8042_i8042_KBD_port_logicaldev_input --key "input.xkb.model"
pc105

Look at this answer for a detailed explanation of how to get values from HAL: setting up process profiles on ubuntu. I think the D-Bus message might give you the configured HAL node for the connected device, but I'm not certain. If not, you know that it will be in either of two places - corresponding to one of two keyboards.

3. Select the layout of your system to match the keyboard layout

joe already pointed out how you can do this using xmodmap.

share|improve this answer
    
good tips. I'll take a look into this and post my findings –  Decio Lira Sep 26 '09 at 15:58
add comment

I don't know about this . But this is my work around i used to solve (switch from us to uk )

sudo xmodmap /usr/share/xmodmap/xmodmap.uk
share|improve this answer
    
I guess this would help in a script, but the important part is how do I detect they keyboard being plugged in? –  Decio Lira Sep 25 '09 at 15:56
add comment

My guess is that this might be possible using some HAL-directives.

However, you don't need to go to Preferences each time you want to switch layouts. Why don't you simply add the "Keyboard Indicator" applet to your panel? It's only one mouse-click then.

share|improve this answer
    
That's what I currently do. But strangely enough the applet works on a per app setup. So it may use the USA layout for an app and still use the BRA setup for another until I click it. Don't know if this is the normal behavior though. –  Decio Lira Sep 25 '09 at 17:35
    
This can be configured from the Keyboard preferences: System -> Preferences -> Keyboard, switch to the "Layouts" tab and untick "Separate layout for each window". –  innaM Sep 25 '09 at 17:50
    
Wow, I must have my eyes checked! That's correct, thanks. :) –  Decio Lira Sep 26 '09 at 15:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.