private void OnUiControlGotFocus() { lock (KeyboardInputEvents) { foreach (var key in WinKeys.mapKeys) { var state = Win32Native.GetKeyState((int)key.Key); if ((state & 0x8000) == 0x8000) { KeyboardInputEvents.Add(new KeyboardInputEvent { Key = key.Value, Type = InputEventType.Down, OutOfFocus = true }); } } } }
private bool HandleKey(VirtualKey virtualKey, CorePhysicalKeyStatus keyStatus, InputEventType type) { // If our EditText TextBox is active, let's ignore all key events if (Game.Context is GameContextWindowsRuntime && ((GameContextWindowsRuntime)Game.Context).EditTextBox.Parent != null) { return(false); } // Remap certain keys switch (virtualKey) { case VirtualKey.Shift: // Only way to differentiate left and right shift is through the scan code virtualKey = keyStatus.ScanCode == 54 ? VirtualKey.RightShift : VirtualKey.LeftShift; break; case VirtualKey.Control: virtualKey = keyStatus.IsExtendedKey ? VirtualKey.RightControl : VirtualKey.LeftControl; break; case VirtualKey.Menu: virtualKey = keyStatus.IsExtendedKey ? VirtualKey.RightMenu : VirtualKey.LeftMenu; break; } // Let Alt + F4 go through if (virtualKey == VirtualKey.F4 && IsKeyDownNow(Keys.LeftAlt)) { return(false); } Keys key; if (!mapKeys.TryGetValue(virtualKey, out key)) { key = Keys.None; } lock (KeyboardInputEvents) { KeyboardInputEvents.Add(new KeyboardInputEvent { Key = key, Type = type }); } return(true); }
private void OnKeyEvent(WinFormsKeys keyCode, bool isKeyUp, bool outOfFocus = false) { Keys key; if (WinKeys.mapKeys.TryGetValue(keyCode, out key) && key != Keys.None) { var type = isKeyUp ? InputEventType.Up : InputEventType.Down; lock (KeyboardInputEvents) { if (!isKeyUp) { winformPressedKeys.Add(keyCode); } else { winformPressedKeys.Remove(keyCode); } KeyboardInputEvents.Add(new KeyboardInputEvent { Key = key, Type = type, OutOfFocus = outOfFocus }); } } }
private void DeviceOnKeyboardInput(object sender, KeyboardInputEventArgs rawKb) { // Code partially from: http://molecularmusings.wordpress.com/2011/09/05/properly-handling-keyboard-input/ var key = Keys.None; var virtualKey = rawKb.Key; var scanCode = rawKb.MakeCode; var flags = rawKb.ScanCodeFlags; if ((int)virtualKey == 255) { // discard "fake keys" which are part of an escaped sequence return; } if (virtualKey == WinFormsKeys.ShiftKey) { // correct left-hand / right-hand SHIFT virtualKey = (WinFormsKeys)WinKeys.MapVirtualKey(scanCode, WinKeys.MAPVK_VSC_TO_VK_EX); } else if (virtualKey == WinFormsKeys.NumLock) { // correct PAUSE/BREAK and NUM LOCK silliness, and set the extended bit scanCode = WinKeys.MapVirtualKey((int)virtualKey, WinKeys.MAPVK_VK_TO_VSC) | 0x100; } // e0 and e1 are escape sequences used for certain special keys, such as PRINT and PAUSE/BREAK. // see http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html bool isE0 = ((flags & ScanCodeFlags.E0) != 0); bool isE1 = ((flags & ScanCodeFlags.E1) != 0); if (isE1) { // for escaped sequences, turn the virtual key into the correct scan code using MapVirtualKey. // however, MapVirtualKey is unable to map VK_PAUSE (this is a known bug), hence we map that by hand. scanCode = virtualKey == WinFormsKeys.Pause ? 0x45 : WinKeys.MapVirtualKey((int)virtualKey, WinKeys.MAPVK_VK_TO_VSC); } switch (virtualKey) { // right-hand CONTROL and ALT have their e0 bit set case WinFormsKeys.ControlKey: virtualKey = isE0 ? WinFormsKeys.RControlKey : WinFormsKeys.LControlKey; break; case WinFormsKeys.Menu: virtualKey = isE0 ? WinFormsKeys.RMenu : WinFormsKeys.LMenu; break; // NUMPAD ENTER has its e0 bit set case WinFormsKeys.Return: if (isE0) { key = Keys.NumPadEnter; } break; } if (key == Keys.None) { WinKeys.mapKeys.TryGetValue(virtualKey, out key); } if (key != Keys.None) { bool isKeyUp = (flags & ScanCodeFlags.Break) != 0; if (isKeyUp) { lock (KeyboardInputEvents) { KeyboardInputEvents.Add(new KeyboardInputEvent { Key = key, Type = InputEventType.Up }); } } else { lock (KeyboardInputEvents) { KeyboardInputEvents.Add(new KeyboardInputEvent { Key = key, Type = InputEventType.Down }); } } } }