tag:blogger.com,1999:blog-38731226284563738032018-03-05T22:26:59.099+01:00That Linux ThingLinux configuration tips for tinkererskaqqaonoreply@blogger.comBlogger1125tag:blogger.com,1999:blog-3873122628456373803.post-40568726415093881052015-11-21T23:45:00.003+01:002015-11-22T02:50:35.963+01:00Configuring function (Fn) keys in Linux under Openbox<div dir="ltr" style="text-align: left;" trbidi="on"><p>Often, special function keys (e.g. for controlling volume, brightness, sleep etc.) will not be automatically configured in many Linux distros. Luckily, in most cases it is easy to manually set this up. As with most other settings, Openbox allows custom key bindings to be added via entries in <span class="snippet">~/.config/openbox/rc.xml</span>. This way, functionality can be assigned to Fn keys not recognized out-of-the-box. <p/><p>This is especially easy for standard functions (like volume-up and down, mute, sleep, brightness-up and down etc.) as these already have special key-codes assigned to them. For example, brightness-up button will be detected as <span class="snippet">XF86MonBrightnessUp</span> allowing functionality to be directly bound to this code. </p> <h3 style="text-align: left;">Discovering the key code to use</h3> <p>The best way to discover what happens when a key is pressed is using <span class="snippet">xev</span>. Fire it from the terminal, and it will give you a window that captures events and logs them to the console. There, you'll be able to see the code for each button or button combination you press. Later on, you can use these codes to bind commands to them.</p> <h3 style="text-align: left;">Configure keys using a graphical interface</h3> <p>Instead of editing Openbox's <span class="snippet">rc.xml</span> manually, you can use <span class="snippet"><strong>obkey</strong></span> (Openbox Key Editor), which can automate the procedure of capturing the key codes for you and binding commands to them. See below for example commands you can bind to keys.</p> <h3 style="text-align: left;">List of common function keys and example configurations</h3><div><ul style="text-align: left;"><li><h4>Increase/decrease brightness</h4>Brightness can be controlled using <span class="snippet">xbacklight</span>, so assigning the following bindings will make the brightness function keys work:<br /> <br /><pre class="xml" name="code"><br /> &lt;keybind key="XF86MonBrightnessUp"&gt;<br /> &lt;action name="Execute"&gt;<br /> &lt;command&gt;xbacklight -inc 40&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /><br /> &lt;keybind key="XF86MonBrightnessDown"&gt;<br /> &lt;action name="Execute"&gt;<br /> &lt;command&gt;xbacklight -dec 40&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li> <li><h4>Sleep and Hibernate</h4>Assuming a SystemD setup, <span class="snippet">systemctl</span> can be used to suspend the system to RAM (a.k.a. sleep), to disk (a.k.a hibernate) or both (a.k.a. hybrid sleep). Add the following to make the sleep function key work: <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86Sleep&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;systemctl suspend&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre> If you have additional keys (apart from sleep), you can try the following binding (make sure hibernation is properly configured and working on your system, and your user has the permissions to use it first): <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86Standby&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;systemctl hibernate&lt;/command&gt; &lt;!-- "systemctl hybrid-sleep" could be used instead --&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li> <li><h4>Search</h4>Many keyboards have a dedicated <em>search</em> key. To bind a command appropriate for your system (exemplified using SpaceFM) use the following: <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86Search&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;spacefm --find-files %F&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li> <li><h4>Screen lock</h4>Bind any screen-locking (screensaver) utility, like <span class="snippet">slock</span> or <span class="snippet">gnome-screensaver-command</span>, as exemplified below to make the lock-screen button work: <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86ScreenSaver&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;slock&lt;/command&gt; &lt;!-- Replace with "gnome-screensaver-command -l" or "xscreensaver-command -lock" if you wish --&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li> <li><h4>Volume up/down/mute</h4>There's a few options for dealing with volume keys. <ul><li><h5>Let <span class="snippet">volumeicon</span> manage the function keys</h5><span class="snippet">volumeicon</span> is often used with Openbox and can be configured to manage volume buttons. Additionally, it can display OSD notifications nicely, and it supports multiple back-ends (GTK+ popups, libnotify, and possibly more). If you want to go this route, edit your <span class="snippet">~/.config/volumeicon/volumeicon</span> to contain the following: <pre class="bash" name="code"><br />[Hotkeys]<br />up_enabled=true<br />down_enabled=true<br />mute_enabled=true<br />up=XF86AudioRaiseVolume<br />down=XF86AudioLowerVolume<br />mute=XF86AudioMute<br /></pre></li> <li><h5>Bind volume buttons to <span class="snippet">amixer</span> commands</h5><span class="snippet">amixer</span> can be called directly to control the volume. If you're using Alsa only, you may have to first find out the name of the mixer control your sound card exposes by using <span class="snippet">amixer scontrols</span>. It is usually called Master, but not always. With PulseAudio, it is always called Master. <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86AudioRaiseVolume&quot;&gt; <br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;!-- Increase volume by 5& --&gt;<br /> &lt;command&gt;amixer set Master 5%+ unmute&lt;/command&gt; &lt;!-- If needed, replace "Master" with whatever "amixer scontrols" has given you as the control name --&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /><br /> &lt;keybind key=&quot;XF86AudioLowerVolume&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;amixer set Master 5%- unmute&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /><br /> &lt;keybind key=&quot;XF86AudioMute&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;amixer set Master toggle&lt;/command&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li></ul></li> <li><h4>Projector/Presentation mode</h4>The projector/presentation mode button will most often simply be understood as <span class="snippet">Super (Windows key) + P</span>, and not as a separate key code, so you can bind it as such. <pre class="xml" name="code"><br /> &lt;keybind key=&quot;XF86Search&quot;&gt;<br /> &lt;action name=&quot;Execute&quot;&gt;<br /> &lt;command&gt;xrandr --auto&lt;/command&gt; &lt;!-- A more sophisticated xrandr command (to setup extended display etc) can be used here --&gt;<br /> &lt;/action&gt;<br /> &lt;/keybind&gt;<br /></pre></li></ul> <h3 style="text-align: left;">Adding OSD notifications to commands</h3> <p>Most DEs come with a notification server you can use to display on-screen messages. Find out which one your DE/distro uses and simply wrap the commands used above in a script that also fires notifications. <span class="snippet">notify-send</span> is a simple utility that comes with <span class="snippet">libnotify</span> itself and can be used in most setups. It should be noted though that it doesn't offer a <strong>universal</strong> way to re-draw or replace a notification, making it hard to display a progress bar (e.g. for displaying volume or brightness level) and will keep creating new notifications each time it is called.</p> <p>Some back-ends, like <span class="snippet">notify-osd</span>, implement an extension supporting this scenario, but you'll have to know what you have in your distro. A back-end-agnostic drop-in replacement called <a href="https://github.com/vlevit/notify-send.sh"><span class="snippet">notify-send.sh</span></a> can also be used to work around the issue with back-ends that don't support this feature themselves.</p> <ul><li><h4>Simple <span class="snippet">notify-send</span> example</h4>For back-ends supporting the extension, a commands similar to the following could be used to increment the volume and show the level on screen: <p><strong><span class="snippet">notify-send " " -i notification-audio-volume-medium -h int:value:$(amixer set Master 5%+ unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1) -h string:synchronous:volume</span></strong></p> This, of course, could be enriched to choose the correct icon based on the volume level etc. </li> <li><h4>Using <span class="snippet">notify-send.sh</span></h4>For back-ends without the extension, <span class="snippet">notify-send.sh</span> can be used with a command similar to the above, but with adding <span class="snippet">--print-id</span> as an argument. This will make the command return the notification ID that can later to be used to replace the notification with a new one by providing <span class="snippet">--replace=$ID</span>. Of course, the ID would have to persisted somewhere between the calls, like a global variable of a file. </li> </ul> </div></div>kaqqaonoreply@blogger.com0