In the Part 9 of the FreeBSD Desktop series I would like to describe key components of self made custom desktop environment such as:
- Window Manager
- Status Bar
- Task Bar
- Wallpaper Handling
- Application Launcher
- Keyboard/Mouse Shortcuts
- Locking Solution
- Blue Light Spectrum Suppress
Today we will focus on the sixth part – the Keyboard/Mouse Shortcuts. In the next series each of these components configuration would also be described along with eventual needed scripts.
You may also check earlier/other articles of the FreeBSD Desktop series:
- FreeBSD Desktop – Part 1 – Simplified Boot
- FreeBSD Desktop – Part 2 – Install
- FreeBSD Desktop – Part 3 – X11 Window System
- FreeBSD Desktop – Part 4 – Key Components – Window Manager
- FreeBSD Desktop – Part 5 – Key Components – Status Bar
- FreeBSD Desktop – Part 6 – Key Components – Task Bar
- FreeBSD Desktop – Part 7 – Key Components – Wallpaper Handling
- FreeBSD Desktop – Part 8 – Key Components – Application Launcher
- FreeBSD Desktop – Part 9 – Key Components – Keyboard/Mouse Shortcuts
- FreeBSD Desktop – Part 10 – Key Components – Locking Solution
- FreeBSD Desktop – Part 11 – Key Components – Blue Light Spectrum Suppress
- FreeBSD Desktop – Part 12 – Configuration – Openbox
- FreeBSD Desktop – Part 13 – Configuration – Dzen2
- FreeBSD Desktop – Part 14 – Configuration – Tint2
While most window managers and desktop environments offer some kind of shortcuts or tools to define your own ones its often quite complicated to force them to do exactly what you want. For example – while Openbox supports creating keyboard and mouse shortcuts it always require modifier key like [ALT], [CTRL] or [SUPER] for example. It will also not allow you to create mouse shortcuts like using only the left button of the mouse wheel (not to confuse with regular left mouse button). Or to add some function or script to keys like [Scroll Lock] or [Pause Break] without any other modifiers.
Fortunately there are tools that can and work well together to achieve almost anything you can imagine. While earlier in the series I made knockouts of solutions – or choosing the best one from several potential candidates in this part its different. I use ALL of these tools and there will be no knockout, they all provide some ‘missing bits’ to the Openbox or probably most of other window managers.
These tools are:
xev is used to ‘read’ what has keyboard key has been pressed or which mouse button was used. While it may sound stupid for most of the keys it does not for ‘special’ ones like Volume Up and Volume Down ‘dedicated’ buttons on many laptops or [Page Up] and [Page Down] keys which are named
This tool is used to assign or rename keys to make something different, for example Lenovo ThinkPad laptops have two special keys – lets call them Forward and Backward – near the Arrows keys. They work a lot better as [Page Up] and [Page Down] keys so with
xmodmap its possible to map them that way.
Other popular use case are laptop Volume Up and Volume Down buttons, or Volume Mute button. While checked with
xev they often has only some keycode attached while you may map them into
XF86AudioMute respectively so that X11 will take care of the rest, they will ‘start work’ in most applications thanks to that mapping. Additional scripting is not needed here.
Example modifier map shown by
% xmodmap -pm xmodmap: up to 4 keys per modifier, (keycodes in parentheses): shift Shift_L (0x32), Shift_R (0x3e) lock Caps_Lock (0x42) control Control_L (0x25), Control_R (0x6d) mod1 Alt_L (0x40), Meta_L (0x9c) mod2 Num_Lock (0x4d) mod3 mod4 Super_L (0x73), Super_R (0x74), Super_L (0x7f), Hyper_L (0x80) mod5 Mode_switch (0x8), ISO_Level3_Shift (0x7c)
xmodmap will load and map our keys and then will exit, files like
~/.xsession are good places for such startup keyboard setup.
While most window managers – including Openbox – support configuring keyboard shortcuts it is not perfect and has some limitations, like requiring modifier key like [ALT], [CTRL] or [SUPER] to be used in every shortcut for example.
This is where
xbindkeys steps in. While
xev will do for most cases it will fail you in more advanced scenarios. For example when You would like to attach Volume Up and Volume Down actions to your mouse Left Wheel Button and Right Wheel Button keys. I will probably not be able to express how useful mapping it is. Even when you laptop screen is locked you will be able to turn the volume down or up.
xbindkeys comes with its own window to catch events.
Here are these bindings.
% xbindkeys -s "mixer pcm 100; mixer vol +5" m:0x0 + b:9 (mouse) "mixer pcm 100; mixer vol -5" m:0x0 + b:8 (mouse) (...)
xbindkeys will run as a daemon in the background, files like
~/.xsession are usual places to start it when our X11 environment starts.
xbindkeys tool also comes with its own ‘key identifier’ as
xev does. Use it with
-k/--key argument to identify one key pressed or
-mk/--multikey argument to identify multi key pressed respectively.
% xbindkeys -k Press combination of keys or/and click under the window. You can use one of the two lines after "NoCommand" in $HOME/.xbindkeysrc to bind a key. "(Scheme function)" m:0x0 + c:99 Prior %
Sometimes you want to ‘fake’ that some keys were pressed – but without pressing them – without touching the keyboard. While some applications offer some settings to be changed by an argument to their main binary name – like for example DeaDBeeF media player allows you to type
deadbeef --next to skip to next song – many applications don’t provide such ‘API’. This is where
xdotool comes handy – it can generate any keyboard shortcut and send it to and application in X11 environment. Great tool for scripting and automating ‘work’ on the desktop and also for situations where nothing else works.
An example of such ‘useful’ shortcut may be emulating of [Z] (the [Shift]+[z] combination) key press on
mupdf PDF viewer to “Zoom page to fit either to width or height of window.” after opening new PDF document.
Each of these tools plays some role in keyboard and mouse shortcuts on the X11 desktop. Together they are able to execute any solution you can imagine.