public X11Keyboard() { Debug.WriteLine("Using X11Keyboard."); state.IsConnected = true; IntPtr display = API.DefaultDisplay; using (new XLock(display)) { // Find the number of keysyms per keycode. int first = 0, last = 0; API.DisplayKeycodes(display, ref first, ref last); IntPtr keysym_ptr = API.GetKeyboardMapping(display, (byte)first, last - first + 1, ref KeysymsPerKeycode); Functions.XFree(keysym_ptr); if (Xkb.IsSupported(display)) { // Request that auto-repeat is only set on devices that support it physically. // This typically means that it's turned off for keyboards what we want). // We prefer this method over XAutoRepeatOff/On, because the latter needs to // be reset before the program exits. bool supported; Xkb.SetDetectableAutoRepeat(display, true, out supported); KeyMap = new X11KeyMap(display); } } }
private void ProcessRawEvent(ref XGenericEventCookie cookie) { lock (Sync) { unsafe { XIRawEvent raw = *(XIRawEvent *)cookie.data; XIMouse mouse; XIKeyboard keyboard; switch (raw.evtype) { case XIEventType.RawMotion: if (GetMouseDevice(raw.deviceid, out mouse)) { ProcessRawMotion(mouse, ref raw); } break; case XIEventType.RawButtonPress: case XIEventType.RawButtonRelease: if (GetMouseDevice(raw.deviceid, out mouse)) { float dx, dy; MouseButton button = X11KeyMap.TranslateButton(raw.detail, out dx, out dy); mouse.State[button] = raw.evtype == XIEventType.RawButtonPress; if (mouse.ScrollX.number == -1 && mouse.ScrollY.number == -1) { mouse.State.SetScrollRelative(dx, dy); } } break; case XIEventType.RawKeyPress: case XIEventType.RawKeyRelease: if (GetKeyboardDevice(raw.deviceid, out keyboard)) { Key key; if (KeyMap.TranslateKey(raw.detail, out key)) { keyboard.State[key] = raw.evtype == XIEventType.RawKeyPress; } } break; } } } }
private bool TranslateKeyX11(ref XKeyEvent e, out Key key) { XKey keysym = (XKey)API.LookupKeysym(ref e, 0); XKey keysym2 = (XKey)API.LookupKeysym(ref e, 1); key = X11KeyMap.TranslateXKey(keysym); if (key == Key.Unknown) { key = X11KeyMap.TranslateXKey(keysym2); } if (key == Key.Unknown) { Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2); } return(key != Key.Unknown); }
public XI2MouseKeyboard() { window = new X11WindowInfo(); window.Display = Functions.XOpenDisplay(IntPtr.Zero); using (new XLock(window.Display)) { XSetWindowAttributes attr = new XSetWindowAttributes(); window.Screen = Functions.XDefaultScreen(window.Display); window.RootWindow = Functions.XRootWindow(window.Display, window.Screen); window.Handle = Functions.XCreateWindow(window.Display, window.RootWindow, 0, 0, 1, 1, 0, 0, CreateWindowArgs.InputOnly, IntPtr.Zero, SetWindowValuemask.Nothing, attr); KeyMap = new X11KeyMap(window.Display); } if (!IsSupported(window.Display)) { throw new NotSupportedException("XInput2 not supported."); } // Enable XI2 mouse/keyboard events // Note: the input event loop blocks waiting for these events // *or* a custom ClientMessage event that instructs us to exit. // See SendExitEvent() below. using (new XLock(window.Display)) using (XIEventMask mask = new XIEventMask(1, XIEventMasks.RawKeyPressMask | XIEventMasks.RawKeyReleaseMask | XIEventMasks.RawButtonPressMask | XIEventMasks.RawButtonReleaseMask | XIEventMasks.RawMotionMask | XIEventMasks.MotionMask | XIEventMasks.DeviceChangedMask)) { XI.SelectEvents(window.Display, window.RootWindow, mask); UpdateDevices(); } ProcessingThread = new Thread(ProcessEvents); ProcessingThread.IsBackground = true; ProcessingThread.Start(); }