How to Bind Shortcut Keys to WINE Programs in Linux

So you love your Linux machine, but you must have that one (or probably a few) Windows program working on your machine. You found what felt like a magic wand, and installed Wine. But when you go to use that favorite app, the shortcut keys you’re used to in the Windows world just don’t work here in Linux! So what do you do?  We’ll show you how you can map and bind shortcut keys to your Wine program. We assume you already have Wine installed.

Note that some apps require you to explicitly enable hotkeys. This is normally found in the “Options” or “Settings” menu. For example for Balabolka, you’d have to go and enable hotkeys in the settings.

BindHotkeysWine-Balabolka-shortcut-key

The most common tools to do this key mapping are xdotool and xte. These are both key emulators that you can use to hook up scripts corresponding to key presses. Then you’ll have to use Xbindkeys to bind any special keys or buttons. Let’s see how to use these, and other ways you can map shortcut keys to Wine programs.

Xdotool lets you do many Windows tasks from the command line. It essentially lets you simulate keyboard and mouse activity. You can look up the complete mapping here, but for our purposes, we’re going to use just two basic commands – search and key. First steps first – install xdotool with the usual command:

sudo apt-get install xdotool

Next you create the mapping you want in this manner:

xdotool key --window $( xdotool search --name YOUR_APP ) YOUR_KEYSTROKE

Let’s take Spotify as an example, and look at how we can map the Global Media Hotkeys. For convenience, let’s put it into a script (courtesy Arch linux Wiki)  music.sh

#!/bin/sh
 
case $1 in
   "play")
       key="XF86AudioPlay"
       ;;
   "next")
       key="XF86AudioNext"
       ;;
   "prev")
       key="XF86AudioPrev"
       ;;
   *)
       echo "Usage: $0 play|next|prev"
       exit 1
        ;;
esac
xdotool key --window $(xdotool search --name "Spotify (Premium )?- Linux Preview"|head -n1) $key
exit 0

Remember to make your script executable with a simple “chmod“. Once you’ve mapped this, you’ll need to bind this script to any tool that actually catches key presses. Here’s how to do it with xbindkeys

<keybind key="XF86AudioPlay">
  <action name="Execute">
      <execute>~/bin/musickeys.sh play</execute>
  </action>
</keybind>
<keybind key="XF86AudioNext">
  <action name="Execute">
      <execute>~/bin/musickeys.sh next</execute>
  </action>
</keybind>
<keybind key="XF86AudioPrev">
  <action name="Execute">
      <execute>~/bin/musickeys.sh prev</execute>
  </action>
</keybind>

BindHotkeysWine-control-modifier-key

The most common problem most folks run into while using xdotool or other key mapping is related to some extra Ctrl, Shift or Alt key being pressed down while they’re setting the shortcut, or sometimes having an extra key_up or key_down. There’s a simple way to remove the extra modifiers – just use the “clearmodifiers” option.

xdotool key --clearmodifiers XF86AudioRaiseVolume

The clearmodifiers searches for any special keys like Shift, Ctrl, or Alt that are being pressed, removes them by sending the appropriate key up sequence, runs your xdotool command, and restores your modifier by sending the appropriate key down. You can look up further details on the man page.

We just saw how to map the global media hotkeys with Spotify. It’s much simpler with foobar. Wine normally allows foobar to map the multimedia keys, if not already mapped by some other program. To set up global multimedia hotkeys with Foobar + Wine, here’s what you can do: 

- Open the “Gnome Configuration Editor”.

- On the left navigation pane, select “/apps/metacity/keybinding_commands” and set up

command_1 - wine ~/.foobar2000/foobar2000.exe /playpause
command_2 - wine ~/.foobar2000/foobar2000.exe /stop
command_3 - wine ~/.foobar2000/foobar2000.exe /next
command_4 - wine ~/.foobar2000/foobar2000.exe /prev

Then go to “/apps/metacity/global_keybindings” and define:

run_command_1 - XF86AudioPause
run_command_2 - XF86AudioStop
run_command_3 - XF86AudioNext
run_command_4 - XF86AudioPrev

Xte is another tool you can use to map keyboard inputs. It’s part of the xautomation package and you can install it with

sudo apt-get install xdotool
sudo apt-get install xautomation
sudo apt-get install xwd

It generates fake inputs (key or mouse) using the XTest Extension. Let’s take a simple example. Say you want to map “Ctrl + PageUp” to switch to the previous tab and make use of the XF86Back key. Create a script with executable permissions (755), call is sc_back:

!/bin/bash
 
/usr/bin/xte 'keydown Control_L' 'keydown Page_Up' 'keyup Page_Up' 'keyup Control_L'

Then make sure you map XF86Back as the shortcut for sc_back. This should let you switch to the previous tab with “Ctrl + PageUp”.

Depending on exactly what you want to achieve, there are many ways to map and bind shortcut keys. We’ve covered some of the examples in this tutorial, but there are many other ways as well. For example, Autokey provides one of the cleanest ways. Like the xdotool method we described above for Spotify, it lets you create scripts and map them to certain shortcut keys. Our earlier tutorial on this topic can show you how to use it. You can experiment to figure out which method works best for you.

Image credits: William Hook, F Hashemi,  Walt Stoneburner