internal MouseEventHandler(NativeDLL.MOUSE_EVENT_RECORD record) { Position = new Point(record.dwMousePosition.X, record.dwMousePosition.Y); ButtonState = (ButtonState)record.dwButtonState; ControlKeyState = (ControlKeyState)record.dwControlKeyState; MouseEventFlag = (MouseEventFlag)record.dwEventFlags; }
private void EmitKeyEvents(Kernel32.KEY_EVENT_RECORD kr) { var keyCode = kr.virtualKeyCode; if (kr.keyDown == 0 && keyCode != Constants.vk_alt) { // Unicode IME input comes through as KeyUp event with no accompanying KeyDown. return; } ControlKeyState state = (ControlKeyState)kr.controlKeyState; bool shift = (state & ControlKeyState.ShiftPressed) != 0; bool alt = (state & (ControlKeyState.LeftAltPressed | ControlKeyState.RightAltPressed)) != 0; bool control = (state & (ControlKeyState.LeftCtrlPressed | ControlKeyState.RightCtrlPressed)) != 0; ConsoleKeyInfo info = new ConsoleKeyInfo((char)kr.uChar, (ConsoleKey)kr.virtualKeyCode, shift, alt, control); // alt codes unicode entry // deal with virtual keys? // deal with mod keys held down/repeated keys? (maybe higher level?) // compare to nix OnKey(info); }
public MouseEvent(short x, short y, ButtonState buttonState, ControlKeyState controlKeyState, MouseEventFlags mouseEventFlags) { this.X = x; this.Y = y; this.ButtonState = buttonState; this.ControlKeyState = controlKeyState; this.MouseEventFlags = mouseEventFlags; }
internal KeyboardEventHandler(KEY_EVENT_RECORD record) { IsKeyDown = record.bKeyDown; RepeatCount = record.wRepeatCount; VirtualKeyCode = record.wVirtualKeyCode; VirtualScanCode = record.wVirtualScanCode; UnicodeChar = record.UnicodeChar; ControlKeyState = (ControlKeyState)record.dwControlKeyState; }
private void RootFrame_Key_Released(VirtualKeys Key, ControlKeyState KeyModifiers) { addLog("Key Released: " + (KeyModifiers > 0 ? (KeyModifiers.ToString() + " + " + Key.ToString()) : Key.ToString())); }
private void RootFrame_Key_Pressed(VirtualKeys Key, ControlKeyState KeyModifiers) { addLog("Key Pressed: " + (KeyModifiers > 0 ? (KeyModifiers.ToString() + " + " + Key.ToString()) : Key.ToString())); // get player movement keys, then make sure the map is walkable if (Key == VirtualKeys.W && _caveMap.IsWalkable(_player.X, _player.Y - 1)) { _player.Y--; } if (Key == VirtualKeys.D && _caveMap.IsWalkable(_player.X + 1, _player.Y)) { _player.X++; } if (Key == VirtualKeys.S && _caveMap.IsWalkable(_player.X, _player.Y + 1)) { _player.Y++; } if (Key == VirtualKeys.A && _caveMap.IsWalkable(_player.X - 1, _player.Y)) { _player.X--; } // move the map frame around if (Key == VirtualKeys.Up) { _mapFrame.Y--; } if (Key == VirtualKeys.Right) { _mapFrame.X++; } if (Key == VirtualKeys.Down) { _mapFrame.Y++; } if (Key == VirtualKeys.Left) { _mapFrame.X--; } // hide, or unhide the console cursor if (Key == VirtualKeys.Tab) { SetCursorVisibility(1, !CursorVisible); addLog("Cursor Visible: " + CursorVisible.ToString()); } // check to see if there's a downfloor. if there is, generate a new map if (KeyModifiers == ControlKeyState.ShiftPressed) { // KEY: SHIFT + . = > if (Key == VirtualKeys.OEMPeriod) { if (_caveMap.DownFloorPosition == _player) { string msg = "Do you wish to drop down a floor? yes/no\n"; using (ConsoleFrame frame = new ConsoleFrame((Width / 2) - (msg.Length / 2), 10, msg.Length, 3)) { frame.SetCursorVisibility(100, true); frame.Write(0, 1, msg, ConsoleColor.White, ConsoleColor.DarkBlue, true); frame.WriteBuffer(); VirtualKeys ans = frame.ReadAsVirtualKey(); if (ans == VirtualKeys.Y) { frame.Clear(); frame.Write(1, 1, "Generating cave...", ConsoleColor.White); frame.WriteBuffer(); _caveMap.Generate(80, 80); _player = _caveMap.UpFloorPosition; } frame.SetCursorVisibility(1, false); } } } } // see if the player wants to quit if (KeyModifiers == ControlKeyState.LeftCtrlPressed || KeyModifiers == ControlKeyState.RightCtrlPressed) { // KEYS: CTRL + Q if (Key == VirtualKeys.Q) { string msg = "Are you sure you want to quit? yes/no\n"; using (ConsoleFrame frame = new ConsoleFrame((Width / 2) - (msg.Length / 2), 10, msg.Length, 3)) { frame.SetCursorVisibility(100, true); frame.Write(0, 1, msg, ConsoleColor.White, ConsoleColor.DarkBlue, true); frame.WriteBuffer(); VirtualKeys ans = frame.ReadAsVirtualKey(); if (ans == VirtualKeys.Y) { Stop(); _caveMap = null; } frame.SetCursorVisibility(1, false); } } } }
public static ConsoleKeyInfo ReadKey(bool intercept) { Interop.InputRecord ir; int numEventsRead = -1; bool r; lock (s_readKeySyncObject) { if (_cachedInputRecord.eventType == Interop.KEY_EVENT) { // We had a previous keystroke with repeated characters. ir = _cachedInputRecord; if (_cachedInputRecord.keyEvent.repeatCount == 0) { _cachedInputRecord.eventType = -1; } else { _cachedInputRecord.keyEvent.repeatCount--; } // We will return one key from this method, so we decrement the // repeatCount here, leaving the cachedInputRecord in the "queue". } else { // We did NOT have a previous keystroke with repeated characters: while (true) { r = Interop.mincore.ReadConsoleInput(InputHandle, out ir, 1, out numEventsRead); if (!r || numEventsRead == 0) { // This will fail when stdin is redirected from a file or pipe. // We could theoretically call Console.Read here, but I // think we might do some things incorrectly then. throw new InvalidOperationException(SR.InvalidOperation_ConsoleReadKeyOnFile); } short keyCode = ir.keyEvent.virtualKeyCode; // First check for non-keyboard events & discard them. Generally we tap into only KeyDown events and ignore the KeyUp events // but it is possible that we are dealing with a Alt+NumPad unicode key sequence, the final unicode char is revealed only when // the Alt key is released (i.e when the sequence is complete). To avoid noise, when the Alt key is down, we should eat up // any intermediate key strokes (from NumPad) that collectively forms the Unicode character. if (!IsKeyDownEvent(ir)) { // REVIEW: Unicode IME input comes through as KeyUp event with no accompanying KeyDown. if (keyCode != AltVKCode) { continue; } } char ch = (char)ir.keyEvent.uChar; // In a Alt+NumPad unicode sequence, when the alt key is released uChar will represent the final unicode character, we need to // surface this. VirtualKeyCode for this event will be Alt from the Alt-Up key event. This is probably not the right code, // especially when we don't expose ConsoleKey.Alt, so this will end up being the hex value (0x12). VK_PACKET comes very // close to being useful and something that we could look into using for this purpose... if (ch == 0) { // Skip mod keys. if (IsModKey(ir)) { continue; } } // When Alt is down, it is possible that we are in the middle of a Alt+NumPad unicode sequence. // Escape any intermediate NumPad keys whether NumLock is on or not (notepad behavior) ConsoleKey key = (ConsoleKey)keyCode; if (IsAltKeyDown(ir) && ((key >= ConsoleKey.NumPad0 && key <= ConsoleKey.NumPad9) || (key == ConsoleKey.Clear) || (key == ConsoleKey.Insert) || (key >= ConsoleKey.PageUp && key <= ConsoleKey.DownArrow))) { continue; } if (ir.keyEvent.repeatCount > 1) { ir.keyEvent.repeatCount--; _cachedInputRecord = ir; } break; } } // we did NOT have a previous keystroke with repeated characters. } ControlKeyState state = (ControlKeyState)ir.keyEvent.controlKeyState; bool shift = (state & ControlKeyState.ShiftPressed) != 0; bool alt = (state & (ControlKeyState.LeftAltPressed | ControlKeyState.RightAltPressed)) != 0; bool control = (state & (ControlKeyState.LeftCtrlPressed | ControlKeyState.RightCtrlPressed)) != 0; ConsoleKeyInfo info = new ConsoleKeyInfo((char)ir.keyEvent.uChar, (ConsoleKey)ir.keyEvent.virtualKeyCode, shift, alt, control); if (!intercept) { Console.Write(ir.keyEvent.uChar); } return(info); }
public bool Matches(KeyEventArgs args) { VirtualKeys wVirtualKeyCode = args.wVirtualKeyCode; if (this.Key != wVirtualKeyCode) { return(false); } ControlKeyState controlKeyState = args.dwControlKeyState; ModifierKeys modifierKeys = this.Modifiers; // Проверяем все возможные модификаторы по очереди if ((modifierKeys & ModifierKeys.Alt) != 0) { if ((controlKeyState & (ControlKeyState.LEFT_ALT_PRESSED | ControlKeyState.RIGHT_ALT_PRESSED)) == 0) { // Должен быть взведён один из флагов, показывающих нажатие Alt, а его нет return(false); } } else { if ((controlKeyState & (ControlKeyState.LEFT_ALT_PRESSED | ControlKeyState.RIGHT_ALT_PRESSED)) != 0) { // Не должно быть взведено ни одного флага, показывающего нажатие Alt, // а на самом деле - флаг стоит return(false); } } if ((modifierKeys & ModifierKeys.Control) != 0) { if ((controlKeyState & (ControlKeyState.LEFT_CTRL_PRESSED | ControlKeyState.RIGHT_CTRL_PRESSED)) == 0) { return(false); } } else { if ((controlKeyState & (ControlKeyState.LEFT_CTRL_PRESSED | ControlKeyState.RIGHT_CTRL_PRESSED)) != 0) { return(false); } } if ((modifierKeys & ModifierKeys.Shift) != 0) { if ((controlKeyState & (ControlKeyState.SHIFT_PRESSED)) == 0) { return(false); } } else { if ((controlKeyState & (ControlKeyState.SHIFT_PRESSED)) != 0) { return(false); } } return(true); }
public KeyPressEventArgs(ConsoleKey key, ControlKeyState keyState) { Key = key; KeyState = keyState; }
public KeyEvent(bool keyDown, char character, ushort repeatCount, VirtualKey virtualKey, ControlKeyState controlKeyState) { this.KeyDown = keyDown; this.Character = character; this.RepeatCount = repeatCount; this.VirtualKey = virtualKey; this.ControlKeyState = controlKeyState; this.Handled = false; }
/// <summary> /// New mouse event args /// </summary> /// <param name="buttonState">The status of the mouse buttons</param> /// <param name="controlKeyState">The status of the control key state</param> public MousePressEventArgs(MouseButtonState buttonState, ControlKeyState controlKeyState) { ButtonState = buttonState; ControlKeyState = controlKeyState; }
protected void getInput() { uint read = 0; uint readEvents = 0; if (APICall.GetNumberOfConsoleInputEvents(_hConsoleIn, out read) && read > 0) { INPUT_RECORD[] eventBuffer = new INPUT_RECORD[read]; APICall.ReadConsoleInput(_hConsoleIn, eventBuffer, read, out readEvents); for (int i = 0; i < readEvents; i++) { ControlKeyState conState = eventBuffer[i].KeyEvent.dwControlKeyState; conState &= ~( ControlKeyState.NumLockOn | ControlKeyState.ScrollLockOn | ControlKeyState.CapsLockOn); switch (eventBuffer[i].EventType) { case EventType.KEY_EVENT: if (eventBuffer[i].KeyEvent.bKeyDown) { Key_Pressed?.Invoke(eventBuffer[i].KeyEvent.wVirtualKeyCode, conState); } else { Key_Released?.Invoke(eventBuffer[i].KeyEvent.wVirtualKeyCode, conState); } break; case EventType.MOUSE_EVENT: if (eventBuffer[i].MouseEvent.dwEventFlags == MouseEventType.MOUSE_MOVED) { Mouse_Moved?.Invoke( eventBuffer[i].MouseEvent.dwMousePosition.X, eventBuffer[i].MouseEvent.dwMousePosition.Y); } if (eventBuffer[i].MouseEvent.dwEventFlags == MouseEventType.MOUSE_WHEELED) { MouseWheeled dir; if (eventBuffer[i].MouseEvent.dwButtonState > 0) { dir = MouseWheeled.Up; } else { dir = MouseWheeled.Down; } Mouse_Wheeled?.Invoke( dir); } if (eventBuffer[i].MouseEvent.dwButtonState > 0) { if (eventBuffer[i].MouseEvent.dwEventFlags == MouseEventType.DOUBLE_CLICK) { MouseButton_DoubleClicked?.Invoke( eventBuffer[i].MouseEvent.dwMousePosition.X, eventBuffer[i].MouseEvent.dwMousePosition.Y, eventBuffer[i].MouseEvent.dwButtonState); } else { MouseButton_Clicked?.Invoke( eventBuffer[i].MouseEvent.dwMousePosition.X, eventBuffer[i].MouseEvent.dwMousePosition.Y, eventBuffer[i].MouseEvent.dwButtonState); } } break; } } } }
public static bool IsControlPressed(this ControlKeyState state) { return((state & (ControlKeyState.LEFT_CTRL_PRESSED | ControlKeyState.RIGHT_CTRL_PRESSED)) != 0); }