how to assign another modifier to Alt key for X11?
Solution 1
(This answer is about xmodmap only. I'm sure it's possible to do this with XKB, I just don't know how.)
Modifiers and keysyms are assigned independently. But you get strange effects if you don't set them consistently. I think all you're missing is the add
command to assign a modifier to Meta_L
, though you may also need to clear and reassign the modifier keys. You may replace Mod1
and Mod2
by Mod3
, Mod4
and Mod5
: they are interchangeable, just make sure you don't use one for two different purposes.
clear Mod1
clear Mod2
remove Lock = Caps_Lock
keysym Caps_Lock = Alt_L
keysym Alt_L = Meta_L
add Mod1 = Alt_L Alt_R
add Mod2 = Meta_L Meta_R
Solution 2
Editing keycodes approach
I found the answer on Polish Ubuntu forum. Now one does not edit "xfree86" file but "evdev" file. This way you can make permanent changes which work for all layouts.
Example (on Gilles request) -- log in as root, go to
/usr/share/X11/xkb/keycodes
make a backup of "evdev" file, and then edit it. Change the codes to your liking, for example in my case:
<LALT> = 66; // when I press CapsLock (66 code) system will report pressing LeftAlt
<LMTA> = 64; // I will get LeftMeta when pressing LeftAlt (64 code)
You can find what the codes by executing command "xev", but you can simply look at original "evdev" file.
Log out, log in, enjoy your new keyboard :-).
Solution 3
try this:
key <CAPS> { [ Alt_L, Alt_L ] };
key <LALT> { [ Meta_L, Meta_L ] };
modifier_map Mod1 { Alt_L, Alt_R };
modifier_map Mod5 { Meta_L, Meta_R };
note the two level definitions. That is because the default for LALT is [ Alt_L, Meta_L ]; if you don't redefine the second one, it will remain Meta_L.
Look at this answer on xkb for how to redefine some keys without need to edit main default files (thus, no need to be root). In such case, you will need in the local symbols file a small section as:
partial modifier_keys
xkb_symbols "capsasalt_altasmeta" {
replace key <CAPS> { [ Alt_L, Alt_L ] };
replace key <LALT> { [ Meta_L, Meta_L ] };
modifier_map Mod1 { Alt_L, Alt_R };
modifier_map Mod5 { Meta_L, Meta_R };
};
Related videos on Youtube
greenoldman
I am interested in such topics: usability and accessibility of user interfaces, design of programming languages, natural language processing, machine translation, data mining. Currently I am developing compiler and programming language Skila -- read more at aboutskila.wordpress.com.
Updated on September 17, 2022Comments
-
greenoldman over 1 year
Aim:
I would like to assign Alt to CapsLock-key, and Meta to Alt-key. But in such way, Alt-key would no longer be recognized as Alt, and CapsLock-key would no longer be recognized as CapsLock.
openSUSE 11.4
Previously:
openSUSE 11.1 -- since I am the only user of my computer I "simply" edited the /usr/share/X11/xkb/keycodes/xfree86 file and it worked without problem. Keys were wired to their symbols at the lowest level.
Problems:
-
xkb
I created a variant of Polish layout (pl_ext) which (for test) consists of such entries:
key <CAPS> { [ Alt_L ] }; modifier_map Mod1 { Alt_L, Alt_R };
However this does simply nothing, CapsLock-key in xev is recognized (symbol) as Alt_L, but when I press CapsLock-key it behaves like CapsLock (e.g. assuming you have File in menu, Alt+F should open this menu, it does not).
Question: How to define a layout file to set CapsLock-key as Alt, and Alt-key as Meta?
Edit: Half of success here! Now I have such entries:
key <CAPS> { [ Alt_L ] }; modifier_map Mod1 { <CAPS> }; // this is a difference
and this works as desired. This does not:
key <LALT> { [ Meta_L ] } ; key <RALT> { [ Meta_R ] } ; modifier_map Mod5 { <LALT>,<RALT> };
Alt-keys are recognized as Meta by xev, but I still can open the menus with Alt+F, switch windows, and I shouldn't. And on the other hand I cannot enter any national character, and I should.
-
xfree86
But now this does not work, I guess other file table is read instead of xfree86.
Question: How to find out which keycode table file is used by system (X/Gnome)?
Edit: The best option for me -- editing keycode tables -- was solved by macias's answer!
-
xmodmap
Half of success here. This part works as desired:
remove Lock = Caps_Lock keysym Caps_Lock = Alt_L
Now, I have trully CapsLock-key which is mapped to Alt. But this:
keysym Alt_L = Meta_L
...does strange thing. xev shows that Alt-key is mapped to Meta, but when I press Alt+F (this should be mapped to Meta+F --> doing nothing) the File menu is opened. What's more, when I press Alt+Tab, I get window-switcher (I should not -- Alt is Meta now).
Question: How to "delete" old behavior for Alt-key?
Edit: This part is solved by Gilles's answer.
Summary:
Answering any question would (hopefully) solve my problem, however I prefer using xkb entirely because I could then pack all the files for xkb and change layout in one place. Thank you in advance for any help!
-
Gilles 'SO- stop being evil' about 13 yearsIf you've found an answer, please post it as such, and give enough information for other people who might be interested. (It's perfectly fine to answer your own question if you found the answer elsewhere or by yourself; you can even mark your answer as accepted if you like.)
-
greenoldman about 13 years@Gilles, ok, but I keep your answer as the answer, just to prevent possible accusation I did it for points. Besides, you help was very valuable for me.
-
Gilles 'SO- stop being evil' about 13 yearsYou don't get points for accepting your own answer anyway. (You do get points for the votes on your own answer, which is fair since you did the work.) The most important thing is to have the information clearly presented. There are now two answers to this question, one with xmodmap and one with xkb, and the page would read clearer if they were both posted as answers.
-
-
greenoldman about 13 yearsBig thanks. So indeed, I used Mod5 instead of Mod2 for Meta, and I can now write as expected (I use meta+key for many national characters). But oddly, now I have problem with CapsLock-key again -- it is binded to... Meta. I guess why, because once there is binding to Alt, but then Alt is redefined. So how to make a difference and once insist on code, and second time on symbol? I would like not to use raw keycodes because they could change from keyboard to keyboard. I cannot swap the order of keysyms, because in the last line I would get error.
-
Gilles 'SO- stop being evil' about 13 yearsThe problem with
keysym Caps_Lock = Alt_L
is that if you run it twice, it puts the system back into an undesired state. That's why I use keycodes; they vary between operating systems and architectures (not between keyboards), but not that often. -
greenoldman about 13 yearsThank you for explanation, I guessed right then :-). If you don't mind I will keep it a bit longer as an unanswered question, maybe somebody could solve my problem at xkb level, which would be more flexible (switching layouts kills xmodmaps).
-
Cody Craven over 7 yearsHuge thanks, kept fighting with symbols/pc with no success.
-
Daniel over 5 yearsCan you preserve the behavior of LALT? That is, not changing it to LMTA? I tried but apparently you cannot have two assignments of the form <LALT> = ...