Openbox: Control Speaker Volume Using the Keyboard (ALSA)

From Manjaro Linux
Jump to: navigation, search

Overview

The Openbox Window Manager is allows for custom key-bindings to be created, whereby designated combinations of key presses may be used to undertake virtually any action, and usually much faster than by alternative other means. In addition to being able to control the brightness of your screen, it is also possible to create key-bindings to control the volume as well.

As with the guide on creating key-bindings to control the screen brightness, although the Manjaro Openbox flavour provides a user-friendly graphical interface that may normally be used, it will instead be necessary to manually edit the Openbox rc.xml file, which stores and executes the key-bindings themselves. This is because the intention is to actually bind the allocated volume control keys, which otherwise cannot be undertaken in the user interface (i.e. other keys would have to be allocated instead).


Step 1: Opening the rc.xml File

note: It is not necessary to use the 'sudo' or 'gksu' commands, as you already have full permission to edit and amend the rc.xml file. It is also strongly recommended to use a text editor to amend the file, as it is quite large, and they provide search functions.


You will need to first open your terminal in order to edit the rc.xml file. The syntax of the command to edit this is:

[text editor] ~/.config/openbox/rc.xml


For example, if you wish to edit the file within the terminal using nano (a standard terminal-based text editor) then enter:

nano ~/.config/openbox/rc.xml


Otherwise - if you have installed the full version of Manjaro (i.e. not the NET-Edition) - you may find it easier to use a text editor like Gedit or Leafpad instead. This will create and open the rc.xml file up as a document, making it easier to read and edit. To use Gedit instead, the command is:

gedit ~/.config/openbox/rc.xml


Step 2: Editing the rc.xml file

Tip: If you know what you are doing, then feel free to change the intended keys. Just ensure that they are not already being used for another purpose.


Once the file has been opened, you will be presented with a substantial amount of commands contained within it. Don't worry about this, as you will only need to add a new block of code, rather than amend anything that is there. The code that must be added to enable full manual control over the volume (i.e. to raise, lower, and mute it) is as follows:

   <keybind key="XF86AudioRaiseVolume">
     <action name="Execute"> 
       <command>amixer set Master 10%+</command>
     </action>
  </keybind>
   <keybind key="XF86AudioLowerVolume">
     <action name="Execute">
       <command>amixer set Master 10%-</command>
     </action>
   </keybind>
   <keybind key="XF86AudioMute">
     <action name="Execute"> 
       <command>amixer set Master toggle</command>
     </action>
  </keybind>
Warning: This code cannot just be placed anywhere in the file. It must be placed in a particular section in order to work. Please follow the instructions below to do so.


To find the correct place in order to insert the above commands, it is recommended to use the find or Search functions provided by Gedit or Leafpad. Otherwise - if using nano - it will be necessary to manually scroll down to about the mid-way point (perhaps using the Page-Down key) to locate the following code:

</keyboard>


The overall section of the code should look like this:

    </keybind>
  </keyboard>
  <mouse>


Once located, the new code above can be inserted between the </keybind> and </keyboard> codes. Press <enter> after the </keybind> code to provide a space to do so. Below is an example where this has taken place. The new code inserted has been highlighted in green for illustrative purposes:

    </keybind>
    <keybind key="XF86AudioRaiseVolume">
      <action name="Execute"> 
        <command>amixer set Master 10%+</command>
      </action>
   </keybind>
    <keybind key="XF86AudioLowerVolume">
     <action name="Execute">
        <command>amixer set Master 10%-</command>
      </action>
    </keybind>
    <keybind key="XF86AudioMute">
      <action name="Execute"> 
        <command>amixer set Master toggle</command>
      </action>
   </keybind>
  </keyboard>
  <mouse>


Once you have made the necessary amendments to the file, save the changes and close it by:

  • nano: Press CTRL and 'x' to exit, 'y' to save, and <enter> to finish, or
  • Gedit or Leafpad: Select the 'save' option and then close the window.


Now logout and back in again for the changes to take effect.