HOW-TO:Modify keymaps

The keyboard.xml file controls how Kodi reacts to keypresses, that is it determines what action is mapped to what keypress. There are two main reasons for modifying keyboard.xml:


 * 1) You want to change the standard key mappings because of a personal preference
 * 2) You are configuring a Media Center remote control that sends keypresses

You can edit keyboard.xml using any text editor such as Notepad++ in Windows or gedit/nano in Linux. The location where the file should be located after creation is in userdata

There is a more detailed discussion of the keyboard.xml file at keymaps, while the remainder of this article is focussed on the gory details of editing it.

Introduction
All the keypresses that Kodi responds to, for example "" for "play" and "" for "stop" are configured in a file called keyboard.xml. Actually there are two keyboard.xml files. There is the system keyboard.xml that contains all the standard key mappings, and each user optionally has their own userdata keyboard.xml that contains just their customised key mappings.

The userdata keyboard.xml only needs to contain additional key mappings, or key mappings that override the defaults in the system keyboard.xml. When Kodi is trying to decide how to respond to a keypress it first looks in the userdata keyboard.xml. If it doesn't find a mapping for the keypress Kodi then looks in the system keyboard.xml. This means that the userdata keyboard.xml is typically quite short because it only needs to define mappings for a few keys.

You can change key mappings by editing the system keyboard.xml, but we strongly recommend you don't do this. The system keyboard.xml is a big complicated file, and if you introduce an error into it you can break all the key mappings. Also any changes you make risk being overwritten if you upgrade Kodi. In general you should only ever edit your userdata keyboard.xml. Even if you make a horrendous hash of this you just need to delete or rename your userdata keyboard.xml to restore the default key mappings.

User keymaps are only processed when Kodi starts up, therfore you must restart Kodi to see any changes.

Where to find keyboard.xml
Keymap configuration files are stored in the "keymaps" folder in the userdata folder:

How to edit keyboard.xml
keyboard.xml is just a text file so you can edit it it using any text editor e.g. in Windows use Notepad++. In Windows there is a third party keymap editor available from http://xbmcmce.sourceforge.net/, or this editor is also available through the Add-on:MCERemote add-on.

Windows

To use Notepad++ to edit your userdata keyboard.xml click Start then Run, or in Win7 click then All Programs then Accessories then Run, or press the keyboard shortcut, then when the Run dialog opens type in:

notepad++ %APPDATA%\Kodi\userdata\keymaps\keyboard.xml


 * If you haven't edited your keyboard.xml before Notepad will ask "Do you want to create a new file?" and you should click Yes.


 * If you have downloaded the KeyMapEdit applet from http://xbmcmce.sourceforge.net/ run it then select File/Open and double click "keyboard.xml".


 * If you use the MCERemote add-on just select the "Edit keyboard.xml" option.

Linux

Use your favourite text editor e.g. gedit. Remember that the .kodi directory is hidden so you need to show hidden files to see it.

What to put in keyboard.xml
This is an outline rather than a definitive guide. For the full details see keymaps.

A keyboard.xml file will look something like:

&lt;keymap&gt; &lt;global&gt; &lt;keyboard&gt; ... key mappings here &lt;/keyboard&gt; &lt;/global&gt; &lt;Home&gt; &lt;keyboard&gt; ... key mappings here &lt;/keyboard&gt; &lt;/Home&gt; ... and so on &lt;/keymap&gt;

The keyboard.xml must start with &lt;keymap&gt; and end with &lt;/keymap&gt;. In between are a number of sections; in the example above the first section is &lt;global&gt;, the second section is &lt;Home&gt;, and there can be lots of other sections as well.

The &lt;global&gt; section defines key mappings that apply everywhere in Kodi unless they are overridden by a mapping in another section. The &lt;Home&gt; section defines key mappings that apply only when you're at the Kodi home screen. Other sections define mappings that apply to other screens, for example the &lt;FullScreenVideo&gt; section defines mappings that apply when you're watching a video full screen. The easiest way to get a list of all the section names is to open the system keyboard.xml in Notepad and look through it.

The key mappings have the form:

&lt;keyname mod="modifiers"&gt;SomeAction&lt;/keyname&gt;

As with the section names, to see possible key names look at the system keyboard.xml. In fact copying and pasting from the system keyboard.xml is probably the easiest way to construct your custom keyboard.xml.

You need only only include the mod="modifiers" if you want to combine the key with a keyboard modifer like control, or. For example: &lt;d&gt;Notification(Keypress, You pressed D!, 3)&lt;/d&gt;

configures the key to execute the action Notification(Keypress, You pressed D!, 3) while:

&lt;d mod="ctrl,alt"&gt;Notification(Keypress, You pressed ctrl-alt-D!, 3)&lt;/d&gt;

configures a keypress to execute the action. Incidentally the Notification action displays a little message at the bottom right of the screen. This can be useful for testing your key mappings.

An example
This example is going to be a bit contrived, but after all it's only intended as an example of how you might make some more useful key mapping.

Media Center remote controls usually have a button labelled, and when you press it this button usually sends a keystroke. In this example we'll configure the Guide button to display the Info screen except when playing a video, when we'll configure it to show the OSD (On Screen Display). So lets start by defining a global mapping for to display Info:

&lt;keymap&gt; &lt;global&gt; &lt;keyboard&gt; &lt;g mod="ctrl"&gt;Info&lt;/g&gt; &lt;/keyboard&gt; &lt;/global&gt; &lt;/keymap&gt;

From the previous section it should be obvious what this key mapping does so I won't dwell on it further. If you create a userdata keyboard.xml with this mapping and run Kodi you should find that whenever you press it displays the Info screen.

However this will make display Info when playing a video, and we want it to display the OSD instead. To achieve this modify the keyboard.xml to:

&lt;keymap&gt; &lt;global&gt; &lt;keyboard&gt; &lt;g mod="ctrl"&gt;Info&lt;/g&gt; &lt;/keyboard&gt; &lt;/global&gt; &lt;FullScreenVideo&gt; &lt;keyboard&gt; &lt;g mod="ctrl"&gt;OSD&lt;/g&gt; &lt;/keyboard&gt; &lt;/FullScreenVideo&gt; &lt;/keymap&gt;

The global action for is still Info, but the  section overrides the global mapping when playing a video and configures  to display the OSD instead.

Debugging
It's easy to make mistakes when writing a keyboard.xml file, and if Kodi finds an error in your keyboard.xml it will simply stop processing it, leaving you wondering why your key mappings aren't working. To check for errors in your keyboard.xml turn debug logging on (in the Settings screen go into System then Debugging and enable the Enable debug logging option), then close Kodi then start it and close it again. Now look in %APPDATA%\Kodi and you'll find a file called kodi.log. Open this file in Notepad and you should be able to find the error.

For example, take the keymapping above but suppose I mistype:

&lt;g mod="ctrl"&gt;Info&lt;/g&gt;

as

&lt;g mod="ctrl"&gt;Info&lt;/&gt;

that is I missed the "" in &lt;/g&gt;. If I run Kodi and then look at kodi.log I find:

INFO: Loading special://masterprofile/keymaps/keyboard.xml ERROR: Error loading keymap: special://masterprofile/keymaps/keyboard.xml, Line 4 Error reading end tag.

Finally, the Notification action can useful when you're testing your key mappings as it displays a little message at the bottom right of the screen to confirm Kodi has processed the keystroke. A typical example would be:

&lt;g mod="ctrl"&gt;Notification(This is the title, This is the message, 3)&lt;/g&gt;

and the last argument, 3, is the number of seconds to display the message.