Re: Registering/capturing shortcut keys in Linux?
- Posted by irv Nov 29, 2017
- 1245 views
Welcome!
There are no "signals" when using text-mode programs, so you'll need to set up your own loop which checks for keypresses, and handles those you are interested in. The following will show how to detect the common ctl-key combinations such as are used in vi, nano, etc.
atom key while 1 do key = get_key() if key != -1 then if key > 0 and key < 27 then printf(1," ctl-%s %d",{key+64,key}) -- for demo purposes; end if switch key do case 5 then -- do stuff with ctl-e; case 10 then puts(1,key) -- this is the enter key, might as well use it; case 23 then -- do stuff with ctl-w; case 127 then puts(1,{8,32,8}) -- destructive backspace; -- etc case else if key > 26 then puts(1,key) end if -- send to screen; end switch end if end while -- ctl-a thru ctl-z return 1..26 (case insensitive)
Beware! this will not change unix keys that already are assigned, such as ctl-z, which puts your program into the background, or ctl-c, which will end your program execution, nor will this simple routine handle alt-keys, which are actually esc- sequences. More complex code is required to handle those.
If you type stty -a in a terminal, you'll see a list of the ctl-keys that are already used.