Reversing role of pressing shift+number

Solution 1:

This is actually pretty easy to achieve using xmodmap. Simply make a new file, with the following:

keycode  10 = exclam 1 exclam 1
keycode  11 = at 2 at 2
keycode  12 = numbersign 3 numbersign 3
keycode  13 = dollar 4 dollar 4
keycode  14 = percent 5 percent 5
keycode  15 = asciicircum 6 asciicircum 6
keycode  16 = ampersand 7 ampersand 7
keycode  17 = asterisk 8 asterisk 8
keycode  18 = parenleft 9 parenleft 9
keycode  19 = parenright 0 parenright 0

Save it in the root of your home directory as .Xmodmap (capitalization is important). Now, just log off and back in (you may have to reboot), and your keys will be swapped. Keep reading for how I came up with this. The information I used to figure this out came from here. Fire up a terminal and type the following command:

xmodmap -pke

This will spit out a bunch of keycode setting/value pairs, like this:

......
keycode  27 = r R r R
keycode  28 = t T t T
keycode  29 = y Y y Y
keycode  30 = u U u U
keycode  31 = i I i I
keycode  32 = o O o O
keycode  33 = p P p P
keycode  34 = bracketleft braceleft bracketleft braceleft
keycode  35 = bracketright braceright bracketright braceright
keycode  36 = Return NoSymbol Return
keycode  37 = Control_L NoSymbol Control_L
keycode  38 = a A a A
keycode  39 = s S s S
keycode  40 = d D d D
keycode  41 = f F f F
keycode  42 = g G g G
keycode  43 = h H h H
keycode  44 = j J j J
keycode  45 = k K k K
......

The keycode is the key on your physical keyboard, and the value on the right of the equals sign is what gets sent to your PC. The reason there are four is the first is plain, the second one is with shift pressed, and the third (plain) and forth (shift) seem to be enabled once you click the Scroll Lock button. As you can see, this give you a lot of flexibility to do whatever you can dream up to modify your keyboard.

Now, this will only work when you are logged in to X (as in the GUI), hence the name. If you fire up a terminal in Gnome, Unity, etc. you are still good using Xmodmap, but if you want the same functionality in the console (i.e. after pressing CTRL+ALT+1, sshing into a box, working on a server, or a PC with no GUI) you will need to use loadkeys. Although the general idea is the same, loadkeys offers much more functionality and granularity, so it is slightly more complicated. Now, these commands all MUST be done from the actual console, they can not be done with a terminal emulator, or you will receive an error, like this: Couldn't get a file descriptor referring to the console.

First we will need to create our default keymap, like this:

dumpkeys > ~/my_keymap.map

It would be a good idea to make a copy of this in case you really mess it up. Now, we just need to edit it, with our changes. Each key definition looks like this:

keycode   2 = one              exclam           one              one             
    alt keycode   2 = Meta_one        
    shift   alt keycode   2 = Meta_exclam     
    altgr   alt keycode   2 = Meta_one        
    shift   altgr   alt keycode   2 = Meta_one        
    shiftl  keycode   2 = one             
    shift   shiftl  keycode   2 = exclam          
    altgr   shiftl  keycode   2 = one             
    shift   altgr   shiftl  keycode   2 = one             
    alt shiftl  keycode   2 = Meta_one        
    shift   alt shiftl  keycode   2 = Meta_exclam     
    altgr   alt shiftl  keycode   2 = Meta_one        
    shift   altgr   alt shiftl  keycode   2 = Meta_one        
    shiftr  keycode   2 = one             
    shift   shiftr  keycode   2 = exclam          
    altgr   shiftr  keycode   2 = one             
    shift   altgr   shiftr  keycode   2 = one             
    alt shiftr  keycode   2 = Meta_one        
    shift   alt shiftr  keycode   2 = Meta_exclam     
    altgr   alt shiftr  keycode   2 = Meta_one        
    shift   altgr   alt shiftr  keycode   2 = Meta_one        
    shiftl  shiftr  keycode   2 = one             
    shift   shiftl  shiftr  keycode   2 = exclam          
    altgr   shiftl  shiftr  keycode   2 = one             
    shift   altgr   shiftl  shiftr  keycode   2 = one             
    alt shiftl  shiftr  keycode   2 = Meta_one        
    shift   alt shiftl  shiftr  keycode   2 = Meta_exclam     
    altgr   alt shiftl  shiftr  keycode   2 = Meta_one        
    shift   altgr   alt shiftl  shiftr  keycode   2 = Meta_one        
    ctrll   keycode   2 = one             
    shift   ctrll   keycode   2 = exclam          
    altgr   ctrll   keycode   2 = one             
    shift   altgr   ctrll   keycode   2 = one             
    alt ctrll   keycode   2 = Meta_one        
    shift   alt ctrll   keycode   2 = Meta_exclam     
    altgr   alt ctrll   keycode   2 = Meta_one        
    shift   altgr   alt ctrll   keycode   2 = Meta_one        
    shiftl  ctrll   keycode   2 = one             
    shift   shiftl  ctrll   keycode   2 = exclam          
    altgr   shiftl  ctrll   keycode   2 = one             
    shift   altgr   shiftl  ctrll   keycode   2 = one             
    alt shiftl  ctrll   keycode   2 = Meta_one        
    shift   alt shiftl  ctrll   keycode   2 = Meta_exclam     
    altgr   alt shiftl  ctrll   keycode   2 = Meta_one        
    shift   altgr   alt shiftl  ctrll   keycode   2 = Meta_one        
    shiftr  ctrll   keycode   2 = one             
    shift   shiftr  ctrll   keycode   2 = exclam          
    altgr   shiftr  ctrll   keycode   2 = one             
    shift   altgr   shiftr  ctrll   keycode   2 = one             
    alt shiftr  ctrll   keycode   2 = Meta_one        
    shift   alt shiftr  ctrll   keycode   2 = Meta_exclam     
    altgr   alt shiftr  ctrll   keycode   2 = Meta_one        
    shift   altgr   alt shiftr  ctrll   keycode   2 = Meta_one        
    shiftl  shiftr  ctrll   keycode   2 = one             
    shift   shiftl  shiftr  ctrll   keycode   2 = exclam          
    altgr   shiftl  shiftr  ctrll   keycode   2 = one             
    shift   altgr   shiftl  shiftr  ctrll   keycode   2 = one             
    alt shiftl  shiftr  ctrll   keycode   2 = Meta_one        
    shift   alt shiftl  shiftr  ctrll   keycode   2 = Meta_exclam     
    altgr   alt shiftl  shiftr  ctrll   keycode   2 = Meta_one        
    shift   altgr   alt shiftl  shiftr  ctrll   keycode   2 = Meta_one 

As you can see, it offers a lot more functionality/granularity. For each key, just swap the shift and regular values, i.e. here would be the swapped one key:

keycode   2 = exclam              one           exclam              exclam             
    alt keycode   2 = Meta_exclam        
    shift   alt keycode   2 = Meta_one     
    altgr   alt keycode   2 = Meta_exclam        
    shift   altgr   alt keycode   2 = Meta_exclam        
    shiftl  keycode   2 = exclam             
    shift   shiftl  keycode   2 = one          
    altgr   shiftl  keycode   2 = exclam             
    shift   altgr   shiftl  keycode   2 = exclam             
    alt shiftl  keycode   2 = Meta_exclam        
    shift   alt shiftl  keycode   2 = Meta_one     
    altgr   alt shiftl  keycode   2 = Meta_exclam        
    shift   altgr   alt shiftl  keycode   2 = Meta_exclam        
    shiftr  keycode   2 = exclam             
    shift   shiftr  keycode   2 = one          
    altgr   shiftr  keycode   2 = exclam             
    shift   altgr   shiftr  keycode   2 = exclam             
    alt shiftr  keycode   2 = Meta_exclam        
    shift   alt shiftr  keycode   2 = Meta_one     
    altgr   alt shiftr  keycode   2 = Meta_exclam        
    shift   altgr   alt shiftr  keycode   2 = Meta_exclam        
    shiftl  shiftr  keycode   2 = exclam             
    shift   shiftl  shiftr  keycode   2 = one          
    altgr   shiftl  shiftr  keycode   2 = exclam             
    shift   altgr   shiftl  shiftr  keycode   2 = exclam             
    alt shiftl  shiftr  keycode   2 = Meta_exclam        
    shift   alt shiftl  shiftr  keycode   2 = Meta_one     
    altgr   alt shiftl  shiftr  keycode   2 = Meta_exclam        
    shift   altgr   alt shiftl  shiftr  keycode   2 = Meta_exclam        
    ctrll   keycode   2 = exclam             
    shift   ctrll   keycode   2 = one          
    altgr   ctrll   keycode   2 = exclam             
    shift   altgr   ctrll   keycode   2 = exclam             
    alt ctrll   keycode   2 = Meta_exclam        
    shift   alt ctrll   keycode   2 = Meta_one     
    altgr   alt ctrll   keycode   2 = Meta_exclam        
    shift   altgr   alt ctrll   keycode   2 = Meta_exclam        
    shiftl  ctrll   keycode   2 = exclam             
    shift   shiftl  ctrll   keycode   2 = one          
    altgr   shiftl  ctrll   keycode   2 = exclam             
    shift   altgr   shiftl  ctrll   keycode   2 = exclam             
    alt shiftl  ctrll   keycode   2 = Meta_exclam        
    shift   alt shiftl  ctrll   keycode   2 = Meta_exclam     
    altgr   alt shiftl  ctrll   keycode   2 = Meta_exclam        
    shift   altgr   alt shiftl  ctrll   keycode   2 = Meta_exclam        
    shiftr  ctrll   keycode   2 = exclam             
    shift   shiftr  ctrll   keycode   2 = one          
    altgr   shiftr  ctrll   keycode   2 = exclam             
    shift   altgr   shiftr  ctrll   keycode   2 = exclam             
    alt shiftr  ctrll   keycode   2 = Meta_exclam        
    shift   alt shiftr  ctrll   keycode   2 = Meta_one     
    altgr   alt shiftr  ctrll   keycode   2 = Meta_exclam        
    shift   altgr   alt shiftr  ctrll   keycode   2 = Meta_exclam        
    shiftl  shiftr  ctrll   keycode   2 = exclam             
    shift   shiftl  shiftr  ctrll   keycode   2 = one          
    altgr   shiftl  shiftr  ctrll   keycode   2 = exclam             
    shift   altgr   shiftl  shiftr  ctrll   keycode   2 = exclam             
    alt shiftl  shiftr  ctrll   keycode   2 = Meta_exclam        
    shift   alt shiftl  shiftr  ctrll   keycode   2 = Meta_one     
    altgr   alt shiftl  shiftr  ctrll   keycode   2 = Meta_exclam        
    shift   altgr   alt shiftl  shiftr  ctrll   keycode   2 = Meta_exclam

The reason for the extra granularity, is because you tend to use a lot more keyboard shortcuts when using the console, due to it's nature.

Now, to test your changes, you can use the following command:

loadkeys ~/my_keymap.map

Play around, use some programs you usually use, and make sure it works the way you want. If you messed up, you can just use the loadkeys command to fall back to the default. To make it permanent, you can add the command to the end of your ~/.bashrc file.