/// <summary> /// Translates the specified character to the corresponding virtual-key code /// for the current keyboard. /// </summary> /// <param name="c">The character to be translated into a virtual-key code.</param> /// <returns>The virtual key code.</returns> public virtual VirtualKeys CharacterToVirtualKey(char character) { if (PlatformInfo.IsWindows) { int virtualKeyCode = (WinApi.NativeMethods.VkKeyScan(character) & 0xFF); if (virtualKeyCode == 0xFF) { return(VirtualKeys.None); } return((VirtualKeys)virtualKeyCode); } if (PlatformInfo.IsLinux) { XKeySym keysym = Linux.KeyInterop.CharToXKeySym(character); if (keysym == XKeySym.None) { keysym = CefNet.Linux.NativeMethods.XStringToKeysym("U" + ((int)character).ToString("X")); } return(Linux.KeyInterop.XKeySymToVirtualKey(TranslateXKeySymToAsciiXKeySym(keysym))); } if (PlatformInfo.IsMacOS) { // US QWERTY only XKeySym keysym = Linux.KeyInterop.CharToXKeySym(character); return(Linux.KeyInterop.XKeySymToVirtualKey(keysym)); } throw new NotImplementedException(); }
/// <summary> /// Translates a character to the corresponding virtual-key code for the current keyboard. /// </summary> /// <param name="c">The character to be translated into a virtual-key code.</param> /// <returns>The virtual key code.</returns> /// <exception cref="InvalidOperationException"> /// The function finds no key that translates to the passed character code. /// Perhaps the wrong locale is being used. /// </exception> public static VirtualKeys GetVirtualKey(char c) { if (PlatformInfo.IsWindows) { int virtualKeyCode = (WinApi.NativeMethods.VkKeyScan(c) & 0xFF); if (virtualKeyCode == 0xFF) { throw new InvalidOperationException("Incompatible input locale."); } return((VirtualKeys)virtualKeyCode); } if (PlatformInfo.IsLinux) { XKeySym keysym = Linux.KeyInterop.CharToXKeySym(c); if (keysym == XKeySym.None) { keysym = Linux.NativeMethods.XStringToKeysym("U" + ((int)c).ToString("X")); } return(Linux.KeyInterop.XKeySymToVirtualKey(TranslateXKeySymToAsciiXKeySym(keysym))); } if (PlatformInfo.IsMacOS) { // US QWERTY only XKeySym keysym = Linux.KeyInterop.CharToXKeySym(c); return(Linux.KeyInterop.XKeySymToVirtualKey(keysym)); } throw new NotImplementedException(); }
/// <summary> /// Converts the value of a UTF-16 encoded character into a native key code. /// </summary> /// <param name="character">The character to be converted.</param> /// <param name="extended">The extended key flag.</param> /// <returns>A native key code for the current keyboard.</returns> public static int GetMacOSNativeKeyCode(char character, bool extended) { XKeySym keysym = Linux.KeyInterop.CharToXKeySym(character); if (keysym == XKeySym.None) { return(0); } return(GetMacOSNativeKeyCode(Linux.KeyInterop.XKeySymToVirtualKey(keysym), extended)); }
/// <summary> /// Converts a combination of a virtual key code and a modifier key state into a string of one or more Unicode characters. /// </summary> /// <param name="virtualKey">The virtual key code that is to be translated.</param> /// <param name="modifiers">A bitwise combination of the <see cref="CefEventFlags"/> values.</param> /// <returns>The character resulting from the virtual key code being handled.</returns> public static char TranslateVirtualKey(VirtualKeys virtualKey, CefEventFlags modifiers) { XKeySym keysym = Linux.KeyInterop.VirtualKeyToXKeySym(virtualKey, modifiers.HasFlag(CefEventFlags.ShiftDown)); if (keysym == XKeySym.None) { return(char.MinValue); } return(Linux.KeyInterop.XKeySymToChar(keysym)); }
/// <summary> /// Converts the value of a UTF-16 encoded character into a native key code. /// </summary> /// <param name="c">The character to be converted.</param> public static int GetMacOSNativeKeyCode(char c) { XKeySym keysym = Linux.KeyInterop.CharToXKeySym(c); if (keysym == XKeySym.None) { return(0); } return(GetMacOSNativeKeyCode(Linux.KeyInterop.XKeySymToVirtualKey(keysym), false)); }
/// <summary> /// Returns a native key code for the specified virtual key. /// </summary> /// <param name="key">Specifies the virtual key.</param> /// <param name="shift">Specifies a Shift key state.</param> /// <returns>A native key code for the current keyboard.</returns> public static int GetLinuxNativeKeyCode(VirtualKeys key, bool shift) { XKeySym keysym = Linux.KeyInterop.VirtualKeyToXKeySym(key, shift); if (keysym == XKeySym.None) { return(0); } return(GetHardwareKeyCode(keysym)); }
/// <summary> /// Converts the value of a UTF-16 encoded character into a native key code. /// </summary> /// <param name="c">The character to be converted.</param> public static int GetLinuxNativeKeyCode(char c) { XKeySym keysym = Linux.KeyInterop.CharToXKeySym(c); if (keysym == XKeySym.None) { return(0); } return(GetHardwareKeyCode(keysym)); }
/// <summary> /// Returns a hardware key code for the specified X keysym (only Linux OS). /// </summary> /// <param name="keysym">Specifies the KeySym.</param> /// <returns>A hardware key code.</returns> public static byte GetHardwareKeyCode(XKeySym keysym) { IntPtr display = Linux.NativeMethods.XOpenDisplay(IntPtr.Zero); try { return(Linux.NativeMethods.XKeysymToKeycode(display, keysym)); } finally { Linux.NativeMethods.XCloseDisplay(display); } }
/// <summary> /// Translates a KeySym to the corresponding KeySym from latin range for the current keyboard. /// </summary> /// <param name="keysym">The KeySym to be translated.</param> /// <returns>The KeySym.</returns> public static XKeySym TranslateXKeySymToAsciiXKeySym(XKeySym keysym) { IntPtr display = Linux.NativeMethods.XOpenDisplay(IntPtr.Zero); try { byte keycode = Linux.NativeMethods.XKeysymToKeycode(display, keysym); return(Linux.NativeMethods.XKeycodeToKeysym(display, keycode, 0)); } finally { Linux.NativeMethods.XCloseDisplay(display); } }
void handleKeyPressEvent(XKeyEvent e, XWindow window, XWindow root, XWindow subwindow) { XKeySym ks = window.KeycodeToKeysym(e.keycode); if (ks == XKeySym.NoSymbol) { return; } switch (ks) { case XKeySym.XK_Delete: Console.WriteLine("Window manager is restarting..."); restart(); break; case XKeySym.XK_End: Console.WriteLine("Window manager is quitting."); quitNicely(); break; } }
public int KeysymToKeycode(XKeySym keysym) { return XKeysymToKeycode (display.Handle, keysym); }
public extern static byte XKeysymToKeycode(IntPtr display, XKeySym keysym);
private int LookupString (ref XEvent xevent, int len, out XKeySym keysym, out IntPtr status) { IntPtr keysym_res; int res; status = IntPtr.Zero; IntPtr xic = GetXic (client_window); if (xic != IntPtr.Zero && have_Xutf8LookupString) { do { try { res = Xutf8LookupString (xic, ref xevent, lookup_byte_buffer, 100, out keysym_res, out status); } catch (EntryPointNotFoundException) { have_Xutf8LookupString = false; /* Duplicate of the non-xic clause */ do { res = XLookupString (ref xevent, lookup_byte_buffer, 100, out keysym_res, out status); if ((int) status != -1) // XLookupBufferOverflow break; lookup_byte_buffer = new byte [lookup_byte_buffer.Length << 1]; } while (true); lookup_buffer.Length = 0; string s2 = Encoding.ASCII.GetString (lookup_byte_buffer, 0, res); lookup_buffer.Append (s2); keysym = (XKeySym) keysym_res.ToInt32 (); return res; } if ((int) status != -1) // XLookupBufferOverflow break; lookup_byte_buffer = new byte [lookup_byte_buffer.Length << 1]; } while (true); lookup_buffer.Length = 0; string s = Encoding.UTF8.GetString (lookup_byte_buffer, 0, res); lookup_buffer.Append (s); keysym = (XKeySym) keysym_res.ToInt32 (); return s.Length; } else { do { res = XLookupString (ref xevent, lookup_byte_buffer, 100, out keysym_res, out status); if ((int) status != -1) // XLookupBufferOverflow break; lookup_byte_buffer = new byte [lookup_byte_buffer.Length << 1]; } while (true); lookup_buffer.Length = 0; string s = Encoding.ASCII.GetString (lookup_byte_buffer, 0, res); lookup_buffer.Append (s); keysym = (XKeySym) keysym_res.ToInt32 (); return res; } }
extern public static int XLookupString (ref XKeyEvent xevent, IntPtr buffer, int bytes_buffer, ref XKeySym keysym_return, IntPtr status_in_out);
private static extern int XKeysymToKeycode(IntPtr display, XKeySym keysym);
private int LookupString (ref XEvent xevent, int len, out XKeySym keysym, out XLookupStatus status) { IntPtr keysym_res; int res; status = XLookupStatus.XLookupNone; IntPtr xic = GetXic (client_window); if (xic != IntPtr.Zero && have_Xutf8LookupString && xevent.type == XEventName.KeyPress) { do { try { res = Xutf8LookupString (xic, ref xevent, lookup_byte_buffer, 100, out keysym_res, out status); } catch (EntryPointNotFoundException) { have_Xutf8LookupString = false; // call again, this time we'll go through the non-xic clause return LookupString (ref xevent, len, out keysym, out status); } if (status != XLookupStatus.XBufferOverflow) break; lookup_byte_buffer = new byte [lookup_byte_buffer.Length << 1]; } while (true); lookup_buffer.Length = 0; string s = Encoding.UTF8.GetString (lookup_byte_buffer, 0, res); lookup_buffer.Append (s); keysym = (XKeySym) keysym_res.ToInt32 (); return s.Length; } else { IntPtr statusPtr = IntPtr.Zero; lookup_buffer.Length = 0; res = XLookupString (ref xevent, lookup_buffer, len, out keysym_res, out statusPtr); keysym = (XKeySym) keysym_res.ToInt32 (); return res; } }
public int GrabKey(XKeySym keysym, XModMask modifiers, bool owner_events, XGrabMode pointer_mode, XGrabMode keyboard_mode) { return XGrabKey (display.Handle, XKeysymToKeycode (display.Handle, keysym), modifiers, Handle, owner_events, pointer_mode, keyboard_mode); }
public int UngrabKey(XKeySym keysym, XModMask modifiers) { return XUngrabKey (display.Handle, XKeysymToKeycode (display.Handle, keysym), modifiers, Handle); }
private int LookupString (ref XEvent xevent, int len, out XKeySym keysym, out IntPtr status) { IntPtr keysym_res; int res; status = IntPtr.Zero; IntPtr xic = GetXic (client_window); if (xic != IntPtr.Zero) { do { res = Xutf8LookupString (xic, ref xevent, utf8_buffer, 100, out keysym_res, out status); if ((int) status != -1) // XLookupBufferOverflow break; utf8_buffer = new byte [utf8_buffer.Length << 1]; } while (true); lookup_buffer.Length = 0; string s = Encoding.UTF8.GetString (utf8_buffer, 0, res); lookup_buffer.Append (s); keysym = (XKeySym) keysym_res.ToInt32 (); return s.Length; } else { lookup_buffer.Length = 0; res = XLookupString (ref xevent, lookup_buffer, len, out keysym_res, IntPtr.Zero); keysym = (XKeySym) keysym_res.ToInt32 (); return res; } }