You are not logged in.

#1 2014-08-04 09:32:04

Franek
Member
Registered: 2010-05-16
Posts: 100

How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

Playing around with the non-modal text editor ne, I found myself wondering if one could simulate a modal interface (like vim has) by having something like "Ctrl Lock" or "Alt Lock" (analogous to Caps Lock) and thus a "ctrl mode"/"alt mode". I would like to try this, but I am not sure how to make it happen.

My best guess is that this could be accomplished via XKB, but its documentation looks pretty intimidating. Is anyone here proficient in XKB and could point me in the right direction?

Other ideas:
– Is this something that a terminal emulator could do? Is there already one that can do it?
– Or a shell?
– For console applications, would it be possible to have a small wrapper that did nothing but intercept keyboard input and just pass everything through to the wrapped process, except that it would take into account the desired lockable modifier key? (I am thinking of tput here, but from my first search it appears that it doesn't do this sort of thing.)

Offline

#2 2014-08-04 11:49:08

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

Sorry, I don't have an answer to your question, but if you're looking for a phrase to search for then I believe that functionality is called "sticky keys".

Offline

#3 2014-08-04 12:33:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

I don't know how to do it off the top of my head, but I am pretty sure an X utility like XKB or xmodmap would be the way to go.  I'm mostly replying though to give some answers to the other ideas:

Franek wrote:

– Is this something that a terminal emulator could do? Is there already one that can do it?

Yes.  No.  A terminal emulator definitely could do this - but I don't know of any that currently do, and I'd be very surprised if any existed.  You could hack an existing one to do this, but this hardly seems an efficient route.

Franek wrote:

– Or a shell?

No.  Shells don't work with keys.  They require other software (or hardware for the oldschool) to interface with keyboards and monitors.  A shell just works with data streams (stdin, stdout, stderr, and any arbirtrary number of numbered file pointers).

I suppose one *could* hack a shell into doing some translation/preprocessing of the input (this is effectively what aliases do).  So you could have a shell builtin command that would activate a translation that would map each input byte (or other size chunk) into something else.  But this would be a real pain.

Franek wrote:

– For console applications, would it be possible to have a small wrapper

Absolutely.  In one (dead) version of my window manager, I made something like this to replicate a vim-like modal window interface using key chaining.  It would "remember" when the Mod4 key was pressed, for example, then everything after that was considered part of a command that it would interpret (until a complete command was recieved or an escape key hit).  But in my case, this was just intercepting those key presses, it didn't need to filter them and re-send them on to a target.  This latter part is what your wrapper would need to do - this could have some drawbacks.

So all in all, many things are possible.   But of all the possible approaches, only a few would be reasonably easy to implement and reasonably sensible to use in the longrun.  XKB and/or xmodmap approaches are at the very top of this list.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2014-08-07 09:28:18

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

@Trilby: Thank you for the elaborate input!

@drcouzelis: "sticky keys" normally means that modifiers remain active only until the next keypress, while I want a way to lock them in active state for an arbitrary number of keypresses. But still, "sticky keys" was a worthwile input for the search engine.

The program xkbset, which can be used to set XKB options on the fly, has a function that comes very close to what I desire:

 $ xkbset h
Usage: To set or unset various options:

  xkbset <options>

where <options> may be all or any of (the '-' switches the feature off, 
otherwise it is switched on):

[…]

To switch sticky keys on or off, and optionally set or reset:
() two keys pressed at the same time stops sticky keys;
() a modifier pressed twice will be locked:
    
    [-]{sticky|st} [[-]twokey|[-]latchlock]...

[…]

I cannot select specific modiers to be affected with xkbset, but that would be fine with me. Sadly, it didn't completely work for me:
– It doesn't affect Alt at all for me.
– The effect seems to go away as soon as I change the window. (Maybe I need to invoke xkbset from .xinitrc for its effects to persist throughout the session? I will try that.)


So I went on and tried to configure XKB manually. I got up to a certain point without studying the documentation in-depth, but now I have landed on an error message that seems to suggest to me that I would have to get to a better understanding about the layout files before I could understand it. Here is what I did:

1. Got my current XKB configuration:

xkbcomp :0 output.xkb

2. Editet output.xkb to include the following at the end of the xkb_compatibility section:

//Hoping this makes the ^-key into a Ctrl-Lock key:
    interpret TLDE {
        action= LockMods(modifiers=Control);
    };

3. Then I recompiled the configuration back into the running X server:

$ xkbcomp out.xkb :0
Warning:          Compat map for group 2 redefined
                  Using new definition
Warning:          Compat map for group 3 redefined
                  Using new definition
Warning:          Compat map for group 4 redefined
                  Using new definition
Error:            Couldn't lookup keysym
                  Symbol interpretation ignored
Warning:          No symbols defined for <AB11> (keycode 97)
Warning:          No symbols defined for <JPCM> (keycode 103)
Warning:          No symbols defined for <I120> (keycode 120)
Warning:          No symbols defined for <AE13> (keycode 132)
Warning:          No symbols defined for <I149> (keycode 149)
Warning:          No symbols defined for <I154> (keycode 154)
Warning:          No symbols defined for <I168> (keycode 168)
Warning:          No symbols defined for <I178> (keycode 178)
Warning:          No symbols defined for <I183> (keycode 183)
Warning:          No symbols defined for <I184> (keycode 184)
Warning:          No symbols defined for <FK19> (keycode 197)
Warning:          No symbols defined for <FK24> (keycode 202)
Warning:          No symbols defined for <I217> (keycode 217)
Warning:          No symbols defined for <I219> (keycode 219)
Warning:          No symbols defined for <I221> (keycode 221)
Warning:          No symbols defined for <I222> (keycode 222)
Warning:          No symbols defined for <I230> (keycode 230)
Warning:          No symbols defined for <I247> (keycode 247)
Warning:          No symbols defined for <I248> (keycode 248)
Warning:          No symbols defined for <I249> (keycode 249)
Warning:          No symbols defined for <I250> (keycode 250)
Warning:          No symbols defined for <I251> (keycode 251)
Warning:          No symbols defined for <I252> (keycode 252)
Warning:          No symbols defined for <I253> (keycode 253)

The "Error" part is where I am currently stuck. It tells me that my changes were ignored (I no that it's about them because it goes away when I comment them out), but I don't yet understand why.

Offline

#5 2014-08-07 09:31:32

Franek
Member
Registered: 2010-05-16
Posts: 100

Re: How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

By the way, in the console this can apparently easily be done using loadkeys. Quoting http://www.tldp.org/HOWTO/Keyboard-and- … TO-15.html:

"Can the Shift, Ctrl and Alt keys be made to behave as toggles?"

Yes, after saying

            % loadkeys
            keymaps 0-15
            keycode 29 = Control_Lock
            keycode 42 = Shift_Lock
            keycode 56 = Alt_Lock
            %

the left Control, Shift and Alt keys will act as toggles. The numbers involved are revealed by showkey (and usually are 29, 97, 42, 54, 56, 100 for left and right control, shift and alt, respectively), and the functions are Control_Lock, Shift_Lock, Alt_Lock, ALtGr_Lock.

Haven't tried this yet, but I need it in X anyway.

Offline

#6 2014-11-01 19:02:33

PieterGen
Member
From: Groningen, NL, EU
Registered: 2012-01-18
Posts: 59

Re: How to make modifier keys lockable? (like Caps Lock for Ctrl or Alt)

Not an answer, but a hint: search on the Colemak forum. i have seen this topic discussed there (a guy named Dreymar posts regularly about this).

Offline

Board footer

Powered by FluxBB