1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
23 // Jackson Harper (jackson@ximian.com)
30 // - dead chars are not translated properly
31 // - There is a lot of potential for optimmization in here
34 using System.Collections;
36 using System.Globalization;
37 using System.Runtime.InteropServices;
39 namespace System.Windows.Forms {
41 internal class X11Keyboard {
43 private IntPtr display;
44 private IntPtr window;
46 private StringBuilder lookup_buffer;
47 private int min_keycode, max_keycode, keysyms_per_keycode, syms;
48 private int [] keyc2vkey = new int [256];
49 private int [] keyc2scan = new int [256];
50 private byte [] key_state_table = new byte [256];
52 private bool num_state, cap_state;
53 private bool initialized;
55 private int NumLockMask;
56 private int AltGrMask;
58 public X11Keyboard (IntPtr display, IntPtr window)
60 this.display = display;
62 lookup_buffer = new StringBuilder (24);
65 public void EnsureLayoutInitialized ()
70 KeyboardLayouts layouts = new KeyboardLayouts ();
71 KeyboardLayout layout = DetectLayout (layouts);
73 CreateConversionArray (layouts, layout);
75 if (!XSupportsLocale ()) {
76 Console.Error.WriteLine ("X does not support your locale");
79 if (!XSetLocaleModifiers (String.Empty)) {
80 Console.Error.WriteLine ("Could not set X locale modifiers");
83 IntPtr xim = XOpenIM (display, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
84 if (xim == IntPtr.Zero)
85 Console.Error.WriteLine ("Could not get XIM");
87 xic = CreateXic (window, xim);
92 public Keys ModifierKeys {
94 Keys keys = Keys.None;
95 if ((key_state_table [(int) VirtualKeys.VK_SHIFT] & 0x80) != 0)
97 if ((key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) != 0)
99 if ((key_state_table [(int) VirtualKeys.VK_MENU] & 0x80) != 0)
105 public void FocusIn (IntPtr focus_window)
107 if (xic != IntPtr.Zero)
111 public void FocusOut (IntPtr focus_window)
113 if (xic != IntPtr.Zero)
117 public bool ResetKeyState(IntPtr hwnd, ref MSG msg) {
118 // FIXME - keep defining events/msg and return true until we've 'restored' all
119 // pending keypresses
120 if ((key_state_table [(int) VirtualKeys.VK_SHIFT] & 0x80) != 0) {
121 key_state_table [(int) VirtualKeys.VK_SHIFT] &= unchecked((byte)~0x80);
124 if ((key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) != 0) {
125 key_state_table [(int) VirtualKeys.VK_CONTROL] &= unchecked((byte)~0x80);
128 if ((key_state_table [(int) VirtualKeys.VK_MENU] & 0x80) != 0) {
129 key_state_table [(int) VirtualKeys.VK_MENU] &= unchecked((byte)~0x80);
134 public void KeyEvent (IntPtr hwnd, XEvent xevent, ref MSG msg)
136 EnsureLayoutInitialized ();
141 IntPtr status = IntPtr.Zero;
142 ascii_chars = LookupString (ref xevent, 24, out keysym, out status);
144 if (((int) keysym >= (int) MiscKeys.XK_ISO_Lock &&
145 (int) keysym <= (int) MiscKeys.XK_ISO_Last_Group_Lock) ||
146 (int) keysym == (int) MiscKeys.XK_Mode_switch) {
147 UpdateKeyState (xevent);
151 if ((xevent.KeyEvent.keycode >> 8) == 0x10)
152 xevent.KeyEvent.keycode = xevent.KeyEvent.keycode & 0xFF;
154 int event_time = (int)xevent.KeyEvent.time;
156 if (status == (IntPtr) 2) {
157 // Copy chars into a globally accessible var, i don't think
158 // this var is exposed anywhere though, so we can just ignore this
162 AltGrMask = xevent.KeyEvent.state & (0x6000 | (int) KeyMasks.ModMasks);
163 int vkey = EventToVkey (xevent);
164 if (vkey == 0 && ascii_chars != 0) {
165 vkey = (int) VirtualKeys.VK_NONAME;
168 switch ((VirtualKeys) (vkey & 0xFF)) {
169 case VirtualKeys.VK_NUMLOCK:
170 GenerateMessage (VirtualKeys.VK_NUMLOCK, 0x45, xevent.type, event_time);
172 case VirtualKeys.VK_CAPITAL:
173 GenerateMessage (VirtualKeys.VK_CAPITAL, 0x3A, xevent.type, event_time);
176 if (((key_state_table [(int) VirtualKeys.VK_NUMLOCK] & 0x01) == 0) != ((xevent.KeyEvent.state & NumLockMask) == 0)) {
177 GenerateMessage (VirtualKeys.VK_NUMLOCK, 0x45, XEventName.KeyPress, event_time);
178 GenerateMessage (VirtualKeys.VK_NUMLOCK, 0x45, XEventName.KeyRelease, event_time);
181 if (((key_state_table [(int) VirtualKeys.VK_CAPITAL] & 0x01) == 0) != ((xevent.KeyEvent.state & (int) KeyMasks.LockMask) == 0)) {
182 GenerateMessage (VirtualKeys.VK_CAPITAL, 0x3A, XEventName.KeyPress, event_time);
183 GenerateMessage (VirtualKeys.VK_CAPITAL, 0x3A, XEventName.KeyRelease, event_time);
189 int bscan = (keyc2scan [xevent.KeyEvent.keycode] & 0xFF);
190 KeybdEventFlags dw_flags = KeybdEventFlags.None;
191 if (xevent.type == XEventName.KeyRelease)
192 dw_flags |= KeybdEventFlags.KeyUp;
193 if ((vkey & 0x100) != 0)
194 dw_flags |= KeybdEventFlags.ExtendedKey;
195 msg = SendKeyboardInput ((VirtualKeys) (vkey & 0xFF), bscan, dw_flags, event_time);
201 public bool TranslateMessage (ref MSG msg)
205 if (msg.message >= Msg.WM_KEYFIRST && msg.message <= Msg.WM_KEYLAST)
208 if (msg.message != Msg.WM_KEYDOWN && msg.message != Msg.WM_SYSKEYDOWN)
211 EnsureLayoutInitialized ();
215 int tu = ToUnicode ((int) msg.wParam, Control.HighOrder ((int) msg.lParam), out buffer);
218 message = (msg.message == Msg.WM_KEYDOWN ? Msg.WM_CHAR : Msg.WM_SYSCHAR);
219 XplatUI.PostMessage (msg.hwnd, message, (IntPtr) buffer [0], msg.lParam);
222 message = (msg.message == Msg.WM_KEYDOWN ? Msg.WM_DEADCHAR : Msg.WM_SYSDEADCHAR);
223 XplatUI.PostMessage (msg.hwnd, message, (IntPtr) buffer [0], msg.lParam);
230 public int ToKeycode(int key)
234 if (nonchar_vkey_key[key] > 0)
235 keycode = XKeysymToKeycode (display, nonchar_vkey_key[key]);
238 keycode = XKeysymToKeycode (display, key);
243 public int ToUnicode (int vkey, int scan, out string buffer)
245 if ((scan & 0x8000) != 0) {
246 buffer = String.Empty;
250 XEvent e = new XEvent ();
251 e.AnyEvent.type = XEventName.KeyPress;
252 e.KeyEvent.display = display;
253 e.KeyEvent.keycode = 0;
254 e.KeyEvent.state = 0;
256 if ((key_state_table [(int) VirtualKeys.VK_SHIFT] & 0x80) != 0) {
257 e.KeyEvent.state |= (int) KeyMasks.ShiftMask;
260 if ((key_state_table [(int) VirtualKeys.VK_CAPITAL] & 0x01) != 0) {
261 e.KeyEvent.state |= (int) KeyMasks.LockMask;
264 if ((key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) != 0) {
265 e.KeyEvent.state |= (int) KeyMasks.ControlMask;
268 if ((key_state_table [(int) VirtualKeys.VK_NUMLOCK] & 0x01) != 0) {
269 e.KeyEvent.state |= NumLockMask;
272 e.KeyEvent.state |= AltGrMask;
274 for (int keyc = min_keycode; (keyc <= max_keycode) && (e.KeyEvent.keycode == 0); keyc++) {
275 // find keycode that could have generated this vkey
276 if ((keyc2vkey [keyc] & 0xFF) == vkey) {
277 // filter extended bit because it is not known
278 e.KeyEvent.keycode = keyc;
279 if ((EventToVkey (e) & 0xFF) != vkey) {
280 // Wrong one (ex: because of num,lock state)
281 e.KeyEvent.keycode = 0;
286 if ((vkey >= (int) VirtualKeys.VK_NUMPAD0) && (vkey <= (int) VirtualKeys.VK_NUMPAD9))
287 e.KeyEvent.keycode = XKeysymToKeycode (display, vkey - (int) VirtualKeys.VK_NUMPAD0 + (int) KeypadKeys.XK_KP_0);
289 if (vkey == (int) VirtualKeys.VK_DECIMAL)
290 e.KeyEvent.keycode = XKeysymToKeycode (display, (int) KeypadKeys.XK_KP_Decimal);
292 if (vkey == (int) VirtualKeys.VK_SEPARATOR)
293 e.KeyEvent.keycode = XKeysymToKeycode(display, (int) KeypadKeys.XK_KP_Separator);
295 if (e.KeyEvent.keycode == 0 && vkey != (int) VirtualKeys.VK_NONAME) {
296 // And I couldn't find the keycode so i returned the vkey and was like whatever
297 Console.Error.WriteLine ("unknown virtual key {0:X}", vkey);
298 buffer = String.Empty;
304 int res = LookupString (ref e, 24, out t, out status);
305 int keysym = (int) t;
307 buffer = String.Empty;
309 int dead_char = MapDeadKeySym (keysym);
310 if (dead_char != 0) {
311 byte [] bytes = new byte [1];
312 bytes [0] = (byte) dead_char;
313 Encoding encoding = Encoding.GetEncoding (new CultureInfo (lcid).TextInfo.ANSICodePage);
314 buffer = new string (encoding.GetChars (bytes));
318 // Shift + arrow, shift + home, ....
319 // X returns a char for it, but windows doesn't
320 if (((e.KeyEvent.state & NumLockMask) == 0) && ((e.KeyEvent.state & (int) KeyMasks.ShiftMask) != 0) &&
321 (keysym >= (int) KeypadKeys.XK_KP_0) && (keysym <= (int) KeypadKeys.XK_KP_9)) {
322 buffer = String.Empty;
326 // CTRL + number, X returns chars, windows does not
327 if ((e.KeyEvent.state & (int) KeyMasks.ControlMask) != 0) {
328 if (((keysym >= 33) && (keysym < 'A')) || ((keysym > 'Z') && (keysym < 'a'))) {
329 buffer = String.Empty;
334 // X returns a char for delete key on extended keyboards, windows does not
335 if (keysym == (int) TtyKeys.XK_Delete) {
336 buffer = String.Empty;
340 if (keysym == (int) TtyKeys.XK_BackSpace && (key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) != 0) {
341 buffer = new string (new char [] { (char) 127 });
347 buffer = lookup_buffer.ToString ();
355 private MSG SendKeyboardInput (VirtualKeys vkey, int scan, KeybdEventFlags dw_flags, int time)
359 if ((dw_flags & KeybdEventFlags.KeyUp) != 0) {
360 bool sys_key = (key_state_table [(int) VirtualKeys.VK_MENU] & 0x80) != 0 &&
361 ((key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) == 0);
362 key_state_table [(int) vkey] &= unchecked ((byte) ~0x80);
363 message = (sys_key ? Msg.WM_SYSKEYUP : Msg.WM_KEYUP);
365 if ((key_state_table [(int) vkey] & 0x80) == 0) {
366 key_state_table [(int) vkey] ^= 0x01;
368 key_state_table [(int) vkey] |= 0x80;
369 bool sys_key = (key_state_table [(int) VirtualKeys.VK_MENU] & 0x80) != 0 &&
370 ((key_state_table [(int) VirtualKeys.VK_CONTROL] & 0x80) == 0);
371 message = (sys_key ? Msg.WM_SYSKEYDOWN : Msg.WM_KEYDOWN);
374 MSG msg = new MSG ();
375 msg.message = message;
376 msg.wParam = (IntPtr) vkey;
377 if ((key_state_table [(int) VirtualKeys.VK_MENU] & 0x80) != 0)
378 msg.lParam = new IntPtr (0x20000000);
380 msg.lParam = IntPtr.Zero;
385 private void GenerateMessage (VirtualKeys vkey, int scan, XEventName type, int event_time)
387 bool state = (vkey == VirtualKeys.VK_NUMLOCK ? num_state : cap_state);
388 KeybdEventFlags up, down;
391 // The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
392 // don't treat it. It's from the same key press. Then the state goes to ON.
393 // And from there, a 'release' event will switch off the toggle key.
394 SetState (vkey, false);
396 down = (vkey == VirtualKeys.VK_NUMLOCK ? KeybdEventFlags.ExtendedKey : KeybdEventFlags.None);
397 up = (vkey == VirtualKeys.VK_NUMLOCK ? KeybdEventFlags.ExtendedKey :
398 KeybdEventFlags.None) | KeybdEventFlags.KeyUp;
399 if ((key_state_table [(int) vkey] & 0x1) != 0) { // it was on
400 if (type != XEventName.KeyPress) {
401 SendKeyboardInput (vkey, scan, down, event_time);
402 SendKeyboardInput (vkey, scan, up, event_time);
403 SetState (vkey, false);
404 key_state_table [(int) vkey] &= unchecked ((byte) ~0x01);
407 if (type == XEventName.KeyPress) {
408 SendKeyboardInput (vkey, scan, down, event_time);
409 SendKeyboardInput (vkey, scan, up, event_time);
410 SetState (vkey, true);
411 key_state_table [(int) vkey] |= 0x01;
417 private void UpdateKeyState (XEvent xevent)
419 int vkey = EventToVkey (xevent);
421 switch (xevent.type) {
422 case XEventName.KeyRelease:
423 key_state_table [vkey & 0xff] &= unchecked ((byte) ~0x80);
425 case XEventName.KeyPress:
426 if ((key_state_table [vkey & 0xff] & 0x80) == 0) {
427 key_state_table [vkey & 0xff] ^= 0x01;
429 key_state_table [vkey & 0xff] |= 0x80;
434 private void SetState (VirtualKeys key, bool state)
436 if (VirtualKeys.VK_NUMLOCK == key)
442 public int EventToVkey (XEvent e)
444 EnsureLayoutInitialized ();
449 LookupString (ref e, 0, out ks, out status);
450 int keysym = (int) ks;
452 if (((e.KeyEvent.state & NumLockMask) != 0) &&
453 (keysym == (int)KeypadKeys.XK_KP_Separator || keysym == (int)KeypadKeys.XK_KP_Decimal ||
454 (keysym >= (int)KeypadKeys.XK_KP_0 && keysym <= (int)KeypadKeys.XK_KP_9))) {
455 // Only the Keypad keys 0-9 and . send different keysyms
456 // depending on the NumLock state
457 return nonchar_key_vkey [keysym & 0xFF];
460 return keyc2vkey [e.KeyEvent.keycode];
463 private void CreateConversionArray (KeyboardLayouts layouts, KeyboardLayout layout)
465 XEvent e2 = new XEvent ();
467 int [] ckey = new int [] { 0, 0, 0, 0 };
469 e2.KeyEvent.display = display;
470 e2.KeyEvent.state = 0;
472 for (int keyc = min_keycode; keyc <= max_keycode; keyc++) {
476 e2.KeyEvent.keycode = keyc;
480 LookupString (ref e2, 0, out t, out status);
484 if ((keysym >> 8) == 0xFF) {
485 vkey = nonchar_key_vkey [keysym & 0xFF];
486 scan = nonchar_key_scan [keysym & 0xFF];
488 if ((scan & 0x100) != 0)
490 } else if (keysym == 0x20) { // spacebar
491 vkey = (int) VirtualKeys.VK_SPACE;
494 // Search layout dependent scancodes
498 for (int i = 0; i < syms; i++) {
499 keysym = XKeycodeToKeysym (display, keyc, i);
500 if ((keysym < 0x800) && (keysym != ' '))
501 ckey [i] = (sbyte) (keysym & 0xFF);
503 ckey [i] = (sbyte) MapDeadKeySym ((int) keysym);
506 for (int keyn = 0; keyn < layout.Keys.Length; keyn++) {
507 int ml = Math.Min (layout.Keys [keyn].Length, 4);
509 for (int i = 0; (ok != 0) && (i < ml); i++) {
510 sbyte ck = (sbyte) layout.Keys [keyn][i];
513 if ((ok != 0) || (i > maxlen)) {
522 scan = layouts.scan_table [(int) layout.ScanIndex][maxval];
523 vkey = layouts.vkey_table [(int) layout.VKeyIndex][maxval];
528 keyc2vkey [e2.KeyEvent.keycode] = vkey;
529 keyc2scan [e2.KeyEvent.keycode] = scan;
535 private KeyboardLayout DetectLayout (KeyboardLayouts layouts)
537 XDisplayKeycodes (display, out min_keycode, out max_keycode);
538 IntPtr ksp = XGetKeyboardMapping (display, (byte) min_keycode,
539 max_keycode + 1 - min_keycode, out keysyms_per_keycode);
540 XplatUIX11.XFree (ksp);
542 syms = keysyms_per_keycode;
544 //Console.Error.WriteLine ("{0} keysymbols per a keycode is not supported, setting to 4", syms);
548 IntPtr modmap_unmanaged;
549 XModifierKeymap xmk = new XModifierKeymap ();
551 modmap_unmanaged = XGetModifierMapping (display);
552 xmk = (XModifierKeymap) Marshal.PtrToStructure (modmap_unmanaged, typeof (XModifierKeymap));
555 for (int i = 0; i < 8; i++) {
556 for (int j = 0; j < xmk.max_keypermod; j++, mmp++) {
557 byte b = Marshal.ReadByte (xmk.modifiermap, mmp);
559 for (int k = 0; k < keysyms_per_keycode; k++) {
560 if ((int) XKeycodeToKeysym (display, b, k) == (int) MiscKeys.XK_Num_Lock)
561 NumLockMask = 1 << i;
566 XFreeModifiermap (modmap_unmanaged);
568 int [] ckey = new int [4];
569 KeyboardLayout layout = null;
573 foreach (KeyboardLayout current in layouts.Layouts) {
580 int key = min_keycode;
583 for (int keyc = min_keycode; keyc <= max_keycode; keyc++) {
584 for (i = 0; i < syms; i++) {
585 uint keysym = XKeycodeToKeysym (display, keyc, i);
587 if ((keysym < 0x800) && (keysym != ' ')) {
588 ckey [i] = (sbyte) (keysym & 0xFF);
590 ckey [i] = (sbyte) MapDeadKeySym ((int) keysym);
594 for (key = 0; key < current.Keys.Length; key++) {
595 int ml = Math.Min (syms, current.Keys [key].Length);
596 for (ok = 0, i = 0; (ok >= 0) && (i < ml); i++) {
597 sbyte ck = (sbyte) current.Keys [key][i];
598 if (ck != 0 && ck == ckey[i])
600 if (ck != 0 && ck != ckey[i])
610 /* and how much the keycode order matches */
615 /* print spaces instead of \0's */
622 if ((score > max_score) || ((score == max_score) && (seq > max_seq))) {
630 if (layout != null) {
633 Console.WriteLine (Locale.GetText("Keyboard layout not recognized, using default layout: " +
634 layouts.Layouts [0].Name));
637 return layouts.Layouts [0];
641 private int MapDeadKeySym (int val)
644 case (int) DeadKeys.XK_dead_tilde :
645 case 0x1000FE7E : // Xfree's Dtilde
647 case (int) DeadKeys.XK_dead_acute :
648 case 0x1000FE27 : // Xfree's XK_Dacute_accent
650 case (int) DeadKeys.XK_dead_circumflex:
651 case 0x1000FE5E : // Xfree's XK_.Dcircumflex_accent
653 case (int) DeadKeys.XK_dead_grave :
654 case 0x1000FE60 : // Xfree's XK_.Dgrave_accent
656 case (int) DeadKeys.XK_dead_diaeresis :
657 case 0x1000FE22 : // Xfree's XK_.Ddiaeresis
659 case (int) DeadKeys.XK_dead_cedilla :
661 case (int) DeadKeys.XK_dead_macron :
663 case (int) DeadKeys.XK_dead_breve :
665 case (int) DeadKeys.XK_dead_abovedot :
667 case (int) DeadKeys.XK_dead_abovering :
669 case (int) DeadKeys.XK_dead_doubleacute :
671 case (int) DeadKeys.XK_dead_caron :
673 case (int) DeadKeys.XK_dead_ogonek :
680 private IntPtr CreateXic (IntPtr window, IntPtr xim)
682 xic = XCreateIC (xim,
683 "inputStyle", XIMProperties.XIMPreeditNothing | XIMProperties.XIMStatusNothing,
684 "clientWindow", window,
685 "focusWindow", window,
690 private int LookupString (ref XEvent xevent, int len, out XKeySym keysym, out IntPtr status)
695 status = IntPtr.Zero;
696 lookup_buffer.Length = 0;
697 if (xic != IntPtr.Zero)
698 res = XmbLookupString (xic, ref xevent, lookup_buffer, len, out keysym_res, out status);
700 res = XLookupString (ref xevent, lookup_buffer, len, out keysym_res, IntPtr.Zero);
702 keysym = (XKeySym) keysym_res.ToInt32 ();
706 [DllImport ("libX11")]
707 private static extern IntPtr XOpenIM (IntPtr display, IntPtr rdb, IntPtr res_name, IntPtr res_class);
709 [DllImport ("libX11")]
710 private static extern IntPtr XCreateIC (IntPtr xim, string name, XIMProperties im_style, string name2, IntPtr value2, string name3, IntPtr value3, IntPtr terminator);
712 [DllImport ("libX11")]
713 private static extern void XSetICFocus (IntPtr xic);
715 [DllImport ("libX11")]
716 private static extern void XUnsetICFocus (IntPtr xic);
718 [DllImport ("libX11")]
719 private static extern bool XSupportsLocale ();
721 [DllImport ("libX11")]
722 private static extern bool XSetLocaleModifiers (string mods);
724 [DllImport ("libX11")]
725 internal extern static int XLookupString(ref XEvent xevent, StringBuilder buffer, int num_bytes, out IntPtr keysym, IntPtr status);
726 [DllImport ("libX11")]
727 internal extern static int XmbLookupString(IntPtr xic, ref XEvent xevent, StringBuilder buffer, int num_bytes, out IntPtr keysym, out IntPtr status);
729 internal static int XmbLookupString (IntPtr xic, ref XEvent xevent, StringBuilder buffer, int num_bytes, out XKeySym keysym, out IntPtr status) {
733 ret = XmbLookupString (xic, ref xevent, buffer, num_bytes, out keysym_ret, out status);
735 keysym = (XKeySym)keysym_ret.ToInt32();
740 internal static int XLookupString (ref XEvent xevent, StringBuilder buffer, int num_bytes, out XKeySym keysym, IntPtr status) {
744 ret = XLookupString (ref xevent, buffer, num_bytes, out keysym_ret, status);
745 keysym = (XKeySym)keysym_ret.ToInt32();
750 [DllImport ("libX11")]
751 private static extern IntPtr XGetKeyboardMapping (IntPtr display, byte first_keycode, int keycode_count,
752 out int keysyms_per_keycode_return);
754 [DllImport ("libX11")]
755 private static extern void XDisplayKeycodes (IntPtr display, out int min, out int max);
757 [DllImport ("libX11", EntryPoint="XKeycodeToKeysym")]
758 private static extern uint XKeycodeToKeysym (IntPtr display, int keycode, int index);
760 [DllImport ("libX11")]
761 private static extern int XKeysymToKeycode (IntPtr display, IntPtr keysym);
762 private static int XKeysymToKeycode (IntPtr display, int keysym) {
763 return XKeysymToKeycode(display, (IntPtr)keysym);
766 [DllImport ("libX11")]
767 internal extern static IntPtr XGetModifierMapping (IntPtr display);
769 [DllImport ("libX11")]
770 internal extern static int XFreeModifiermap (IntPtr modmap);
773 private readonly static int [] nonchar_key_vkey = new int []
776 0, 0, 0, 0, 0, 0, 0, 0, /* FF00 */
778 (int) VirtualKeys.VK_BACK, (int) VirtualKeys.VK_TAB, 0, (int) VirtualKeys.VK_CLEAR, 0, (int) VirtualKeys.VK_RETURN, 0, 0, /* FF08 */
779 0, 0, 0, (int) VirtualKeys.VK_PAUSE, (int) VirtualKeys.VK_SCROLL, 0, 0, 0, /* FF10 */
780 0, 0, 0, (int) VirtualKeys.VK_ESCAPE, 0, 0, 0, 0, /* FF18 */
782 0, 0, 0, 0, 0, 0, 0, 0, /* FF20 */
783 0, 0, 0, 0, 0, 0, 0, 0, /* FF28 */
784 0, 0, 0, 0, 0, 0, 0, 0, /* FF30 */
785 0, 0, 0, 0, 0, 0, 0, 0, /* FF38 */
786 0, 0, 0, 0, 0, 0, 0, 0, /* FF40 */
787 0, 0, 0, 0, 0, 0, 0, 0, /* FF48 */
789 (int) VirtualKeys.VK_HOME, (int) VirtualKeys.VK_LEFT, (int) VirtualKeys.VK_UP, (int) VirtualKeys.VK_RIGHT, /* FF50 */
790 (int) VirtualKeys.VK_DOWN, (int) VirtualKeys.VK_PRIOR, (int) VirtualKeys.VK_NEXT, (int) VirtualKeys.VK_END,
791 0, 0, 0, 0, 0, 0, 0, 0, /* FF58 */
793 (int) VirtualKeys.VK_SELECT, (int) VirtualKeys.VK_SNAPSHOT, (int) VirtualKeys.VK_EXECUTE, (int) VirtualKeys.VK_INSERT, 0, 0, 0, 0, /* FF60 */
794 (int) VirtualKeys.VK_CANCEL, (int) VirtualKeys.VK_HELP, (int) VirtualKeys.VK_CANCEL, (int) VirtualKeys.VK_CANCEL, 0, 0, 0, 0, /* FF68 */
795 0, 0, 0, 0, 0, 0, 0, 0, /* FF70 */
797 0, 0, 0, 0, 0, 0, 0, (int) VirtualKeys.VK_NUMLOCK, /* FF78 */
798 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
799 0, 0, 0, 0, 0, (int) VirtualKeys.VK_RETURN, 0, 0, /* FF88 */
800 0, 0, 0, 0, 0, (int) VirtualKeys.VK_HOME, (int) VirtualKeys.VK_LEFT, (int) VirtualKeys.VK_UP, /* FF90 */
801 (int) VirtualKeys.VK_RIGHT, (int) VirtualKeys.VK_DOWN, (int) VirtualKeys.VK_PRIOR, (int) VirtualKeys.VK_NEXT, /* FF98 */
802 (int) VirtualKeys.VK_END, 0, (int) VirtualKeys.VK_INSERT, (int) VirtualKeys.VK_DELETE,
803 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
804 0, 0, (int) VirtualKeys.VK_MULTIPLY, (int) VirtualKeys.VK_ADD, /* FFA8 */
805 (int) VirtualKeys.VK_SEPARATOR, (int) VirtualKeys.VK_SUBTRACT, (int) VirtualKeys.VK_DECIMAL, (int) VirtualKeys.VK_DIVIDE,
806 (int) VirtualKeys.VK_NUMPAD0, (int) VirtualKeys.VK_NUMPAD1, (int) VirtualKeys.VK_NUMPAD2, (int) VirtualKeys.VK_NUMPAD3, /* FFB0 */
807 (int) VirtualKeys.VK_NUMPAD4, (int) VirtualKeys.VK_NUMPAD5, (int) VirtualKeys.VK_NUMPAD6, (int) VirtualKeys.VK_NUMPAD7,
808 (int) VirtualKeys.VK_NUMPAD8, (int) VirtualKeys.VK_NUMPAD9, 0, 0, 0, 0, /* FFB8 */
810 (int) VirtualKeys.VK_F1, (int) VirtualKeys.VK_F2,
811 (int) VirtualKeys.VK_F3, (int) VirtualKeys.VK_F4, (int) VirtualKeys.VK_F5, (int) VirtualKeys.VK_F6, (int) VirtualKeys.VK_F7, (int) VirtualKeys.VK_F8, (int) VirtualKeys.VK_F9, (int) VirtualKeys.VK_F10, /* FFC0 */
812 (int) VirtualKeys.VK_F11, (int) VirtualKeys.VK_F12, (int) VirtualKeys.VK_F13, (int) VirtualKeys.VK_F14, (int) VirtualKeys.VK_F15, (int) VirtualKeys.VK_F16, 0, 0, /* FFC8 */
813 0, 0, 0, 0, 0, 0, 0, 0, /* FFD0 */
814 0, 0, 0, 0, 0, 0, 0, 0, /* FFD8 */
816 0, (int) VirtualKeys.VK_SHIFT, (int) VirtualKeys.VK_SHIFT, (int) VirtualKeys.VK_CONTROL, /* FFE0 */
817 (int) VirtualKeys.VK_CONTROL, (int) VirtualKeys.VK_CAPITAL, 0, (int) VirtualKeys.VK_MENU,
818 (int) VirtualKeys.VK_MENU, (int) VirtualKeys.VK_MENU, (int) VirtualKeys.VK_MENU, 0, 0, 0, 0, 0, /* FFE8 */
819 0, 0, 0, 0, 0, 0, 0, 0, /* FFF0 */
820 0, 0, 0, 0, 0, 0, 0, (int) VirtualKeys.VK_DELETE /* FFF8 */
823 private static readonly int [] nonchar_key_scan = new int []
826 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF00 */
828 0x0E, 0x0F, 0x00, /*?*/ 0, 0x00, 0x1C, 0x00, 0x00, /* FF08 */
829 0x00, 0x00, 0x00, 0x45, 0x46, 0x00, 0x00, 0x00, /* FF10 */
830 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, /* FF18 */
832 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF20 */
833 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF28 */
834 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF30 */
835 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF38 */
836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF40 */
837 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF48 */
839 0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F, /* FF50 */
840 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF58 */
842 /*?*/ 0, 0x137, /*?*/ 0, 0x152, 0x00, 0x00, 0x00, 0x00, /* FF60 */
843 /*?*/ 0, /*?*/ 0, 0x38, 0x146, 0x00, 0x00, 0x00, 0x00, /* FF68 */
844 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF70 */
846 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x138, 0x145, /* FF78 */
847 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FF80 */
848 0x00, 0x00, 0x00, 0x00, 0x00, 0x11C, 0x00, 0x00, /* FF88 */
849 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x4B, 0x48, /* FF90 */
850 0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53, /* FF98 */
851 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFA0 */
852 0x00, 0x00, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135, /* FFA8 */
853 0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47, /* FFB0 */
854 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, /* FFB8 */
857 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, /* FFC0 */
858 0x57, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFC8 */
859 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFD0 */
860 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFD8 */
862 0x00, 0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0x00, 0x38, /* FFE0 */
863 0x138, 0x38, 0x138, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFE8 */
864 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* FFF0 */
865 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x153 /* FFF8 */
868 private readonly static int [] nonchar_vkey_key = new int []
870 0, 0, 0, 0, 0, /* 00-04 */
871 0, 0, 0, (int)XKeySym.XK_BackSpace, (int)XKeySym.XK_Tab, /* 05-09 */
872 0, 0, (int)XKeySym.XK_Clear, (int)XKeySym.XK_Return, 0, 0, /* 0A-0F */
873 (int)XKeySym.XK_Shift_L, (int)XKeySym.XK_Control_L, (int)XKeySym.XK_Menu, 0, (int)XKeySym.XK_Caps_Lock, /* 10-14 */
874 0, 0, 0, 0, 0, /* 15-19 */
875 0, 0, 0, 0, 0, 0, /* 1A-1F */
876 0, 0, 0, (int)XKeySym.XK_End, (int)XKeySym.XK_Home, /* 20-24 */
877 (int)XKeySym.XK_Left, (int)XKeySym.XK_Up, (int)XKeySym.XK_Right, (int)XKeySym.XK_Down, 0, /* 25-29 */
878 0, 0, 0, 0, 0, 0, /* 2A-2F */
879 0, 0, 0, 0, 0, /* 30-34 */
880 0, 0, 0, 0, 0, /* 35-39 */
881 0, 0, 0, 0, 0, 0, /* 3A-3F */
882 0, 0, 0, 0, 0, /* 40-44 */
883 0, 0, 0, 0, 0, /* 45-49 */
884 0, 0, 0, 0, 0, 0, /* 4A-4F */
885 0, 0, 0, 0, 0, /* 50-54 */
886 0, 0, 0, 0, 0, /* 55-59 */
887 0, (int)XKeySym.XK_Meta_L, (int)XKeySym.XK_Meta_R, 0, 0, 0, /* 5A-5F */
888 0, 0, 0, 0, 0, /* 60-64 */
889 0, 0, 0, 0, 0, /* 65-69 */
890 0, 0, 0, 0, 0, 0, /* 6A-6F */
891 0, 0, 0, 0, 0, /* 70-74 */
892 0, 0, 0, 0, 0, /* 75-79 */
893 0, 0, 0, 0, 0, 0, /* 7A-7F */
894 0, 0, 0, 0, 0, /* 80-84 */
895 0, 0, 0, 0, 0, /* 85-89 */
896 0, 0, 0, 0, 0, 0, /* 8A-8F */
897 0, 0, 0, 0, 0, /* 90-94 */
898 0, 0, 0, 0, 0, /* 95-99 */
899 0, 0, 0, 0, 0, 0, /* 9A-9F */
900 (int)XKeySym.XK_Shift_L, (int)XKeySym.XK_Shift_R, (int)XKeySym.XK_Control_L, (int)XKeySym.XK_Control_R, (int)XKeySym.XK_Alt_L, /* A0-A4 */
901 (int)XKeySym.XK_Alt_R, 0, 0, 0, 0, /* A5-A9 */
902 0, 0, 0, 0, 0, 0, /* AA-AF */
903 0, 0, 0, 0, 0, /* B0-B4 */
904 0, 0, 0, 0, 0, /* B5-B9 */
905 0, 0, 0, 0, 0, 0, /* BA-BF */
906 0, 0, 0, 0, 0, /* C0-C4 */
907 0, 0, 0, 0, 0, /* C5-C9 */
908 0, 0, 0, 0, 0, 0, /* CA-CF */
909 0, 0, 0, 0, 0, /* D0-D4 */
910 0, 0, 0, 0, 0, /* D5-D9 */
911 0, 0, 0, 0, 0, 0, /* DA-DF */
912 0, 0, 0, 0, 0, /* E0-E4 */
913 0, 0, 0, 0, 0, /* E5-E9 */
914 0, 0, 0, 0, 0, 0, /* EA-EF */
915 0, 0, 0, 0, 0, /* F0-F4 */
916 0, 0, 0, 0, 0, /* F5-F9 */
917 0, 0, 0, 0, 0, 0 /* FA-FF */