PDA

View Full Version : X11 KeySyms and KeyCodes



code_glitch
29-08-2011, 01:38 PM
For the past few days, I've been working on an X11 layer to replace SDL in my future applications. All has been nice sailing up until now where I've arrived on stranger tides :D

Ok, jokes aside, I have this:



PMEvent.LastKeyDown := XKeySymToKeyCode(Display, XLookupKeySym(@event.xkey, 0));

and


PMEvent.LastKeyDown := Display, XLookupKeySym(@event.xkey, 0);


Now although both appear to work - they do not work the way they should. The first implementation results in the following behaviour:
q=24, w=25, e=26 and so on for each row of keys on the keyboard
The second implementation returns the 'correct' values and can be reliably fed from Ord() and into char() however pressing up, esc etc results in values over 65,000! :o

Has anyone come up with a solution to this - or at least can find a way of getting these to match the 'correct' sdl and readkey() keycodes?

Carver413
29-08-2011, 03:22 PM
vKey:=XKeycodeToKeysym(Event.xkey.display,Event.xk ey.keycode,0);

this returns a Integer value which is mapped to the table in keysym unit. so for the up key = XK_Up = $FF52; { Move up, up arrow }
you will have to use this table to convert them to something else.

code_glitch
29-08-2011, 06:03 PM
Oh boy, I feared as much :(

Looks like I have some constants to type out. Well thanks for clearing that up there carver.

Andru
29-08-2011, 07:27 PM
I'm using scancodes everywhere and here is my old solution... :)


function xkey_to_scancode( XKey, KeyCode : Integer ) : Byte;
begin
case XKey of
XK_Pause: Result := K_PAUSE;

XK_Up: Result := K_UP;
XK_Down: Result := K_DOWN;
XK_Left: Result := K_LEFT;
XK_Right: Result := K_RIGHT;

XK_Insert: Result := K_INSERT;
XK_Delete: Result := K_DELETE;
XK_Home: Result := K_HOME;
XK_End: Result := K_END;
XK_Page_Up: Result := K_PAGEUP;
XK_Page_Down: Result := K_PAGEDOWN;

XK_Control_R: Result := K_CTRL_R;
XK_Alt_R: Result := K_ALT_R;
XK_Super_L: Result := K_SUPER_L;
XK_Super_R: Result := K_SUPER_R;
XK_Menu: Result := K_APP_MENU;

XK_KP_Divide: Result := K_KP_DIV;
else
Result := ( KeyCode - 8 ) and $FF;
end;
end;

// Somewhere in code
xkey_to_scancode( XLookupKeysym( @event.xkey, 0 ), event.xkey.keycode );

code_glitch
29-08-2011, 10:07 PM
Not a bad snippet there. After further study I did find that if made a simple console application and wrote out the Ord(Readkey()) I could get it to match the X11 keysyms as scancodes or whatever.... So I guess I will have to make do with 'non-sdl compatible' code ;)

Not a big deal - just me being picky.