zxsoftware
Posts: 1
Joined: Sun Jan 27, 2013 8:32 am

Mapping a custom keyboard

Sun Jan 27, 2013 9:55 pm

Hi all,

I have recently been working on the hardware side of a project which now works fine. However I now have a software problem to overcome regarding keyboard mapping.

I have connected a Sinclair Spectrum 48k keyboard via a USB keyboard controller to my Raspi. All the keys work but return completely the wrong letters/numbers to the screen. This is of course exactly what I expected to happen but I now need to remap the keyboard back to something sane. By following the man pages I have worked out how to change the remapping from (say) UK to German layouts but I can’t find anywhere how I remap on a more fundamental basis, i.e. mapping each individual keypress.

At the moment if I press “a” on the keyboard it will return “Y” to the screen and if I press the shift key it will then return “d”. (BTW – the shift lives on the “h” key at the moment).

The spectrum keyboard works on a 10 x 4 matrix consisting of A – Z, 0 – 9, 2 x shift keys, Return and Space keys. This means realistically I will need one shift key for lower case / capitals and will need to use the second to shift letters / numbers to symbols (i.e. +, =, /, \, ect). This of course means that most keys will have 3 functions (Upper case / Lower case / Symbol).

There is of course the problem that I cannot really work on the booted image for obvious reasons so I will need to work by editing the files (or whatever I need to do) on a separate machine. I need this remapping to work at both the command line and in the x GUI levels.

So my question is how do I do this type of remapping?. I only have a very limited understanding of Linux but I am more than happy to put in the work researching and learning if somebody can point me in the direction of some documentation or better still some sort of tutorial on this subject.

Thanks in anticipation.

Moderators - If you feel this post is in the wrong section please move it. I wasn't sure if this was a beginners / advanced OS question or a case project question.... or something else.

User avatar
rpdom
Posts: 15597
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Mapping a custom keyboard

Mon Jan 28, 2013 5:24 am

I can't remember how to create a custom keyboard map in the CLI environment at the moment, but you could look at xmodmap for the GUI.

Jetman68
Posts: 1
Joined: Sat Apr 06, 2013 7:13 pm

Re: Mapping a custom keyboard

Sat Apr 06, 2013 7:56 pm

Hello ZX,
I remapped the keys using Xmodmap. I got help from EdinburghRetroPC see http://www.youtube.com/watch?v=m0ZHmZkv3CU.

You need Xev & Xmodmap. Please note that you need to be connected to the internet. I had to install this by going into the xterminal console (the dos type box) and I typed sudo apt-get xmodmap (I'm not a 100% on te exact wording, as it was a few months ago). As I remember it advised me that it was no longer available, but it was in a different package. I installed what was recommended, which contained both commands xev & xmodmap.

I then typed the command xev, which gave me the keycode for each key.

EdinburghRetroPC kindly gave me this file:

#!/bin/bash
# remap keycodes for ZX Spectrum USB Keyboard

# first, sort out CAPS SHIFT and SYMBOL SHIFT as a shift and AltGr key..
xmodmap -e "keycode 100 = Shift_L"
xmodmap -e "keycode 13 = Control_R"
xset -r 100 # prevent new shift from autorepeating
xset -r 13 # prevent new AlrGr from autorepeating
xmodmap -e "add shift = Shift_L"
xmodmap -e "remove control = Control_R"
xmodmap -e "add mod5 = Control_R"

# one key maps to 'Caps Lock', need to remove this function to get it working
xmodmap -e "remove lock = Caps_Lock"

# Right, standard keys now... top row first, left to right...
xmodmap -e "keycode 25 = 1 question exclam exclam exclam"
xmodmap -e "keycode 27 = 2 2 at at at"
xmodmap -e "keycode 24 = 3 sterling numbersign numbersign numbersign"
xmodmap -e "keycode 30 = 4 4 dollar dollar dollar"
xmodmap -e "keycode 33 = 5 5 percent percent percent"
xmodmap -e "keycode 48 = 6 6 ampersand ampersand ampersand"
xmodmap -e "keycode 43 = 7 7 apostrophe apostrophe apostrophe"
xmodmap -e "keycode 9 = 8 parenleft parenleft parenleft parenleft"
xmodmap -e "keycode 42 = 9 9 parenright parenright parenright"
xmodmap -e "keycode 94 = 0 BackSpace underscore underscore underscore"
# second row....
xmodmap -e "keycode 66 = q Q Left Left Left"
xmodmap -e "keycode 28 = w W W W W"
xmodmap -e "keycode 23 = e E E E E"
xmodmap -e "keycode 29 = r R less less less"
xmodmap -e "keycode 34 = t T greater greater greater"
xmodmap -e "keycode 51 = y Y Y Y Y"
xmodmap -e "keycode 58 = u U Up Up Up"
xmodmap -e "keycode 52 = i I I I I"
xmodmap -e "keycode 55 = o O semicolon semicolon semicolon"
xmodmap -e "keycode 53 = p P quotedbl quotedbl quotedbl"
# third row....
xmodmap -e "keycode 39 = a A Left Left Left"
xmodmap -e "keycode 41 = s S Right Right Right"
xmodmap -e "keycode 38 = d D Down Down Down"
xmodmap -e "keycode 44 = f F F F F"
xmodmap -e "keycode 47 = g G asciitilde asciitilde asciitilde"
xmodmap -e "keycode 20 = h H bar bar bar"
xmodmap -e "keycode 15 = j J minus minus minus"
xmodmap -e "keycode 49 = k K plus plus plus"
xmodmap -e "keycode 14 = l L equal equal equal"
xmodmap -e "keycode 67 = Return Return Return Return Return"
# bottom row...
xmodmap -e "keycode 56 = z Z colon colon colon"
xmodmap -e "keycode 131 = x X sterling sterling sterling"
xmodmap -e "keycode 57 = c C question question question"
xmodmap -e "keycode 61 = v V slash slash slash"
xmodmap -e "keycode 19 = b B asterisk asterisk asterisk"
xmodmap -e "keycode 16 = n N comma comma comma"
xmodmap -e "keycode 10 = m M period period period"
xmodmap -e "keycode 11 = space space Escape Escape Escape"

I replaced all the keycodes that corresponded to my set up. I then saved this as text document and copied and pasted it into xterminal.

The only problem with this solution, is that it takes a few minutes to work. You have to do this every time the system reboots. It is possible to set it up form boot, but I don't no how (I'm a noob in Linux) and I don't like the idea of adding 3 minutes to every bootup. I wish there was a better solution, such as being able to create your own keyboard file.

I hope this helps.

P.S. If you find a better solution, please advise me.

Return to “Advanced users”