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; }
public ConsoleKeyEventArgs(KEY_EVENT_RECORD rec) { KeyDown = rec.KeyDown != 0; RepeatCount = rec.RepeatCount; VirtualKeyCode = rec.VirtualKeyCode; VirtualScanCode = rec.VirtualScanCode; UnicodeChar = rec.UnicodeChar; ControlKeys = rec.ControlKeys; }
public void ConsoleKeyEventArgs_ConstructorSetsCorrectValues() { KEY_EVENT_RECORD record = new KEY_EVENT_RECORD { KeyDown = 12, RepeatCount = 123, VirtualKeyCode = VirtualKey.Accept, UnicodeChar = 'x', VirtualScanCode = 321, ControlKeys = ControlKeyStates.RIGHT_ALT_PRESSED }; var sut = new ConsoleKeyEventArgs(record); sut.KeyDown.Should().BeTrue(); sut.ControlKeys.Should().Be(record.ControlKeys); sut.RepeatCount.Should().Be(123); sut.VirtualKeyCode.Should().Be(record.VirtualKeyCode); sut.UnicodeChar.Should().Be('x'); sut.VirtualScanCode.Should().Be(record.VirtualScanCode); }
protected override void OnKey(KEY_EVENT_RECORD e) { double moveSpeed = 5.0 * elapsed; if (e.bKeyDown == true) { if (e.UnicodeChar == 'w') { if (posX + dirX * moveSpeed > 0 && posX + dirX * moveSpeed < worldMap.GetLength(0)) { if (worldMap[(int)(posX + dirX * moveSpeed), (int)posY] == 0) { posX += dirX * moveSpeed; } if (worldMap[(int)(posX), (int)(posY + dirY * moveSpeed)] == 0) { posY += dirY * moveSpeed; } } } if (e.UnicodeChar == 's') { if (posX - dirX * moveSpeed > 0 && posX - dirX * moveSpeed < worldMap.GetLength(0)) { if (worldMap[(int)(posX - dirX * moveSpeed), (int)posY] == 0) { posX -= dirX * moveSpeed; } if (worldMap[(int)(posX), (int)(posY - dirY * moveSpeed)] == 0) { posY -= dirY * moveSpeed; } } } } }
protected virtual void OnKey(KEY_EVENT_RECORD e) { }
public void ParseInputEvent(INPUT_RECORD inputRecord, Control rootElement) { if (inputRecord.EventType == EventType.MOUSE_EVENT) { MOUSE_EVENT_RECORD mouseEvent = inputRecord.MouseEvent; if (mouseEvent.dwEventFlags != MouseEventFlags.PRESSED_OR_RELEASED && mouseEvent.dwEventFlags != MouseEventFlags.MOUSE_MOVED && mouseEvent.dwEventFlags != MouseEventFlags.DOUBLE_CLICK && mouseEvent.dwEventFlags != MouseEventFlags.MOUSE_WHEELED && mouseEvent.dwEventFlags != MouseEventFlags.MOUSE_HWHEELED) { // throw new InvalidOperationException("Flags combination in mouse event was not expected."); } Point rawPosition; if (mouseEvent.dwEventFlags == MouseEventFlags.MOUSE_MOVED || mouseEvent.dwEventFlags == MouseEventFlags.PRESSED_OR_RELEASED) { rawPosition = new Point(mouseEvent.dwMousePosition.X, mouseEvent.dwMousePosition.Y); lastMousePosition = rawPosition; } else { // При событии MOUSE_WHEELED в Windows некорректно устанавливается mouseEvent.dwMousePosition // Поэтому для определения элемента, над которым производится прокручивание колёсика, мы // вынуждены сохранять координаты, полученные при предыдущем событии мыши rawPosition = lastMousePosition; } Control topMost = VisualTreeHelper.FindTopControlUnderMouse(rootElement, Control.TranslatePoint(null, rawPosition, rootElement)); // если мышь захвачена контролом, то события перемещения мыши доставляются только ему, // события, связанные с нажатием мыши - тоже доставляются только ему, вместо того // контрола, над которым событие было зарегистрировано. Такой механизм необходим, // например, для корректной обработки перемещений окон (вверх или в стороны) Control source = (inputCaptureStack.Count != 0) ? inputCaptureStack.Peek() : topMost; // No sense to further process event with no source control if (source == null) { return; } if (mouseEvent.dwEventFlags == MouseEventFlags.MOUSE_MOVED) { MouseButtonState leftMouseButtonState = getLeftButtonState(mouseEvent.dwButtonState); MouseButtonState middleMouseButtonState = getMiddleButtonState(mouseEvent.dwButtonState); MouseButtonState rightMouseButtonState = getRightButtonState(mouseEvent.dwButtonState); // MouseEventArgs mouseEventArgs = new MouseEventArgs(source, Control.PreviewMouseMoveEvent, rawPosition, leftMouseButtonState, middleMouseButtonState, rightMouseButtonState ); eventsQueue.Enqueue(mouseEventArgs); // lastLeftMouseButtonState = leftMouseButtonState; lastMiddleMouseButtonState = middleMouseButtonState; lastRightMouseButtonState = rightMouseButtonState; // detect mouse enter / mouse leave events // path to source from root element down List <Control> mouseOverStack = new List <Control>(); Control current = topMost; while (null != current) { mouseOverStack.Insert(0, current); current = current.Parent; } int index; for (index = 0; index < Math.Min(mouseOverStack.Count, prevMouseOverStack.Count); index++) { if (mouseOverStack[index] != prevMouseOverStack[index]) { break; } } for (int i = prevMouseOverStack.Count - 1; i >= index; i--) { Control control = prevMouseOverStack[i]; MouseEventArgs args = new MouseEventArgs(control, Control.MouseLeaveEvent, rawPosition, leftMouseButtonState, middleMouseButtonState, rightMouseButtonState ); eventsQueue.Enqueue(args); } for (int i = index; i < mouseOverStack.Count; i++) { // enqueue MouseEnter event Control control = mouseOverStack[i]; MouseEventArgs args = new MouseEventArgs(control, Control.MouseEnterEvent, rawPosition, leftMouseButtonState, middleMouseButtonState, rightMouseButtonState ); eventsQueue.Enqueue(args); } prevMouseOverStack.Clear(); prevMouseOverStack.AddRange(mouseOverStack); } if (mouseEvent.dwEventFlags == MouseEventFlags.PRESSED_OR_RELEASED) { // MouseButtonState leftMouseButtonState = getLeftButtonState(mouseEvent.dwButtonState); MouseButtonState middleMouseButtonState = getMiddleButtonState(mouseEvent.dwButtonState); MouseButtonState rightMouseButtonState = getRightButtonState(mouseEvent.dwButtonState); // MouseButtonEventArgs eventArgs = null; if (leftMouseButtonState != lastLeftMouseButtonState) { eventArgs = new MouseButtonEventArgs(source, leftMouseButtonState == MouseButtonState.Pressed ? Control.PreviewMouseDownEvent : Control.PreviewMouseUpEvent, rawPosition, leftMouseButtonState, lastMiddleMouseButtonState, lastRightMouseButtonState, MouseButton.Left ); } if (middleMouseButtonState != lastMiddleMouseButtonState) { eventArgs = new MouseButtonEventArgs(source, middleMouseButtonState == MouseButtonState.Pressed ? Control.PreviewMouseDownEvent : Control.PreviewMouseUpEvent, rawPosition, lastLeftMouseButtonState, middleMouseButtonState, lastRightMouseButtonState, MouseButton.Middle ); } if (rightMouseButtonState != lastRightMouseButtonState) { eventArgs = new MouseButtonEventArgs(source, rightMouseButtonState == MouseButtonState.Pressed ? Control.PreviewMouseDownEvent : Control.PreviewMouseUpEvent, rawPosition, lastLeftMouseButtonState, lastMiddleMouseButtonState, rightMouseButtonState, MouseButton.Right ); } if (eventArgs != null) { eventsQueue.Enqueue(eventArgs); } // lastLeftMouseButtonState = leftMouseButtonState; lastMiddleMouseButtonState = middleMouseButtonState; lastRightMouseButtonState = rightMouseButtonState; if (leftMouseButtonState == MouseButtonState.Pressed) { if (eventArgs != null && !autoRepeatTimerRunning) { startAutoRepeatTimer(eventArgs); } } else { if (eventArgs != null && autoRepeatTimerRunning) { stopAutoRepeatTimer( ); } } } if (mouseEvent.dwEventFlags == MouseEventFlags.MOUSE_WHEELED) { MouseWheelEventArgs args = new MouseWheelEventArgs( topMost, Control.PreviewMouseWheelEvent, rawPosition, lastLeftMouseButtonState, lastMiddleMouseButtonState, lastRightMouseButtonState, mouseEvent.dwButtonState > 0 ? 1 : -1 ); eventsQueue.Enqueue(args); } } if (inputRecord.EventType == EventType.KEY_EVENT) { KEY_EVENT_RECORD keyEvent = inputRecord.KeyEvent; KeyEventArgs eventArgs = new KeyEventArgs( ConsoleApplication.Instance.FocusManager.FocusedElement, keyEvent.bKeyDown ? Control.PreviewKeyDownEvent : Control.PreviewKeyUpEvent); eventArgs.UnicodeChar = keyEvent.UnicodeChar; eventArgs.bKeyDown = keyEvent.bKeyDown; eventArgs.dwControlKeyState = keyEvent.dwControlKeyState; eventArgs.wRepeatCount = keyEvent.wRepeatCount; eventArgs.wVirtualKeyCode = keyEvent.wVirtualKeyCode; eventArgs.wVirtualScanCode = keyEvent.wVirtualScanCode; eventsQueue.Enqueue(eventArgs); } }
private static void ConsoleListener_KeyEvent(KEY_EVENT_RECORD r) { if (updatingHeader) { return; } if (gameState == GameState.Start && r.UnicodeChar == ' ') { Console.Clear(); UpdateHeader(); gameState = GameState.Playing; } if (gameState == GameState.GameOver && r.UnicodeChar == (char)27) { Environment.Exit(0); } if (gameState == GameState.GameOver && r.UnicodeChar == ' ') { gameBuffer.Clear(); commandBuffer.Clear(); Console.Clear(); gameSpeed = 1000; score = 0; lives = 3; timer.Interval = gameSpeed; gameState = GameState.Idle; } if (r.UnicodeChar == 'c') { activeColor = (System.ConsoleColor) new Random().Next(1, 15); } if (r.UnicodeChar == 'b') { activeBrush = characterBrushes[new Random().Next(0, 3)]; } if (r.UnicodeChar == 'u') { if (commandBuffer.Count > 0) { var lastCommand = commandBuffer.Last(); commandBuffer.Remove(lastCommand); var previousCommandAtPosition = commandBuffer.LastOrDefault(cc => cc.X == lastCommand.X && cc.Y == lastCommand.Y); if (previousCommandAtPosition != null) { previousCommandAtPosition.Execute(); } else { lastCommand.Clear(); } } } if (r.UnicodeChar == 's') { using (var sw = new System.IO.StreamWriter(fileName)) { foreach (var cc in commandBuffer) { sw.WriteLine(cc.X); sw.WriteLine(cc.Y); sw.WriteLine(cc.Value); } } } if (r.UnicodeChar == 'l') { if (System.IO.File.Exists(fileName)) { // Load commandBuffer = new List <ConsoleCommand>(); Console.Clear(); using (var sw = new System.IO.StreamReader(fileName)) { while (!sw.EndOfStream) { int x = int.Parse(sw.ReadLine()); int y = int.Parse(sw.ReadLine()); string value = sw.ReadLine(); commandBuffer.Add(new ConsoleCommand(x, y, value)); } } // Replay buffer foreach (var cc in commandBuffer) { cc.Execute(); } } } }
internal KeyMessage(KEY_EVENT_RECORD record) { _record = record; }
public static ConsoleKeyInfo AsConsoleKeyInfo(KEY_EVENT_RECORD record) { var(ctrl, alt, shift) = record.dwControlKeyState.GetModifiers(); return(new ConsoleKeyInfo(record.UnicodeChar, (ConsoleKey)record.wVirtualKeyCode, shift, alt, ctrl)); }
public Win32ConsoleKeyboardEvent(KEY_EVENT_RECORD record) : base(record.bKeyDown, record.wRepeatCount, record.UnicodeChar) { _record = record; }