1

I have an OSX laptop with a running Ubuntu Virtualbox and I either interact with it with the laptop keyboard or with an external USB keyboard that I plug in when in the office.

Is there a script I could run to detect if the external keyboard is plugged so that the keymap in Ubuntu Virtualbox changes accordingly?

I currently manually run either 'mac' or 'key' which executes this command:

setxkbmap -layout gb

or

xmodmap .Xmodmap

I would like to do the same automatically when the keyboard is plugged or unplugged. Any ideas?

3
2

You can use a udev-rule for this. In my example I am using a USB dongle for my wireless mouse, you will have to adjust accordingly:

1. Getting the identifiers

In order to make a unique rule for the device, we need to identify it properly. Use the output of udevadm for this ( your USB keyboard should show in /dev/usb/<device>. Plug it in and out to see if this device (dis)appears ). The vendor and device IDs should be unique enough, but the more matches, the better.

udeadm info -a -n /dev/usb/hiddev0 | grep 'idVendor\|idProduct'
ATTRS{idVendor}=="0e8f"
ATTRS{idProduct}=="00a4"
ATTRS{idVendor}=="1d6b"
ATTRS{idProduct}=="0001"

Note the two values that match the output from lsusb at ID (to know which device this is, either test the difference of lsubs with and without the keyboard, or see if you can match the names in the output)

lsusb
Bus 005 Device 004: ID 0e8f:00a4 GreenAsia Inc.

So we will use 0e8f and 00a4 for vendor and product ID, respectively.

2. Creating a rule

The rule itself is just a listing of what to do. Save it in /etc/udev/rules.d/ as e.g. keyboard.rules (note that the .rules suffix is obligatory). The file will match (==) a few attributes, the action, and run our script:

ACTION=="add", ATTRS{idVendor}=="0e8f", ATTRS{idProduct}=="00a4", RUN+="/full/path/to/script.sh"

You might run into problems for the ACTION=="remove" part, have a read of this. The problem being some of the attributes being deleted upon removal, and thus udev cannot match them anymore for your removal rule.

In my case I used udeadm monitor --environemnt and unplugged the devive. I selected

ID_MODEL_ID=00a4

and used this as the only requirement (you might need to do a bit of trial and error for good matches here, however a single match is rather save for your case of changing keymaps only). Note that the removal rule needs ENV instead of ATTRS (AFAIK ENV works for the plugin rule, too, but ATTRS fails for removal)

ACTION=="remove", ENV{ID_MODEL_ID}=="00a4", RUN+="/path/to/removal_script.sh"

The add and remove rules can be one file with one line for each action.

3. The script

The script will ALWAYS be run by root, so make sure that a) it is writeable by root only (for security reasons) b) if your command needs to be executed by a user, use sudo -u user1 command in the script. It should be executable of course. If you are trying to directly execute a command (i.e. without using a script) use the absolute path of said command.

Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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