public void HandleKeyDown(int player_index, KeyPressed key) { if (!KeysDown.Contains(key)) { KeysDown.Add(key); } }
public void HandleKeyRelease(int player_index, KeyPressed key) { if (KeysDown.Contains(key)) { KeysDown.Remove(key); } }
void form_KeyUp(object sender, KeyEventArgs e) { Keys key = e.KeyData & ~Keys.Shift; KeysReleased.Add(key); KeysDown.Remove(key); }
public XkeyPressedEventArgs(List <KeyBase> keysDown, bool shiftKeyDown, bool ctrlKeyDown, bool altKeyDown, string customData) { CustomData = customData; KeysDown.AddRange(keysDown); ShiftKeyDown = shiftKeyDown; CtrlKeyDown = ctrlKeyDown; AltKeyDown = altKeyDown; }
void form_KeyDown(object sender, KeyEventArgs e) { Keys key = e.KeyData & ~Keys.Shift; if (KeysDown.Contains(key) == false) { KeysPressed.Add(key); KeysDown.Add(key); } }
/// <summary> /// Handles keyboard input. /// </summary> /// <param name="clickData"></param> /// <param name="time"></param> public static void UpdateKeyboardInput(GameTime time) { GetChanges(out List <Keys> pressed, out List <Keys> down, out List <Keys> released); if (pressed.Count > 0) { int length = pressed.Count; for (int i = 0; i < length; i++) { KeysPressed?.Invoke(null, pressed[i]); } } if (down.Count > 0) { int length = down.Count; for (int i = 0; i < length; i++) { KeysDown?.Invoke(null, down[i]); } } if (released.Count > 0) { int length = released.Count; for (int i = 0; i < length; i++) { KeysReleased?.Invoke(null, released[i]); } } KeyboardState keyboardState = Keyboard.GetState(); Modifiers = KeyboardModifiers.None; if (keyboardState.IsKeyDown(Keys.LeftControl) || keyboardState.IsKeyDown(Keys.RightControl)) { Modifiers |= KeyboardModifiers.Control; } if (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift)) { Modifiers |= KeyboardModifiers.Shift; } if (keyboardState.IsKeyDown(Keys.LeftAlt) || keyboardState.IsKeyDown(Keys.RightAlt)) { Modifiers |= KeyboardModifiers.Alt; } }
public void Device_KeyboardInput(object sender, KeyboardInputEventArgs e) { if (e.State == KeyState.Pressed) { if (KeysDown.Contains(e.Key) == false) { KeysPressed.Add(e.Key); KeysDown.Add(e.Key); } } else if (e.State == KeyState.Released) { KeysReleased.Add(e.Key); KeysDown.Remove(e.Key); } }
// 鼠标按钮按下选中文本 private void mouseDownSelectText() { if (MouseButtons.Left.Equals(MouseDownButton)) // 点击的左键 { Point p = MouseDownLocation; if (p.X < 0) { p.X = 0; } if (p.Y < 0) { p.Y = 0; } int mouseIndex = this.GetCharIndexFromPosition(p); int selIndex = this.SelectionStart; if (KeysDown.Equals(Keys.ShiftKey)) // 按下了shift { if (mouseIndex < selIndex) { this.SelectionStart = mouseIndex; } else { this.SelectionStart = SelectionStart; } this.SelectionLength = Math.Abs(mouseIndex - selIndex); } else { if (isMouseEnterSelectText(p) /*|| SelectionLength == 0*/) { ControlsUtils.AsynchronousMethod(this, 120, new EventHandler((object sender1, EventArgs e1) => { if (!Cursor.Equals(selectTextDragCur) && MouseDownButton.Equals(MouseButtons.None)) { SelectionLength = 0; SelectionStart = mouseIndex; } })); } else { SelectionLength = 0; SelectionStart = mouseIndex; } } } }
public List <Tuple <string, KeyDownStates> > GetKeyOverrides() { // Combine all requests for key state overrides var overrideKeyStates = new List <Tuple <string, KeyDownStates> >(); KeysUp?.ForEach(keyString => { overrideKeyStates.Add(Tuple.Create(keyString, KeyDownStates.Up)); }); KeysDown?.ForEach(keyString => { overrideKeyStates.Add(Tuple.Create(keyString, KeyDownStates.Down)); }); KeysLockedDown?.ForEach(keyString => { overrideKeyStates.Add(Tuple.Create(keyString, KeyDownStates.LockedDown)); }); return(overrideKeyStates); }
/// <summary> /// Tracks key presses, filters keys and updates data bindings. /// </summary> protected override void OnKeyDown(KeyRoutedEventArgs args) { // Fire before event BeforeKeyDown?.Invoke(this, args); // Call before event handler OnBeforeKeyDown(args); // Process key press unless blocked if (!args.Handled) { if (DirectInput) { // Attempt to translate and enter key directly when read-only var upperCase = Uppercase || KeysDown.Contains(VirtualKey.Shift) || KeysDown.Contains(VirtualKey.LeftShift) || KeysDown.Contains(VirtualKey.RightShift) || KeysDown.Contains(VirtualKey.CapitalLock); var inputText = KeyboardKeyConverter.ConvertToString((KeyboardKey)args.Key, upperCase, CultureInfo.CurrentCulture); if (inputText != null) { // Update text directly... this.SendKeys(inputText); args.Handled = true; } } else { // Let system handle key press base.OnKeyDown(args); } } // Track pressed keys (add to keys down if not already present) if (!_keysDown.Contains(args.Key)) { _keysDown.Add(args.Key); } // Call after event handler OnAfterKeyDown(args); }
public bool KeyRelease(Key key) { return(!KeysDown.Contains(key) && KeysDownLast.Contains(key)); }
public override void Update() { // Character movement: should be camera-aware { // Left stick: movement var moveDirection = Input.GetLeftThumbAny(DeadZone); // Keyboard: movement if (KeysLeft.Any(key => Input.IsKeyDown(key))) { moveDirection += -Vector2.UnitX; } if (KeysRight.Any(key => Input.IsKeyDown(key))) { moveDirection += +Vector2.UnitX; } if (KeysUp.Any(key => Input.IsKeyDown(key))) { moveDirection += +Vector2.UnitY; } if (KeysDown.Any(key => Input.IsKeyDown(key))) { moveDirection += -Vector2.UnitY; } // Broadcast the movement vector as a world-space Vector3 to allow characters to be controlled var worldSpeed = (Camera != null) ? Utils.LogicDirectionToWorldDirection(moveDirection, Camera, Vector3.UnitY) : new Vector3(moveDirection.X, 0, moveDirection.Y); // Adjust vector's magnitute - worldSpeed has been normalized var moveLength = moveDirection.Length(); var isDeadZoneLeft = moveLength < DeadZone; if (isDeadZoneLeft) { worldSpeed = Vector3.Zero; } else { if (moveLength > 1) { moveLength = 1; } else { moveLength = (moveLength - DeadZone) / (1f - DeadZone); } worldSpeed *= moveLength; } MoveDirectionEventKey.Broadcast(worldSpeed); } // Camera rotation: left-right rotates the camera horizontally while up-down controls its altitude { // Right stick: camera rotation var cameraDirection = Input.GetRightThumbAny(DeadZone); var isDeadZoneRight = cameraDirection.Length() < DeadZone; if (isDeadZoneRight) { cameraDirection = Vector2.Zero; } else { cameraDirection.Normalize(); } // Mouse-based camera rotation. Only enabled after you click the screen to lock your cursor, pressing escape cancels this if (Input.IsMouseButtonDown(MouseButton.Left)) { Input.LockMousePosition(true); Game.IsMouseVisible = false; } if (Input.IsKeyPressed(Keys.Escape)) { Input.UnlockMousePosition(); Game.IsMouseVisible = true; } if (Input.IsMousePositionLocked) { cameraDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity; } // Broadcast the camera direction directly, as a screen-space Vector2 CameraDirectionEventKey.Broadcast(cameraDirection); } // Jumping: don't bother with jump restrictions here, just pass the button states { // Controller: jumping var isJumpDown = Input.IsGamePadButtonDownAny(GamePadButton.A); var didJump = (!jumpButtonDown && isJumpDown); jumpButtonDown = isJumpDown; // Keyboard: jumping didJump |= (KeysJump.Any(key => Input.IsKeyPressed(key))); JumpEventKey.Broadcast(didJump); } }
public override void Update() { MoveDirection = Vector2.Zero; AimDirection = Vector2.Zero; // Left stick: movement var padDirection = Input.GetLeftThumb(ControllerIndex); var isDeadZone = padDirection.Length() < DeadZone; if (!isDeadZone) { MoveDirection = padDirection; } MoveDirection.Normalize(); // Right stick: aim padDirection = Input.GetRightThumb(ControllerIndex); var aimSpeed = padDirection.Length(); isDeadZone = aimSpeed < DeadZone; // Make sure aim starts at 0 when outside deadzone aimSpeed = (aimSpeed - DeadZone) / (1.0f - DeadZone); // Clamp aim speed if (aimSpeed > 1.0f) { aimSpeed = 1.0f; } // Curve aim speed aimSpeed = (float)Math.Pow(aimSpeed, 1.6); if (!isDeadZone) { AimDirection = padDirection; AimDirection.Normalize(); AimDirection *= aimSpeed; } // Keyboard move if (KeysLeft.Any(key => Input.IsKeyDown(key))) { MoveDirection += -Vector2.UnitX; } if (KeysRight.Any(key => Input.IsKeyDown(key))) { MoveDirection += +Vector2.UnitX; } if (KeysUp.Any(key => Input.IsKeyDown(key))) { MoveDirection += +Vector2.UnitY; } if (KeysDown.Any(key => Input.IsKeyDown(key))) { MoveDirection += -Vector2.UnitY; } var isAiming = KeysAim.Any(key => Input.IsKeyDown(key)) || Input.GetLeftTrigger(ControllerIndex) >= DeadZone; var isFiring = KeysShoot.Any(key => Input.IsKeyDown(key)) || Input.GetRightTrigger(ControllerIndex) >= DeadZone; var isStarting = KeysStart.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Start); var isReloading = KeysReload.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Y); var isInteracting = KeysInteract.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.A); if (isStarting) { OnStart?.Invoke(Entity); } if (isReloading) { OnReload?.Invoke(Entity); } if (isInteracting) { OnInteract?.Invoke(Entity); } // Mouse aim (after normalization of aim direction) // mouse aim is only enabled after you click the screen to lock your cursor, pressing escape cancels this if (Input.IsMouseButtonDown(MouseButton.Left)) { Input.LockMousePosition(true); } if (Input.IsKeyPressed(Keys.Escape)) { Input.UnlockMousePosition(); } if (Input.IsMousePositionLocked) { // Mouse shooting if (Input.IsMouseButtonDown(MouseButton.Left)) { isFiring = true; } // Mouse aiming if (Input.IsMouseButtonDown(MouseButton.Right)) { isAiming = true; } AimDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity; } if (InvertXAxis) { AimDirection = new Vector2(-AimDirection.X, AimDirection.Y); } if (InvertYAxis) { AimDirection = new Vector2(AimDirection.X, -AimDirection.Y); } AimState = isAiming; FireState = isFiring; }
// Add a key to the list public void HandleKeyDown(int player_index, KeyPressed key) { state.HandleKeyDownInput(player_index, key); //Debug.WriteLine("KeyPressed = " + key); if (!KeysDown.Contains(key)) { KeysDown.Add(key); } //-----dbleTap----- if (dbleTapCounter == 2) { dbleTapCounter = 0; } if (dbleTapCounter == 0) { dbleTapTimer = dbleTapTime; } dbleTapCounter++; //-----Combo detection----- if (state.canCombo) // if this state can combo { // which item? switch (key) { // Switch to first item case (KeyPressed.Attack1): if (items[0] != null) { state.ChangeState(items[0].GetState(0, this, key)); // A button } break; // switch to second case (KeyPressed.Attack2): if (items[1] != null) { state.ChangeState(items[1].GetState(1, this, key)); // B button } break; // switch to third case (KeyPressed.Attack3): if (items[2] != null) { state.ChangeState(items[2].GetState(2, this, key)); // X button } break; // switch to fourth case (KeyPressed.Attack4): if (items[3] != null) { state.ChangeState(items[3].GetState(3, this, key)); // Y button } break; } } // Add the new key, there will always be 3 keys in comboKeys. comboKeys.Add(key); if (comboKeys.Count > 3) { comboKeys.RemoveAt(0); } // This is cleared in the update after the timer is 0 // Are we in a combo sequence? (Subequent keys pressed within a time limit) if (comboTimer > 0 && comboKeys.Count == 3) { CheckForCombo(); } comboTimer = comboTime; // reset combo timer // (Debug) Print the key list /*string s = ""; * for(int i = 0; i < comboKeys.Count; i++) * { * s += comboKeys[i].ToString() + " "; * }*/ //Debug.WriteLine("Keys: " + s); }
public void Update() { mouseDown = false; mouseUp = false; mouseStay = false; if (Input.touchSupported) { for (var i = 0; i < Input.touchCount; i++) { var touch = Input.GetTouch(i); switch (touch.phase) { case TouchPhase.Began: mouseDown = true; break; case TouchPhase.Moved: case TouchPhase.Stationary: mouseStay = true; break; case TouchPhase.Ended: case TouchPhase.Canceled: mouseUp = true; break; } } } else { if (Input.GetMouseButtonDown(0)) { mouseDown = true; } if (Input.GetMouseButton(0)) { mouseStay = true; } if (Input.GetMouseButtonUp(0)) { mouseUp = true; } var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); MousePositionWorldUnits = new Vector2(mousePosition.x, mousePosition.y); } KeysDown.Clear(); KeysUp.Clear(); KeysStay.Clear(); foreach (KeyCode keycode in Enum.GetValues(typeof(KeyCode))) { if (Input.GetKeyDown(keycode)) { KeysDown.Add(keycode); } if (Input.GetKey(keycode)) { KeysStay.Add(keycode); } if (Input.GetKeyUp(keycode)) { KeysStay.Add(keycode); } } }
public void ProcessEvents() { void PerformEventForInstances(EventType eventType, int eventNumber = 0) { ForInstances(i => i.PerformEvent(eventType, eventNumber)); } // Process begin step events ForInstances(i => { i.XPrevious = i.X; i.YPrevious = i.Y; i.PerformEvent(EventType.Step, (int)StepKind.BeginStep); }); // Process alarm events ForInstances(i => { for (var j = 0; j < 12; j++) { if (i.Alarm[j] >= 0 && --i.Alarm[j] == 0) { i.PerformEvent(EventType.Alarm, j); } } }); // Process keyboard events { var anyKey = false; foreach (var keyCode in KeysDown) { if (!Input.CheckKeyPressed(keyCode)) { continue; } anyKey = true; PerformEventForInstances(EventType.Keyboard, (int)keyCode); } // vk_any/vk_none PerformEventForInstances(EventType.KeyPress, anyKey ? (int)VirtualKeyCode.AnyKey : (int)VirtualKeyCode.NoKey); } // Process key press events if (KeysPressed.Count > 0) { while (KeysPressed.Count > 0) { var keyCode = KeysPressed.Dequeue(); PerformEventForInstances(EventType.KeyPress, (int)keyCode); } // vk_any PerformEventForInstances(EventType.KeyPress, (int)VirtualKeyCode.AnyKey); } else { // vk_none PerformEventForInstances(EventType.KeyPress, (int)VirtualKeyCode.NoKey); } // Process key release events { var released = new HashSet <VirtualKeyCode>(); foreach (var key in KeysDown) { if (!Input.CheckKeyPressed(key)) { released.Add(key); PerformEventForInstances(EventType.KeyRelease, (int)key); } } // vk_any/vk_none PerformEventForInstances(EventType.KeyRelease, released.Any() ? (int)VirtualKeyCode.AnyKey : (int)VirtualKeyCode.NoKey); // update KeysDown KeysDown.ExceptWith(released); } // Process step events and set instances to their new positions ForInstances(i => i.FullStep()); // Process collision events ProcessCollisionEvents(); // Process end step events PerformEventForInstances(EventType.Step, (int)StepKind.EndStep); // Draw the screen and process draw events DrawScreen(); }
/// <summary> /// Returns true if the key is in the <see cref="KeysDown"/> collection. /// </summary> /// <param name="key">The key to check.</param> /// <returns>True when the key is being pressed.</returns> public bool IsKeyDown(Keys key) { return(KeysDown.Contains(AsciiKey.Get(key))); }
public bool KeyPress(Key key) { return(KeysDown.Contains(key) && !KeysDownLast.Contains(key)); }
public bool KeyDown(Key key) { return(KeysDown.Contains(key)); }
/// <summary> /// Called before the <see cref="UIElement.KeyDown" /> event is processed. /// Set <see cref="KeyRoutedEventArgs.Handled"/> true to block the key press from being processed by the base control. /// Use the <see cref="KeyRoutedEventArgs.Key"/> to examine the current key press, the <see cref="KeysDown"/> collection /// does not contain the current key unless it was previously pressed to support repeat detection. /// This class implements grouped control navigation in this method. /// If you override this method and do not call it this functionality will be disabled. /// </summary> protected virtual void OnBeforeKeyDown(KeyRoutedEventArgs args) { // Handle specific key presses var selectIndex = SelectionStart; var selectLength = SelectionLength; var text = Text; var textLength = text.Length; var overtype = MaxLength > 0 && textLength >= MaxLength; var selectionMax = overtype ? textLength - 1 : textLength; var shift = KeysDown.Contains(VirtualKey.Shift) || KeysDown.Contains(VirtualKey.LeftShift) || KeysDown.Contains(VirtualKey.RightShift); switch (args.Key) { case VirtualKey.Delete: // Delete args.Handled = true; DoDelete(); break; case VirtualKey.Back: // Backspace args.Handled = true; DoBackspace(); break; case VirtualKey.Left: { // Move or extend selection left within text box, or previous group args.Handled = true; if (shift) { // Extend selection when shift is held if (selectIndex > 0) { selectIndex--; selectLength++; this.SelectText(selectIndex, selectLength); } } else if (selectIndex > 0 || selectLength > (overtype ? 1 : 0)) { // Move cursor and cancel any extended selection when shift is not held if (selectLength <= (overtype ? 1 : 0)) { selectIndex--; } selectLength = 0; this.SelectText(selectIndex, selectLength); } else { // Shift not held and no selection... // Moving left at edge moves to previous in group when specified MoveLeftInGroup(); } break; } case VirtualKey.Right: { // Move or extend selection right within text box, or move to next group args.Handled = true; if (shift) { // Extend selection when shift is held if (selectIndex + selectLength < textLength) { selectLength++; this.SelectText(selectIndex, selectLength); } } else if (selectIndex < selectionMax) { // Move cursor and cancel any extended selection when shift is not held selectIndex += selectLength > (overtype ? 1 : 0) ? selectLength : 1; selectLength = 0; this.SelectText(selectIndex, selectLength); } else { // Shift not held... // Moving right at edge moves to next in group when specified MoveRightInGroup(); } break; } case VirtualKey.Up: { // Move up within group if specified args.Handled = true; MoveUpInGroup(); break; } case VirtualKey.Down: { // Move down within group if specified args.Handled = true; MoveDownInGroup(); break; } } }
public override void Update() { // Character movement // The character movement can be controlled by a game controller or a keyboard // The character receives input in 3D world space, so that it can be controlled by an AI script as well // For this reason we map the 2D user input to a 3D movement using the current camera { // Game controller: left stick var moveDirection = Input.GetLeftThumbAny(DeadZone); var isDeadZoneLeft = moveDirection.Length() < DeadZone; if (isDeadZoneLeft) { moveDirection = Vector2.Zero; } else { moveDirection.Normalize(); } // Keyboard if (KeysLeft.Any(key => Input.IsKeyDown(key))) { moveDirection += -Vector2.UnitX; } if (KeysRight.Any(key => Input.IsKeyDown(key))) { moveDirection += +Vector2.UnitX; } if (KeysUp.Any(key => Input.IsKeyDown(key))) { moveDirection += +Vector2.UnitY; } if (KeysDown.Any(key => Input.IsKeyDown(key))) { moveDirection += -Vector2.UnitY; } // Broadcast the movement vector as a world-space Vector3 to allow characters to be controlled var worldSpeed = (Camera != null) ? Utils.LogicDirectionToWorldDirection(moveDirection, Camera, Vector3.UnitY) : new Vector3(moveDirection.X, 0, moveDirection.Y); // If we don't have the correct camera attached we can send the directions anyway, but they probably won't match MoveDirectionEventKey.Broadcast(worldSpeed); } // Camera rotation // Camera rotation is ALWAYS in camera space, so we don't need to account for View or Projection matrices { // Game controller: right stick var cameraDirection = Input.GetRightThumbAny(DeadZone); var isDeadZoneRight = cameraDirection.Length() < DeadZone; if (isDeadZoneRight) { cameraDirection = Vector2.Zero; } else { cameraDirection.Normalize(); } // Mouse-based camera rotation. // Only enabled after you click the screen to lock your cursor, pressing escape will cancel it. if (Input.IsMouseButtonDown(MouseButton.Left)) { Input.LockMousePosition(true); Game.IsMouseVisible = false; } if (Input.IsKeyPressed(Keys.Escape)) { Input.UnlockMousePosition(); Game.IsMouseVisible = true; } if (Input.IsMousePositionLocked) { cameraDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity; } // Broadcast the camera direction directly, as a screen-space Vector2 CameraDirectionEventKey.Broadcast(cameraDirection); } { // Controller: Right trigger // Mouse: Left button, Tap events var didShoot = Input.GetRightTriggerAny(0.2f) > 0.2f; // This will allow for continuous shooting if (Input.PointerEvents.Any(x => x.EventType == PointerEventType.Pressed)) { didShoot = true; } if (Input.HasMouse && Input.IsMouseButtonDown(MouseButton.Left)) // This will allow for continuous shooting { didShoot = true; } ShootEventKey.Broadcast(didShoot); } { // Reload weapon var isReloading = Input.IsGamePadButtonDownAny(GamePadButton.X); if (KeysReload.Any(key => Input.IsKeyDown(key))) { isReloading = true; } ReloadEventKey.Broadcast(isReloading); } }
private void GameOnKeyDown(object sender, KeyboardKeyEventArgs keyboardKeyEventArgs) { KeysDown.Add(keyboardKeyEventArgs.Key); }
/// <summary> /// Clears the <see cref="KeysPressed"/>, <see cref="KeysDown"/>, <see cref="KeysReleased"/> collections. /// </summary> public void Clear() { KeysPressed.Clear(); KeysDown.Clear(); KeysReleased.Clear(); }
private void GameOnKeyUp(object sender, KeyboardKeyEventArgs keyboardKeyEventArgs) { KeysDown.RemoveAll(i => i == keyboardKeyEventArgs.Key); }