5

How can I map the spanish eñe letter to a key combination? What application is suitable for this purpose (xmodmap, xbindkeys, ...)?

When I press Caps Lock + n, it should type ñ

When I press Caps Lock + N, it should type Ñ

I'd like to do this without switching keyboard layouts; I want to use only the English keyboard layout.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
xralf
  • 15,415
  • Do you want Caps Lock to also have the caps lock effect? Do you also have an AltGr key (i.e. a key that makes some character keys insert a different character, never mind that it's the right Alt key or not)? – Gilles 'SO- stop being evil' Apr 29 '11 at 22:37
  • Caps Lock can lose its effect. Or it can be mapped to Fn key + n or something what is comfortable to press and not colliding (Alt is colliding and Win key is already in use). – xralf Apr 30 '11 at 13:25

3 Answers3

6

Not exactly what you want, but instead of Caps Lock you can use AltGr:

AltGr + n producing ñ

AltGr + N producing Ñ

To get that with xmodmap you must know the keycode which produces the N. This can be done with:

xmodmap -pke | grep "n N n N"

where you may get something like: keycode 57 = n N n N n N. This means that the keycode 57 represents the n key.

Then, you remap this keycode, to produce the ñ and Ñ with:

xmodmap -e "keycode  57 = n N n N ntilde Ntilde"

(with this command, the fifth position after the = is produced when you type that key in combination with AltGr, and the sixth position when AltGr and Shift are pressed together).

If this woks, you can make it to run automatically after login.

nozimica
  • 884
  • 8
  • 24
  • Thank you but this doesn't work. When I press AltGr + n it prints :. And it would be better to map it with caps lock or other key because AltGr would conflict in many applications. – xralf Apr 29 '11 at 17:27
  • You show 6 values: n N n N n N .. I only get 4 values in the output from xmodmap (Ubuntu) and it works when I set the 3rd and 4th values, but fails on 5th and 6th... You even mention the words "fitth" and "sixth" ... Is there a difference in implementation of xmodmap from one distro to another? – Peter.O Apr 30 '11 at 18:56
  • @fred: I use ubuntu in two different machines and in both of them I get six values for that command... I don't know why the difference with you. May be it doesn't depend on the distribution but it does according to other circumstances or settings... – nozimica Apr 30 '11 at 22:08
  • @xralf: Are your Alt and AltGr keys identical in behaviour? – nozimica May 01 '11 at 01:09
5

This answer explains how to set this up with xmodmap. Put the configuration snippets below in ~/.Xmodmap, and run xmodmap <~/.Xmodmap to apply them. Depending on your distribution and your setup, ~/.Xmodmap may be loaded automatically when you log in, or you may need to call xmodmap explicitly from ~/.xinitrc or ~/.xsession, or you may need to configure your desktop environment to apply ~/.Xmodmap.

X has a keysym (i.e. an abstract key name) called Mode_switch. On most keyboard mappings for latin-script languages other than US, this keysym is bound to the key labeled AltGr, which replaces the right Alt key. You can choose to map Mode_switch to a different key such as Caps Lock. On a PC keyboard, the keycode (what the hardware sends) for Caps Lock is 66, so change its binding to Mode_switch, and remove the caps lock modifier from it:

keycode 66 = Mode_switch
clear Lock

You also need to associate a modifier with Mode_switch. There are 5 custom modifiers, Mod1 through Mod5; any will do, but there has to be one. Run xmodmap -pm to see what modifiers are in use, and pick one of the 5 that isn't, then add a line like this to your .Xmodmap:

add Mod3 = Mode_switch

In an xmodmap key specification, the character sent by the key with Mode_switch is in the third column after the = sign, and with Mode_switch+Shift in the fourth column. (The first two columns are for the key with no modifiers and with Shift.) You can use a keysym directive to rebind the key that now sends n regardless of its keycode:

keysym n = n N ntilde Ntilde

The names on the right are in fact keysym names. You can find a list of these in /usr/include/X11/keysymdef.h, e.g. the line #define XK_Ntilde 0x00d1 means there's a keysym called Ntilde that corresponds to Unicode character U+00D1. There are characters that don't have a keysym name; you can use the unicode number instead.

! U+2030 is PER MILLE SIGN, U+2031 is PER TEN THOUSAND SIGN
keysym 5 = 5 percent U2030 U2031

Note that if you're shifting modifiers around on systems of ~2009–2011 vintage, you might run into an X_SetModifierMapping bug. Often, but not always, using clear Lock will work around the bug.

  • This works perfectly. Thanks... very interesting... keysym n = and keycode 57 =, do the same thing, but I suppose that using keysym is more portable... another mystery demystified... (and it is easy to toggle it via a hotkey, to quickly get access the caps-lock again) ... – Peter.O Apr 30 '11 at 18:22
  • This worked only for a while. After a few sentences it swapped n with ntilde. So, I have to press mod_switch + n to type n and n to type ntilde. It happened already three times, so I believe there is something wrong with this mod_switch. – xralf May 01 '11 at 16:34
  • @xralf: Did you add clear Lock? (I forgot it in my original answer.) – Gilles 'SO- stop being evil' May 01 '11 at 16:38
  • @Gilles Actually I used rather keycode 94 = Mode_switch without clear lock. – xralf May 01 '11 at 16:42
  • @xralf: If you rebound a key that isn't CapsLock, you don't need the clear Lock part. But you need another part I'd forgotten (sorry): there must be a modifier associated with Mode_switch. See my edit. – Gilles 'SO- stop being evil' May 01 '11 at 16:53
  • Thank you. I added add Mod3 = Mode_switch after keycode 94 = Mode_switch and will test it. Could you please tell me where you learned about xmodmap to avoid my other questions? I searched for some tutorials but nowhere was good explanation of the principles and basic concepts like mod1..mod5, mod_switch etc. and how to utilize it effectively. – xralf May 01 '11 at 17:05
  • @xralf: I don't remember, it was last century! There's a GUI application xkeycaps that generates a .Xmodmap file, my .Xmodmap is ultimately derived from that. And you can at least find examples on the web or through locate xmodmap. – Gilles 'SO- stop being evil' May 01 '11 at 17:15
  • The new settings ended with error message X Error of failed request: BadValue (integer parameter out of range for operation) Major opcode of failed request: 118 (X_SetModifierMapping) Value in failed request: 0x17 Serial number of failed request: 33 Current serial number in output stream: 33 – xralf May 01 '11 at 17:18
  • @xralf: I'd have to see your .Xmodmap file for a better diagnosis, but I suspect you're affected by this issue. Try the cited workaround. – Gilles 'SO- stop being evil' May 01 '11 at 17:29
  • Here is my xmodmap file. – xralf May 01 '11 at 17:58
  • @xralf: So yes, it looks like you're hit by that bug. Try using clear Lock instead of remove, or the other changes suggested in the workaround. – Gilles 'SO- stop being evil' May 01 '11 at 18:31
  • I tried the workaround but without success. I have found here and here the suggestion for patch but don't know how to use it. Is it suitable to ask another question for it? – xralf May 02 '11 at 06:12
  • @xralf: If you can't make it work with the existing question on that topic, then yes, ask another question. – Gilles 'SO- stop being evil' May 02 '11 at 07:18
  • I will accept it because it's fault of bug. I heard that xkb is better than xmodmap so I will rather try xkb than solving this bug. Please warn readers about this bug in your answer. – xralf May 07 '11 at 13:36
1

If you don't mind having caps lock not work anymore as a 'caps lock' key, you can remap it to an 'altgr-like' status with xmodmap -e 'keysym Caps_Lock = ISO_Level3_Shift'.

Random832
  • 10,666
  • I've never used an AltGr "enabled" keyboard before, but I think that simply setting Caps-Lock to be AlgGr isn't enough... all relevant keys would need to be set to act differntly when AlgGr is pressed... A standard US keyboard mapping is not aware of anything other than ASCII characters. – Peter.O Apr 30 '11 at 18:34
  • Well, you would also have to do the mapping mentioned in the other answer. I thought that would go without saying. – Random832 Apr 30 '11 at 20:52