10

My space-bar button is not working any more so i want to change the behavior of it to another key to simulate space button press, so how to map space button to the right alt/windows key ???

2
  • Thanks, but I can't even type "xmodmap -pm" because I can't make the space. Usually I use CTRL-V to paste a space but it doesn't work in DOS. Any workarounds?
    – user309703
    Jul 27, 2014 at 16:31
  • 1st thing it's not called dos in Linux it's (terminal) 2nd thing it's (CTRL + SHIFT + V) to baste in terminal Aug 17, 2014 at 19:41

2 Answers 2

11

To make major changes to your keyboard, including remapping characters and changing modifiers, you need to use the xmodmap utility - see man xmodmap. There are a couple ways to execute the changes:

  • either from the command line

    xmodmap -e "your commands"
    
  • or write the commands to a file like .xmodmaprc, then execute it in your login items or your .bashrc with

    xmodmap .xmodmaprc
    

    If you use the file ~/.xmodmap it will be executed automatically on login.

Alt and Super (the Windows key) are modifiers, and behave specially to the system. The keys will need to be unmapped from their modifier before you can remap anything else. The space bar is considered a regular key.

Unmapping the modifier

Open up a terminal window (Ctrl-Alt-T, if you're unfamiliar with it).

Run the command xmodmap -pm to get a list of the modifier keys on your system. In the output, the leftmost column is the list of modifiers available to the system. The other columns list the keys bound to these modifiers as pairs by keysym (keycode). For example, one line of my output is

mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd)

The keycodes correspond to physical keys, and can (hypothetically) differ between keyboards. The keysym is the 'name' of a key. If the key is a printable character, the name corresponds to the character printed.

Now you have what you need to unmap a key from its corresponding modifier. I'm going to pretend we're working with Alt_R.

Run the xmodmap command (by one of the methods up at the top)

remove mod1 = Alt_R

Remap the space bar

Next we need to know what the spacebar is. Run xmodmap -pke in your terminal window. This will print out what every key on your keyboard is mapped to. Somewhere in that four or five screens-full is a reference to the space bar. We need its name in order to map a key to it.

Spoiler alert: the spacebar is named space

We still have a key named Alt_R, but it doesn't do anything. We can use its name to remap it to the spacebar. Run this xmodmap command:

keysym Alt_R = space

It might happen while you test these out that you've changed Alt_R's name already. Maybe you accidentally remapped it to the letter 'a'. You don't want to remap 'a' to something else, because you still need one of the keys with that name. If you end up in that situation, you can still refer to Alt_R by its keycode. Remember that on my keyboard this is 0x6c. It might be different for you. You can use this xmodmap command to change it:

keycode 0x6c = space

Good luck!

3
  • this doesn't work for in the terminal (pressing ALT_R in the terminal doesn't produce space). Any idea why? And how can I fix it,thnx.
    – Alex bries
    Jan 5, 2023 at 2:25
  • I would check: does pressing Alt_R in another program produce space? Does mapping a different key (like the letter 'z' or the modifier Alt_L) to space produce space?
    – pconley
    Jan 5, 2023 at 19:39
  • Turns out you need to restart whatever program was running after you do the modification, thnx for the help.
    – Alex bries
    Jan 6, 2023 at 7:52
0

pconley's answer is useful, but only after solving the immediate problem of not being able to generate a whitespace character. I was able to do that, after returning to my house in Mexico after 6 months and finding the b, v, z, space, and quote keys not responding, by typing /?in/sh, then using the tab key as whitespace.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .