4

How do I convert the X11 keycode to a microsoft virtual key code

int processKeyboardMessage( XEvent *event )
{
  assert( KeyPress == event->type );

  //TODO: Before returning convert keycode into microsoft virtual key code     
  return ( event->xkey.keycode );
}
JonnyRo
  • 1,844
  • 1
  • 14
  • 20
  • Why do you want to use MS virtual keycodes rather than X keysyms? – parkydr Feb 20 '13 at 20:56
  • it's a multi platform project. I will be receiving keysyms from X. I need to translate them into MS Virtual Keycodes for processing in another system. – JonnyRo Feb 21 '13 at 05:03
  • Probably the best you can do then, is to convert the X keycode to an X keysym and then map them yourself. – parkydr Feb 21 '13 at 08:18

1 Answers1

1

One option is to make enums for all of the possible keys on every platform. Then you can deal with keycodes in the application in the system's native format. There are some subtle things you need to do to work around certain situations (like left/right alt on win32), but you can implement these special cases and update your enum for them. Rather than creating large tables or switch-case statements on every platform.

/* my_key.h : creates names for key codes on Windows and X11 */
#ifndef MY_KEY_H
#define MY_KEY_H
#if defined(_WIN32)
#include <windows.h>
enum my_key {
    MY_KEY_BACKSPACE = VK_BACK,
    MY_KEY_RETURN = VK_RETURN,
    MY_KEY_LEFT = VK_LEFT,
    MY_KEY_RIGHT = VK_RIGHT,
    MY_KEY_DOWN = VK_DOWN,
    MY_KEY_UP = VK_UP,
    /* TODO: define the rest of the keys */
};
#else defined(__APPLE__)
enum my_key {
    MY_KEY_BACKSPACE = 0x33,
    MY_KEY_RETURN = 0x24,
    MY_KEY_LEFT = 0x7b,
    MY_KEY_RIGHT = 0x7c,
    MY_KEY_DOWN = 0x7d,
    MY_KEY_UP = 0x7e,
    /* TODO: define the rest of the keys */
};
#else /* assume X11 */
#include <X11/keysym.h>
enum my_key {
    MY_KEY_BACKSPACE = XK_BackSpace,
    MY_KEY_RETURN = XK_Linefeed,
    MY_KEY_LEFT = XK_Left,
    MY_KEY_RIGHT = XK_Right,
    MY_KEY_DOWN = XK_Down,
    MY_KEY_UP = XK_Up,
    /* TODO: define the rest of the keys */
};
#endif
#endif
KlemenPl
  • 354
  • 4
  • 21
orangetide
  • 71
  • 6