public Keys[] GetKeysPressed() { var keys = CurrentKeyboardState.GetPressedKeys(); var pkeys = PreviousKeyboardState.GetPressedKeys(); List <Keys> pressedKeys = new List <Keys>(); if (active) { foreach (var key in keys) { bool pressed = true; foreach (var pkey in pkeys) { if (pkey == key) { pressed = false; } } if (pressed) { pressedKeys.Add(key); } } } return(pressedKeys.ToArray()); }
//überprüft tastaturinput und schreib sie in den string, gibt über Flags zurück welche Tasten gedrückt wurden (im moment nur Enter) static public byte WriteInput(ref string changingText) { Keys[] lk = LastKeyboardState.GetPressedKeys(); Keys[] ck = CurrentKeyboardState.GetPressedKeys(); Keys[] k = ck.Except(lk).ToArray(); byte code = 0; string tmpstring = ""; bool shiftpressed = CurrentKeyboardState.IsKeyDown(Keys.RightShift) || CurrentKeyboardState.IsKeyDown(Keys.LeftShift); bool backpressed = false; for (int i = 0; i < k.Length; i++) { switch (k[i]) { case Keys.Back: backpressed = true; break; case Keys.OemPeriod: tmpstring += "."; break; case Keys.Space: tmpstring += " "; break; //Enter Flag Setzen case Keys.Enter: code |= 0x01; break; case Keys.LeftShift: case Keys.RightShift: shiftpressed = true; break; default: //Buchstabe zum string hinzufügen fall er sich im erlaubten Ascii bereich befindet if ((int)k[i] > 32 && (int)k[i] < 128) { tmpstring += (char)k[i]; } break; } } if (!shiftpressed) { changingText += tmpstring.ToLower(); } else { changingText += tmpstring.ToUpper(); } if (backpressed && changingText != "") { //Lösche Letztes Zeichen changingText = changingText.Substring(0, changingText.Length - 1); } return(code); }
/// <summary> /// Returns all the keys which were not down last frame, but are up now /// </summary> /// <returns></returns> public Keys[] GetPressedKeys() { if (!calculatedPressedKeys) { // Get all the keys in thisFrame that weren't in last frame PressedKeys = Array.FindAll(CurrentKeyboardState.GetPressedKeys(), x => Array.IndexOf(PreviousKeyboardState.GetPressedKeys(), x) == -1); calculatedPressedKeys = true; } return(PressedKeys); }
/// <summary> /// Gets the current number key pressed, returns -1 if none /// </summary> public int GetDigitPressed() { var pressedDigitKeys = CurrentKeyboardState.GetPressedKeys() // Select those that are within D0 and D9 .Where(x => x >= Keys.D0 && x <= Keys.D9) // Select those that weren't pressed last time .Where(x => !PreviousKeyboardState.GetPressedKeys().Contains(x)).ToArray(); if (pressedDigitKeys.Length == 0) { return(-1); } // D0 is 9, D1 is 0, D2 is 1, and so on... return(pressedDigitKeys[0] == Keys.D0 ? 0 : ((int)pressedDigitKeys[0]) - 48); }
public Keys GetDownedKey(List <Keys> acceptOnlyTheseKeys) // returns first key found. only applies if the key is still down in this frame/cycle { List <Keys> keysdown = new List <Keys>(CurrentKeyboardState.GetPressedKeys()); keysdown.Remove(Keys.None); foreach (Keys key in acceptOnlyTheseKeys) { if (keysdown.Contains(key)) { return(key); } } return(Keys.None); }
public List <Keys> GetAllReleasedKeys() // downed in prev state and now released { List <Keys> prevKeysPressed = new List <Keys>(PrevKeyboardState.GetPressedKeys()); List <Keys> currentKeysPressed = new List <Keys>(CurrentKeyboardState.GetPressedKeys()); prevKeysPressed.Remove(Keys.None); currentKeysPressed.Remove(Keys.None); List <Keys> result = new List <Keys>(); foreach (Keys key in prevKeysPressed) { if (!currentKeysPressed.Contains(key)) // dus als de oude list een key heeft die de nieuwe niet heeft dan is die dus released { result.Add(key); } } return(result); }
public void Update() { PreviousKeyboardState = CurrentKeyboardState; CurrentKeyboardState = Keyboard.GetState(); DownKeys.Clear(); PressedKeys.Clear(); Keys[] keys = CurrentKeyboardState.GetPressedKeys(); // TODO: per frame heap allocs foreach (Keys key in keys) { if (PreviousKeyboardState.IsKeyUp(key) && IsKeyDown(key)) { PressedKeys.Add(key); } //else //if (_previousKeyState.IsKeyDown(key) && _currentKeyState.IsKeyDown(key)) // DownKeys.Add(key); if (IsKeyDown(key)) { DownKeys.Add(key); } } }
/// <summary> /// Returns all the keys which are currently down /// </summary> /// <returns></returns> public Keys[] GetKeysDown() { return(CurrentKeyboardState.GetPressedKeys()); }
public static Keys[] KeysPressed() { return(CurrentKeyboardState.GetPressedKeys()); }
public bool AnyKeyPressed() { return(CurrentKeyboardState.GetPressedKeys().Length > 0); }
public static void UpdateControlStates(Vector2 origin) { PreviousGamePadState = CurrentGamePadState; PreviousKeyboardState = CurrentKeyboardState; PreviousMouseState = CurrentMouseState; CurrentKeyboardState = Keyboard.GetState(); CurrentGamePadState = GamePad.GetState(PlayerIndex.One); CurrentMouseState = Mouse.GetState(); InputDirection = Vector2.Zero; // InputDirection.X = 0f; // InputDirection.Y = 0f; //get mouse state and capture button type and respond button press MousePosition.X = CurrentMouseState.X; MousePosition.Y = CurrentMouseState.Y; bool leftMouseDown = CurrentMouseState.LeftButton == ButtonState.Pressed; if (leftMouseDown) { CurrentInputDirectionType = InputDirectionType.Full; Vector2 posDelta = MousePosition - origin; posDelta.Normalize(); InputDirection = posDelta; // posDelta = posDelta * playerMoveSpeed; // player.Position = player.Position + posDelta; } Vector2 analogDirection = CurrentGamePadState.ThumbSticks.Left; if (analogDirection != Vector2.Zero) { CurrentInputDirectionType = InputDirectionType.Scaled; InputDirection = analogDirection; InputDirection.Y = -InputDirection.Y; } Keys[] arrowKeysPressed = CurrentKeyboardState.GetPressedKeys(); Keys[] arrowKeys = { Keys.Left, Keys.Right, Keys.Up, Keys.Down }; var usingArrowKeys = arrowKeys.Where(arrowKeysPressed.Contains).Any(); var dPadInUse = CurrentGamePadState.DPad.Down == ButtonState.Pressed || CurrentGamePadState.DPad.Up == ButtonState.Pressed || CurrentGamePadState.DPad.Left == ButtonState.Pressed || CurrentGamePadState.DPad.Right == ButtonState.Pressed; Console.WriteLine("dPadInUse: " + dPadInUse); if (usingArrowKeys || dPadInUse) { CurrentInputDirectionType = InputDirectionType.Full; } if (CurrentKeyboardState.IsKeyDown(Keys.Left) || CurrentGamePadState.DPad.Left == ButtonState.Pressed) { InputDirection.X = -1f; } if (CurrentKeyboardState.IsKeyDown(Keys.Right) || CurrentGamePadState.DPad.Right == ButtonState.Pressed) { InputDirection.X = 1f; } if (CurrentKeyboardState.IsKeyDown(Keys.Down) || CurrentGamePadState.DPad.Down == ButtonState.Pressed) { InputDirection.Y = 1f; } if (CurrentKeyboardState.IsKeyDown(Keys.Up) || CurrentGamePadState.DPad.Up == ButtonState.Pressed) { InputDirection.Y = -1f; } }