howto recognize the Shift and Alt keys when /pressed\

Polytropon freebsd at
Tue Oct 22 20:10:37 UTC 2013

On Tue, 22 Oct 2013 12:16:23 +0000, Gary Kline wrote:
> Organization: Thought Unlimited.  Public service Unix since 1986.
> Of_Interest: With 27 years  of service  to the  Unix  community.
> On Tue, Oct 22, 2013 at 12:59:01PM +0200, Polytropon wrote:
> > On Mon, 21 Oct 2013 16:49:26 +0000, Gary Kline wrote:
> > > 	since im using curses/ncurses, I just use getch(0.  I.e., 
> > > 
> > > 		c = getch(); 
> > > 
> > > 	which works fine for everything except the modifier keys like ahift
> > > 	and alt and ctl.  if there is another way within X11, let's hear
> > > 	it!
> > 
> > In this case I have to remain silent. As far as I know,
> > there is no way to capture modifier keys with this means
> > as _individual_ key presses.
> > 
> > In case you really want to implement that functionality,
> > you'll have to "go down a few levels" and hook directly
> > into the keyboard driver which, unlike the ncurses library
> > that is tied to the console driver, is able to recognize
> > modifier keys as individual keys. In "upper levels", this
> > information is fully lost (press and release Shift key) or
> > combined with other information (press Shift key, press
> > letter 'k', release both, a capital 'K' will be written
> > to the keyboard buffer that terminal-based applications
> > can then read).
> 	getting dow to the keyboard driver level is what I was going to
> 	do--actually, what I did do way, way back.  there was a crash that
> 	cost me a month+  worth of work and I bought a "clicky" keyboard
> 	for $80.  I would up buying two of them and both got  ruined!

An _excellent_ keyboard which shouldn't be that expensive
is the IBM "Model M" (and similarly constructed ones, e. g.
by BOSCOM). They have a "mechanical click" due to the spring
mechanism in each key.

I remember that some terminals had a built-in "beep" (more
like a click sound) that could be activated for key presses;
I think the DEC vt100 had that feature. A possible idea would
be to add the clicking routine (either directed at the PC
speaker attached to the mainboard, or if needed, aimed at
the sound card / DSP module) as a part of the terminal emu-
lator itself: When it receives a keypress, emit the sound.
Sadly I'm not familiar enough on how this works on FreeBSD,
if the "level is low enough" to find that functionality in
the terminal emulator, or if it would really be easier to
search for that functionality in the keyboard driver because
the terminal emulator already contains "too much abstraction".

> 	I just *barely* understand this python code that the guy has in
> 	"", but I see what he's done (I think) to grab onto the 
> 	modifyer keys::
>   def _setup_lookup(self):
>     """Setup the key lookups."""
>     for name in dir(XK):
>       if name[:3] == "XK_":
>         code = getattr(XK, name)
>         self.keycode_to_symbol[code] = 'KEY_' + name[3:].upper()
>     self.keycode_to_symbol[65027] = 'KEY_ISO_LEVEL3_SHIFT'
>     self.keycode_to_symbol[269025062] = 'KEY_BACK'
>     self.keycode_to_symbol[269025063] = 'KEY_FORWARD'
>     self.keycode_to_symbol[16777215] = 'KEY_CAPS_LOCK'
>     self.keycode_to_symbol[269025067] = 'KEY_WAKEUP'

Hmmm... I did hope that getattr() would be something present
on the system (a C library function or part of ncurses), but
it's a Python function. :-(

Still: Those KEY_ variables are definitions from ncurses found
in /usr/include/curses.h and they partially rely on terminfo
(again, "higher level" terminal emulation stuff). There are
many interesting entries, but Shift as a single key does not
seem to be present (because, as I already mentioned, it is a
modifier key that adds nothing to the input buffer).


#define KEY_LEFT        0404            /* left-arrow key */


#define KEY_SLEFT       0611            /* shifted left-arrow key */

I would assume that the above Python code has something to
do with those naming conventions in ncurses... still I don't
understand how Caps Lock would be captured that way... The
XK_ prefix seems to suggest that those are X defintions for

The KEY_ISO_LEVEL3_SHIFT sounds familiar, it's what many
non-US countries call "Alt Gr" (or "Alt Graph") to add an
extra level of key symbols for national characters or
the "differently placed" curly braces and brackets, the |
character or \ and the like.

> 	Hmmm!  interesting.  do you see any way that I could set up an 
> 	xmod when the computer first boots and the assign the modifiers
> 	to some never-used keys, then make those keys click, or am I
> 	off in the weeds??

That's easy.

Create a file ~/.xmodmaprc with the following content:

! left control
keycode 37 = F25
! left meta
keycode 115 = F26
! left alt
keycode 67 = F27
! left shift
keycode 50 = F28
! caps lock
keycode 66 = F29
! right shift
keycode 64 = F30
! right alt, alt gr
keycode 113 = F31
! right meta
keycode 116 = F32
! compose
keycode 117 = F33

Then add the command

	xmodmap ~/.xmodmaprc

to ~/.xinitrc or ~/.xsession (depending on how you start X).

I'm using a similar approach myself to make the additional
keys of the Sun USB Type 7 keyboard usable in X: They are
assigned key symbols (usually F-something) so WindowMaker
can pick them up for window management functions or starting
programs (e. g. the volume keys lower or raise the audio
volume or mute the headphones, the "moon key" quits the
session / logout, Ctrl+Alt+Moon shuts down the system).
All I did was first use "sev" to find out the key codes of
the non-functional keys, then assign them an arbitrarily
chosen name via xmodmap, and finally use the "Capture"
function of the WindowMaker preferences utility to make
them actually do something.

But note! If you redefine modifier keys to be "further
function keys", expect to lose their modifier functionality!
This could render them useless. So make sure you can revert
your setting (e. g. have an external shell access that
won't be affected) to comment out the xmodmaprc command in
the X startup file if needed!

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list