Difference between revisions of "Xbindkeys"

From ArchWiki
Jump to: navigation, search
(add Xorg#Automation to Related articles)
m (Identifying keycodes: layout improvements)
 
(10 intermediate revisions by the same user not shown)
Line 22: Line 22:
 
  $ xbindkeys -d > ~/.xbindkeysrc
 
  $ xbindkeys -d > ~/.xbindkeysrc
  
Now you can either edit {{ic|~/.xbindkeysrc}} to set keybindings, or you can do that with the GUI.
+
Now you can either edit {{ic|~/.xbindkeysrc}} to set keybindings, or you can do that with the [[#GUI method]].
  
{{Tip|After you made a change, execute {{ic|xbindkeys -p}} to reload the configuration file and apply the changes.}}
+
{{Tip|After you made a change, execute {{ic|xbindkeys --poll-rc}} to reload the configuration file and apply the changes.}}
  
 
=== Volume control ===
 
=== Volume control ===
  
Here is an example configuration file that binds Fn key combos on a laptop to ''pactl'' commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.
+
Here is an example configuration file that binds {{ic|Fn}} key combos on a laptop to ''pactl'' commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.
 
  # Increase volume
 
  # Increase volume
 
  "pactl set-sink-volume @DEFAULT_SINK@ +1000"
 
  "pactl set-sink-volume @DEFAULT_SINK@ +1000"
Line 62: Line 62:
  
 
To find the keycodes for a particular key, enter the following command:
 
To find the keycodes for a particular key, enter the following command:
  $ xbindkeys -k
+
  $ xbindkeys --key
 +
or the following to grab multiple keys:
 +
$ xbindkeys --multikey
  
 
A blank window will pop up. Press the key(s) to which you wish to assign a command and ''xbindkeys'' will output a handy snippet that can be entered into {{ic|~/.xbindkeysrc}}. For example, while the blank window is open, press {{ic|Alt+o}} to get the following output (results may vary):
 
A blank window will pop up. Press the key(s) to which you wish to assign a command and ''xbindkeys'' will output a handy snippet that can be entered into {{ic|~/.xbindkeysrc}}. For example, while the blank window is open, press {{ic|Alt+o}} to get the following output (results may vary):
Line 69: Line 71:
 
     Alt + o
 
     Alt + o
  
The first line represents a command. The second contains the state (0x8) and keycode (32) as reported by {{ic|xev}}. The third line contains the keysyms associated with the given keycodes. To use this output, copy either one of the last two lines to {{ic|~/.xbindkeysrc}} and replace "(Scheme function)" with the command you wish to perform.
+
# The first line represents a command.
 +
# The second line contains the state (0x8) and keycode (32) as reported by the tool ''xev''.
 +
# The third line contains the keysyms associated with the given keycodes.
  
{{Tip|Use {{ic|xbindkeys -mk}} to keep the key prompt open for multiple keypresses. Press {{ic|q}} to quit.}}
+
To use this output, copy either one of the last two lines to {{ic|~/.xbindkeysrc}} and replace "(Scheme function)" with the command you wish to perform.
  
To identify mouse buttons, you can use xev, see [https://blog.hanschen.org/2009/10/13/mouse-shortcuts-with-xbindkeys/].
+
To identify mouse buttons, ''xev'' can be used, see [https://blog.hanschen.org/2009/10/13/mouse-shortcuts-with-xbindkeys/].
 +
 
 +
{{Tip|If the multikey mode is used, press {{ic|q}} to quit the window.}}
 +
{{Note|''xev'' is provided within the {{Pkg|xorg-xev}} package, see {{man|1|xev}} for more information.}}
  
 
== Making changes permanent ==
 
== Making changes permanent ==
Line 89: Line 96:
 
     m:0x50 + c:20
 
     m:0x50 + c:20
 
     Mod2+Mod4 + minus
 
     Mod2+Mod4 + minus
However, to actually call the keys themselves you can use tools like {{Pkg|xdotool}} (from [[official repositories]]) and {{AUR|xmacro}} (from the [[AUR]]). Unfortunately since you would already be holding down some modifier key (Super or Shift, for example), X will see the result as {{ic|Super-XF86AudioLowerVolume}} which will not do anything useful. Here is a script based on ''xmacro'' and ''xmodmap'' from the {{Pkg|xorg-xmodmap}} package for doing this[https://bbs.archlinux.org/viewtopic.php?pid=843395].
+
However, to actually call the keys themselves you can use tools like {{Pkg|xdotool}} and {{AUR|xmacro}}. Unfortunately since you would already be holding down some modifier key (Super or Shift, for example), X will see the result as {{ic|Super-XF86AudioLowerVolume}} which will not do anything useful. Here is a script based on ''xmacro'' and ''xmodmap'' from the {{Pkg|xorg-xmodmap}} package for doing this[https://bbs.archlinux.org/viewtopic.php?pid=843395].
 
{{bc|
 
{{bc|
 
#!/bin/sh
 
#!/bin/sh
Line 114: Line 121:
 
     Mod2 + F7 + Release
 
     Mod2 + F7 + Release
  
This will make the F7 key play/pause audio. Where the "xdotool" command would work in commandline, if the "+ Release" is removed it will fail with xbindkeys.
+
This will make the {{ic|F7}} key play/pause audio. Where the "xdotool" command would work in commandline, if the "+ Release" is removed it will fail with xbindkeys.

Latest revision as of 08:42, 13 October 2018

Xbindkeys is a program that allows to bind commands to certain keys or key combinations on the keyboard. Xbindkeys works with multimedia keys and is independent of the window manager and desktop environment.

Installation

Install the xbindkeys package.

Configuration

Create a blank ~/.xbindkeysrc, or you can create a sample file (Note this includes some bindings such as Ctrl+f, which you may want to edit or remove):

$ xbindkeys -d > ~/.xbindkeysrc

Now you can either edit ~/.xbindkeysrc to set keybindings, or you can do that with the #GUI method.

Tip: After you made a change, execute xbindkeys --poll-rc to reload the configuration file and apply the changes.

Volume control

Here is an example configuration file that binds Fn key combos on a laptop to pactl commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.

# Increase volume
"pactl set-sink-volume @DEFAULT_SINK@ +1000"
   XF86AudioRaiseVolume
# Decrease volume
"pactl set-sink-volume @DEFAULT_SINK@ -1000"
   XF86AudioLowerVolume
# Mute volume
"pactl set-sink-mute @DEFAULT_SINK@ toggle"
   XF86AudioMute

For alternative commands to control volume, see PulseAudio#Keyboard volume control or ALSA#Keyboard volume control.

Backlight control

Keybindings can also be defined in order to control the brightness of the screen.

# Increase backlight
"xbacklight -inc 10"
   XF86MonBrightnessUp
# Decrease backlight
"xbacklight -dec 10"
   XF86MonBrightnessDown

GUI method

For graphical configuration install the xbindkeys_config-gtk2AUR package and run:

$ xbindkeys_config

Identifying keycodes

To find the keycodes for a particular key, enter the following command:

$ xbindkeys --key

or the following to grab multiple keys:

$ xbindkeys --multikey

A blank window will pop up. Press the key(s) to which you wish to assign a command and xbindkeys will output a handy snippet that can be entered into ~/.xbindkeysrc. For example, while the blank window is open, press Alt+o to get the following output (results may vary):

"(Scheme function)"
    m:0x8 + c:32
    Alt + o
  1. The first line represents a command.
  2. The second line contains the state (0x8) and keycode (32) as reported by the tool xev.
  3. The third line contains the keysyms associated with the given keycodes.

To use this output, copy either one of the last two lines to ~/.xbindkeysrc and replace "(Scheme function)" with the command you wish to perform.

To identify mouse buttons, xev can be used, see [1].

Tip: If the multikey mode is used, press q to quit the window.
Note: xev is provided within the xorg-xev package, see xev(1) for more information.

Making changes permanent

Once you are done configuring your keys, edit your xprofile or xinitrc file (depending on your window manager) and place

xbindkeys

before the line that starts your window manager or DE.

Simulating multimedia keys

The XF86Audio* and other multimedia keys [2] are pretty-much well-recognized by the major DEs. For keyboards without such keys, you can simulate their effect with other keys

# Decrease volume on pressing Super-minus
"pactl set-sink-volume 0 -1000"
   m:0x50 + c:20
   Mod2+Mod4 + minus

However, to actually call the keys themselves you can use tools like xdotool and xmacroAUR. Unfortunately since you would already be holding down some modifier key (Super or Shift, for example), X will see the result as Super-XF86AudioLowerVolume which will not do anything useful. Here is a script based on xmacro and xmodmap from the xorg-xmodmap package for doing this[3].

#!/bin/sh
echo 'KeyStrRelease Super_L KeyStrRelease minus' 

This works for calling XF86AudioLowerVolume once (assuming you are using Super+minus), but repeatedly calling it without releasing the Super key (like tapping on a volume button) does not work. If you would like it to work that way, add the following line to the bottom of the script.

echo 'KeyStrPress Super_L' | xmacroplay :0

With this modified script, if you press the key combination fast enough your Super_L key will remain 'on' till the next time you hit it, which may result in some interesting side-effects. Just tap it again to remove that state, or use the original script if you want things to 'just work' and do not mind not multi-tapping on volume up/down.

These instructions are valid for pretty much any one of the XF86 multimedia keys (important ones would be XF86AudioRaiseVolume, XF86AudioLowerVolume, XF86AudioPlay, XF86AudioPrev, XF86AudioNext).

Troubleshooting

If, for any reason, a hotkey you already set in ~/.xbindkeysrc does not work, open up a terminal and type the following:

$ xbindkeys -n

By pressing the non-working key, you will be able to see any error xbindkeys encounter (e.g: mistyped command/keycode,...).

If the command for a keybind works via the xdotool in command line, but not when activated by the hotkey try adding "+ Release" to the hotkey (Esp notable on gnome):

"xdotool key --clearmodifiers XF86AudioPlay"
    Mod2 + F7 + Release

This will make the F7 key play/pause audio. Where the "xdotool" command would work in commandline, if the "+ Release" is removed it will fail with xbindkeys.