Difference between revisions of "Xbindkeys"

From ArchWiki
Jump to: navigation, search
m (Installation: conform with the style guide)
(Undo revision 451352 by Thomastc (talk) duplicates Backlight article, see Help:Style#Hypertext metaphor)
 
(41 intermediate revisions by 24 users not shown)
Line 1: Line 1:
 
[[Category:Keyboards]]
 
[[Category:Keyboards]]
[[Category:X Server]]
+
[[Category:X server]]
 
[[fr:Xbindkeys]]
 
[[fr:Xbindkeys]]
[[tr:Xbindkeys]]
+
[[ja:Xbindkeys]]
 
[[ru:Xbindkeys]]
 
[[ru:Xbindkeys]]
== Xbindkeys ==
+
[[tr:Xbindkeys]]
 +
{{Related articles start}}
 +
{{Related|Xmodmap}}
 +
{{Related|Sxhkd}}
 +
{{Related articles end}}
  
Xbindkeys is a program that enables us to bind commands to certain keys or key combinations on the keyboard. Xbindkeys works with multimedia keys and is window manager / DE independent, so if you switch much, xbindkeys is very handy.  
+
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 ==
 
== Installation ==
Xbindkeys can be [[Pacman|installed]] with the package {{Pkg|xbindkeys}}, available in the [[official repositories]].
 
  
For those who prefer a GUI, there is the {{AUR|xbindkeys_config}} package in the [[AUR]].
+
[[Install]] the {{Pkg|xbindkeys}} package.
  
 
== Configuration ==
 
== Configuration ==
  
 
Create a file named {{ic|.xbindkeysrc}} in your home directory:
 
Create a file named {{ic|.xbindkeysrc}} in your home directory:
  touch ~/.xbindkeysrc
+
  $ touch ~/.xbindkeysrc
 +
 
 +
Alternatively, you can create a sample file (Note this includes some bindings such as {{ic|Ctrl+f}}, which you may want to edit or remove):
 +
$ 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.
  
=== Xbindkeysrc ===
+
{{Tip|After you made a change, execute {{ic|xbindkeys -p}} to reload the configuration file and apply the changes.}}
  
To see the format of a configuration file entry, enter the following command:
+
=== Volume control ===
xbindkeys -k
+
  
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 Alt + o to get the following output (results may vary):
+
Here is an example configuration file that binds Fn key combos on a laptop to {{Pkg|pamixer}} commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.
  "(Scheme function)"
+
# Increase volume
    m:0x8 + c:32
+
  "pamixer --increase 5"
    Alt + o
+
    XF86AudioRaiseVolume
  
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 the three lines to {{ic|~/.xbindkeysrc}} and replace "(Scheme function)" with the command you wish to perform. Here is an example configuration file that binds Fn key combos on a laptop to {{Ic|amixer}} commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.
 
# Increase volume
 
"amixer set Master playback 1+"
 
    m:0x0 + c:123
 
    XF86AudioRaiseVolume
 
 
 
  # Decrease volume
 
  # Decrease volume
  "amixer set Master playback 1-"
+
  "pamixer --decrease 5"
    m:0x0 + c:122
+
    XF86AudioLowerVolume
    XF86AudioLowerVolume
+
+
# Toggle mute
+
"amixer set Master toggle"
+
    m:0x0 + c:121
+
    XF86AudioMute
+
  
{{Tip|Use ''xbindkeys -mk'' to keep the key prompt open for multiple keypresses. Press ''q'' to quit.}}
+
For alternative commands to control volume, see [[PulseAudio#Keyboard volume control]] or [[ALSA#Keyboard volume control]].
  
 
=== GUI method ===
 
=== GUI method ===
  
If you installed the xbindkeys_config package, just run:
+
For graphical configuration [[install]] the {{AUR|xbindkeys_config-gtk2}} package and run:
  xbindkeys_config
+
  $ xbindkeys_config
  
== Usage ==
+
== Identifying keycodes ==
  
Once you're done configuring your keys, edit your {{ic|~/.xinitrc}} and place  
+
To find the keycodes for a particular key, enter the following command:
 +
$ xbindkeys -k
 +
 
 +
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):
 +
"(Scheme function)"
 +
    m:0x8 + c:32
 +
    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.
 +
 
 +
{{Tip|Use {{ic|xbindkeys -mk}} to keep the key prompt open for multiple keypresses. Press {{ic|q}} to quit.}}
 +
 
 +
To identify mouse buttons, you can use xev, see [https://blog.hanschen.org/2009/10/13/mouse-shortcuts-with-xbindkeys/ here]
 +
 
 +
== Making changes permanent ==
 +
 
 +
Once you're done configuring your keys, edit your [[xprofile]] or [[xinitrc]] file (depending on your window manager) and place  
 
  xbindkeys
 
  xbindkeys
  
 
before the line that starts your window manager or DE.
 
before the line that starts your window manager or DE.
 +
  
 
== Simulating multimedia keys ==
 
== Simulating multimedia keys ==
  
The XF86Audio* and other multimedia keys[http://wiki.linuxquestions.org/wiki/XF86_keyboard_symbols] are pretty-much well-recognized by the major DEs. For keyboards without such keys, you can simulate their effect with other keys
+
The XF86Audio* and other multimedia keys [http://wiki.linuxquestions.org/wiki/XF86_keyboard_symbols] 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
 
  # Decrease volume on pressing Super-minus
 
  "amixer set Master playback 1-"
 
  "amixer set Master playback 1-"
 
     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 xdotool[http://www.semicomplete.com/projects/xdotool/] (its in [community]) and xmacro[http://xmacro.sourceforge.net/] (in the AUR). Unfortunately since you'd already be holding down some modifier key (Super or Shift, for example), X will see the result as Super-XF86AudioLowerVolume which won't do anything useful. Here's a script based on xmacro and xmodmap from the xorg-server-utils 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}} (from [[official repositories]]) and {{AUR|xmacro}} (from the [[AUR]]). Unfortunately since you'd already be holding down some modifier key (Super or Shift, for example), X will see the result as {{ic|Super-XF86AudioLowerVolume}} which won't do anything useful. Here's a script based on ''xmacro'' and ''xmodmap'' from the {{Pkg|xorg-server-utils}} package for doing this[https://bbs.archlinux.org/viewtopic.php?pid=843395].
#!/bin/sh
+
{{bc|
echo 'KeyStrRelease Super_L KeyStrRelease minus' | xmacroplay :0;
+
#!/bin/sh
xmodmap -e 'remove Mod4 = Super_L';
+
echo 'KeyStrRelease Super_L KeyStrRelease minus' | xmacroplay :0
echo 'KeyStrPress XF86AudioLowerVolume KeyStrRelease XF86AudioLowerVolume' | xmacroplay :0;
+
xmodmap -e 'remove Mod4 = Super_L'
xmodmap -e 'add Mod4 = Super_L';
+
echo 'KeyStrPress XF86AudioLowerVolume KeyStrRelease XF86AudioLowerVolume' | xmacroplay :0
This works for calling XF86AudioLowerVolume once (assuming you're using Super-minus), but repeatedly calling it without releasing the Super key (like tapping on a volume button) doesn't work. If you'd like it to work that way, add the following line to the bottom of the script.
+
xmodmap -e 'add Mod4 = Super_L'
 +
}}
 +
This works for calling XF86AudioLowerVolume once (assuming you are using {{ic|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
 
  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.
 
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.
Line 82: Line 94:
  
 
If, for any reason, a hotkey you ''already'' set in {{ic|~/.xbindkeysrc}} doesn't work, open up a terminal and type the following:
 
If, for any reason, a hotkey you ''already'' set in {{ic|~/.xbindkeysrc}} doesn't work, open up a terminal and type the following:
  xbindkeys -n
+
  $ 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
  
By pressing the non-working key, you will be able to see any error xbindkeys encounter (e.g: mistyped command/keycode,...).
+
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.

Latest revision as of 20:56, 21 September 2016

Related articles

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 file named .xbindkeysrc in your home directory:

$ touch ~/.xbindkeysrc

Alternatively, 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.

Tip: After you made a change, execute xbindkeys -p 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 pamixer commands that adjust sound volume. Note that pound (#) symbols can be used to create comments.

# Increase volume
"pamixer --increase 5"
   XF86AudioRaiseVolume
# Decrease volume
"pamixer --decrease 5"
   XF86AudioLowerVolume

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

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 -k

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

The first line represents a command. The second contains the state (0x8) and keycode (32) as reported by 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 ~/.xbindkeysrc and replace "(Scheme function)" with the command you wish to perform.

Tip: Use xbindkeys -mk to keep the key prompt open for multiple keypresses. Press q to quit.

To identify mouse buttons, you can use xev, see here

Making changes permanent

Once you're 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 [1] 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
"amixer set Master playback 1-"
   m:0x50 + c:20
   Mod2+Mod4 + minus

However, to actually call the keys themselves you can use tools like xdotool (from official repositories) and xmacroAUR (from the AUR). Unfortunately since you'd already be holding down some modifier key (Super or Shift, for example), X will see the result as Super-XF86AudioLowerVolume which won't do anything useful. Here's a script based on xmacro and xmodmap from the xorg-server-utils package for doing this[2].

#!/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 doesn't 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.