Name

KeyPress,KeyRelease — (generated event).

When Generated

KeyPress and KeyRelease events are generated for all keys, even those mapped to modifier keys such as Shift or Control.

Select With

Each type of keyboard event may be selected separately with KeyPressMask and KeyReleaseMask.

XEvent Structure Name

typedef union _XEvent {
   ...
   XKeyEvent xkey;
   ...
} XEvent;

Event Structure

typedef struct {
   int type;                  /* of event */
   unsigned long serial;      /* # of last request processed by server */
   Bool send_event;           /* True if this came from SendEvent request */
   Display *display;          /* Display the event was read from */
   Window window;             /* event window it is reported relative to */
   Window root;               /* root window that the event occurred on */
   Window subwindow;          /* child window */
   Time time;                 /* milliseconds */
   int x, y;                  /* pointer coordinates relative to receiving 
                               * window */
   int x_root, y_root;        /* coordinates relative to root */
   unsigned int state;        /* modifier key and button mask */
   unsigned int keycode;      /* server-dependent code for key */
   Bool same_screen;          /* same screen flag */
} XKeyEvent;   
typedef XKeyEvent XKeyPressedEvent;
typedef XKeyEvent XKeyReleasedEvent;

Event Structure Members

subwindow 

If the source window is the child of the receiving window, then the subwindow member is set to the ID of that child.

time 

The server time when the button event occurred, in milliseconds. Time is declared as unsigned long, so it wraps around when it reaches the maximum value of a 32-bit number (every 49.7 days).

x, y 

If the receiving window is on the same screen as the root window specified by root, then x and y are the pointer coordinates relative to the receiving window's origin. Otherwise, x and y are zero. When active button grabs and pointer grabs are in effect (see 9.4 of Volume One), the coordinates relative to the receiving window may not be within the window (they may be negative or greater than window height or width).

x_root, y_root 

The pointer coordinates relative to the root window which is an ancestor of the event window. If the pointer was on a different screen, these are zero.

state 

The state of all the buttons and modifier keys just before the event, represented by a mask of the button and modifier key symbols: Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, ControlMask, LockMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask, and ShiftMask.

keycode 

The keycode member contains a server-dependent code for the key that changed state. As such, it should be translated into the portable symbol called a keysym before being used. It can also be converted directly into ASCII with XLookupString. For a description and examples of how to translate keycodes, see 9.1.1.

Notes

Remember that not all hardware is capable of generating release events and that only the main keyboard (a-z, A-Z, 0-9), Shift, and Control keys are always found.

Keyboard events are analogous to button events, though, of course, there are many more keys than buttons and the keyboard is not automatically grabbed between press and release.

All the structure members have the same meaning as described for ButtonPress and ButtonRelease events, except that button is replaced by keycode.