Howto handle modified keys

Glynn Clements glynn at gclements.plus.com
Fri Oct 20 06:09:35 PDT 2006


Daniel Stone wrote:

> > is there an easy posibility to react on combinations of keyboard-events 
> > like ALT+F or something like that? I have implemented an event handler, 
> > which reacts on the KeyPress event. When I press the 'ALT'-key, the 
> > event handler recognizes it correctly as keycode '64'. When I press the 
> > 'f'-key, the event handler recognizes ist correctly as keycode '41'. But 
> > when I press 'f' in combination with 'ALT' theres no difference, it 
> > first recognizes 'ALT' and than 'f' (depending on chronological order). 
> > So how can I handle modified keys?
> 
> Look in the state field of the key event, there's Mod[1..4]Mask
> available.  I think Alt is generally Mod4.  So a key press of f with
> Mod4Mask in state, means Alt-F.

The Control, Shift and Lock modifiers are associated with specific
modifier bits.

For other modifiers (Alt, Meta, Hyper, Super), the correct way to
identify modifier bits is to check which keysyms are associated with
the modifier bits. E.g. if mod1 is associated with Alt_L and/or Alt_R,
then mod1 is the Alt modifier. Use XGetModifierMapping and
XGetKeyboardMapping to determine the mapping between modifier bits and
keysyms.

Note that, historically, the key labelled Alt tended to correspond to
the Meta modifier rather than the Alt modifier.

Also, a number of GUI toolkits don't handle modifiers correctly; e.g. 
treating mod1 as the Alt modifier regardless of which keysyms are
associated with it.

-- 
Glynn Clements <glynn at gclements.plus.com>



More information about the xorg mailing list