private void HookEx_Events(object sender, WindowsHookExEventArgs e) { if (KeyChange == null) { return; } KeyboardLLHookStruct @struct = (KeyboardLLHookStruct)Marshal.PtrToStructure((IntPtr)e.lParam, typeof(KeyboardLLHookStruct)); PressType pressType = PressType.None; VirtualKeys vk = (VirtualKeys)@struct.VirtualKeyCode; KeyboardState state = KeyboardState.FromCurrentState(); char?inputChar = null; if (e.wParam.ToInt64() == WM.KEYDOWN || e.wParam.ToInt64() == WM.SYSKEYDOWN) { pressType = PressType.KeyDown; } else if (e.wParam.ToInt64() == WM.KEYUP || e.wParam.ToInt64() == WM.SYSKEYUP) { pressType = PressType.KeyUp; } // Get Press Char char?PressKey = null; byte[] inBuffer = new byte[2]; if (User32.ToAscii(@struct.VirtualKeyCode, @struct.ScanCode, state.Bytes, inBuffer, @struct.Flags) == 1) { char ch = (char)inBuffer[0]; if (!char.IsControl(ch)) { PressKey = ch; if ((state.CapsLockToggled ^ state.ShiftPressed) && char.IsLetter(ch)) { PressKey = char.ToUpper(ch); } inputChar = PressKey; } } var args = new KeyChangeEventArgs() { Handled = false, PressType = pressType, Key = vk, KeyboardState = state, InputChar = inputChar, }; foreach (var action in KeyChange.GetInvocationList().Reverse()) { action.DynamicInvoke(this, args); if (args.Handled) { e.Handled = true; return; } } }
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { for (int c; (c = serialPort.ReadByte()) >= 0;) { var frame = protocol.NextRecv((byte)c); if (frame != null) { var ss = frame.Split(' '); KeyChange?.Invoke(this, new KeyChangeEventArgs { Key = int.Parse(ss[0]), Value = int.Parse(ss[1]) }); } } }
private void RegisterEvents() { _gamepadEventOpen.LeftAnalogStick_Changed += (p, v) => { BlockPos = v; KeyChange?.Invoke(this); }; _gamepadEventOpen.LeftTrigger_Changed += (p, v) => { var buttonActive = v > 0.5; if (ChangeCase == buttonActive) { return; } ChangeCase = buttonActive; KeyChange?.Invoke(this); }; _gamepadEventOpen.RightTrigger_Changed += (p, v) => { var buttonActive = v > 0.5; if (ChangeSymbols == buttonActive) { return; } ChangeSymbols = buttonActive; KeyChange?.Invoke(this); }; _gamepadEventOpen.ButtonA_Down += p => CharPos = new Vector2(0, -1); _gamepadEventOpen.ButtonB_Down += p => CharPos = new Vector2(-1, 0); _gamepadEventOpen.ButtonX_Down += p => CharPos = new Vector2(1, 0); _gamepadEventOpen.ButtonY_Down += p => CharPos = new Vector2(0, 1); _gamepadEventOpen.ButtonA_Down += DispatchKeyChange; _gamepadEventOpen.ButtonB_Down += DispatchKeyChange; _gamepadEventOpen.ButtonX_Down += DispatchKeyChange; _gamepadEventOpen.ButtonY_Down += DispatchKeyChange; _gamepadEventOpen.ButtonDPadLeft_Down += p => { MoveLeft = true; KeyChange?.Invoke(this); MoveLeft = false; }; _gamepadEventOpen.ButtonDPadRight_Down += p => { MoveRight = true; KeyChange?.Invoke(this); MoveRight = false; }; _gamepadEventOpen.ButtonLeftBumper_Down += p => { Delete = true; KeyChange?.Invoke(this); Delete = false; }; _gamepadEventOpen.ButtonRightBumper_Down += p => { Space = true; KeyChange?.Invoke(this); Space = false; }; _gamepadEventOpen.ButtonDPadDown_Down += p => { Enter = true; KeyChange?.Invoke(this); Enter = false; }; _gamepadEventOpen.ButtonBack_Down += p => { Console.WriteLine("open:back button"); OpenClose = true; KeyChange?.Invoke(this); OpenClose = false; Disable(); }; _gamepadEventOpen.ButtonDPadUp_Down += p => { Console.WriteLine("open:up button"); OpenClose = true; KeyChange?.Invoke(this); OpenClose = false; Disable(); }; _gamepadEventClosed.ButtonBack_Down += p => { Console.WriteLine("closed:back button"); OpenClose = true; KeyChange?.Invoke(this); OpenClose = false; Enable(); }; }
private void DispatchKeyChange(int player) { KeyChange?.Invoke(this); CharPos = new Vector2(); }
/// <summary> /// Creates a new <see cref="KeyEventArgs"/> instance. /// </summary> /// <param name="key">Which key state changed.</param> /// <param name="change">Whether a key got pressed or released.</param> public KeyEventArgs(KeyCode key, KeyChange change) { this.Key = key; this.Change = change; }