XKB – remap arrow keys and preserve shift behaviour to select text

remappingshiftxkb

I realize arrow key remapping is an old problem, however I cannot seem to find a good solution that lets me select text with SHIFT + remapped keys as I would do with the vanilla arrow keys.

For instance, if I remap Caps Lock to ISO_Level3_Shift and set xkb_symbols to read either

key <AC08>  { [         k,          K , Down, Down] };

or

key <AC08>  { type="THREE_LEVEL",
              [         k,          K , Down ]  };

Pressing Shift+CapsLock+K will behave exactly as CapsLock+K (while Shift+Down behaves differently from Down alone).

I had somewhat more success using higher level macro utilities and generating keyboard events (i.e. generate both the shift and the arrow keypresses); hoever that approach has a whole set of different problems – often the UI response to a simulated keypress is different from the "real" keypress, and there are performance problems as well – I can type faster than the thing can handle.

Tl;dr; how can you shift-select using remapped arrow keys under X?

Best Answer

You can use preserve for that: add to you type-definition (e.g. THREE_LEVEL) the line

preserve[LevelThree+Shift] = Shift;

This tells xkb to preserve the shift modifier when figuring out the appropriate level from your modifier combination in the case of LevelThree + Shift. The shift modifier is passed on for use by the toolkit or the application (see also here: using preserve with types).

Note that this has impact for example on how the application interprets keystroke combinations: if you had defined the above preserve and

key <AC08> { type="THREE_LEVEL", [ k, K, kappa ]  };

and you'd press Ctrl + Shift + LevelThree + <AC08> the application would read it as a key combo Ctrl + Shift + kappa (instead of usually Ctrl + kappa) because the shift modifier gets preserved and is passed on to the application.

Related Question