public bool KeyReleased(Keys key, bool lastFrame = false) { if (lastFrame) { return(LastKeyboardState.IsKeyUp(key)); } return(KeyboardState.IsKeyUp(key)); }
public bool KeyPressed(Keys key, bool lastFrame = false) { if (lastFrame) { return(LastKeyboardState.IsKeyDown(key)); } return(KeyboardState.IsKeyDown(key)); }
public bool IsKeyReleased(Keys key) { if (CurrentKeyboardState.IsKeyUp(key) && (LastKeyboardState.IsKeyDown(key))) { return(true); } return(false); }
public bool KeyClicked(Keys key) { if (KeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key)) { return(true); } return(false); }
public bool KeyUp(params Keys[] keys) { var result = true; foreach (var key in keys) { if (LastKeyboardState.IsKeyUp(key)) { continue; } result = false; } return(result); }
/// <summary> /// Returns true is the given key was down last frame but isn't this frame /// </summary> /// <param name="key"></param> /// <returns></returns> public bool WasKeyDown(Keys key) { if (!Engine.Instance.WindowActive) { return(false); } if (LastKeyboardState.IsKeyDown(key)) { if (Keyboard.GetState().IsKeyUp(key)) { return(true); } } return(false); }
protected override void OnUpdateFrame(FrameEventArgs args) { base.OnUpdateFrame(args); if (LastKeyboardState.IsKeyDown(Key.Space)) { int iterations = 10; for (int i = 1; i < iterations; i++) { chip8.UpdateTimers((float)args.Time * 60 / iterations); chip8.Step(this); } UpdateTextureTarget(); } }
/* * Action up */ // public bool ActionUp(string action) { // if (InputType == InputType.GamePad) { // return GamePadMap.ContainsKey(action) && GamePadState.IsButtonDown(GamePadMap[action]); // } // return KeyboardMap.ContainsKey(action) && KeyboardState.IsKeyDown(KeyboardMap[action]); // } /* * Action pressed */ public bool ActionPressed(string action) { if (InputType == InputType.GamePad && GamePadMap.ContainsKey(action)) { foreach (Buttons button in GamePadMap[action]) { if (button != 0 && GamePadState.IsButtonDown(button) && !LastGamePadState.IsButtonDown(button)) { return(true); } } } else if (InputType == InputType.Keyboard && KeyboardMap.ContainsKey(action)) { foreach (Keys key in KeyboardMap[action]) { if (KeyboardState.IsKeyDown(key) && !LastKeyboardState.IsKeyDown(key)) { return(true); } } } return(false); // if (InputType == InputType.GamePad) // { // return ( // GamePadMap.ContainsKey(action) && // GamePadState.IsButtonDown(GamePadMap[action]) && // !LastGamePadState.IsButtonDown(GamePadMap[action]) // ); // } // return ( // KeyboardMap.ContainsKey(action) && // KeyboardState.IsKeyDown(KeyboardMap[action]) && // !LastKeyboardState.IsKeyDown(KeyboardMap[action]) // ); }
protected override void OnKeyDown(KeyboardKeyEventArgs e) { base.OnKeyDown(e); if (e.Key == Key.S) { int iterations = 1; if (LastKeyboardState.IsKeyDown(Key.ControlLeft)) { iterations = 100; } for (int i = 0; i < iterations; i++) { chip8.Step(this); } //chip8.DumpGfx(); UpdateTextureTarget(); } }
/// <summary> /// Helper for checking if a key was newly pressed during this update. The /// controllingPlayer parameter specifies which player to read input for. /// If this is null, it will accept input from any player. When a keypress /// is detected, the output playerIndex reports which player pressed it. /// </summary> public bool IsNewKeyPress(Keys key) { return(CurrentKeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key)); }
void target_KeyUp(object sender, KeyEventArgs e) { LastKeyboardState.Press(e.Key); CurrentKeyboardState.Leave(e.Key); }
public bool IsKeyReleased(Key key) { return(KeyboardState.IsKeyUp(key) && !LastKeyboardState.IsKeyUp(key)); }
/// <summary> /// Helper for checking if a key was released during this update. /// </summary> public bool IsKeyReleased(Key key) { return(LastKeyboardState.IsPressed(key) && !KeyboardState.IsPressed(key)); }
/// <summary> /// Update cursor and keyboard events /// </summary> private void UpdateCursorAndKeyboardEvents() { if (!Visible) { return; } MouseEvent mouseState = MouseHandler.GetState(); KeyboardState kbState = KeyboardHandler.GetState(); var mouseSize = new Vector2(5); var mouseRect = new Rectangle((int)mouseState.X, (int)mouseState.Y, (int)mouseSize.X, (int)mouseSize.Y); var thisRect = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); //First check position if (mouseRect.Intersects(thisRect)) { //Second, check buttons //Left if (LastMouseCheck.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Left); } if (LastMouseCheck.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Left); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Left); } //Right if (LastMouseCheck.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Right); } if (LastMouseCheck.RightButton == ButtonState.Pressed && mouseState.RightButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Right); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Right); } //Middle if (LastMouseCheck.MiddleButton == ButtonState.Released && mouseState.MiddleButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Middle); } if (LastMouseCheck.MiddleButton == ButtonState.Pressed && mouseState.MiddleButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Middle); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Middle); } //Check move if (LastMouseCheck.Position != mouseState.Position) { OnMouseMove?.Invoke(this, mouseState.Position, MouseButtons.None); } //Hook Keyboard if (kbState.GetPressedKeys().Count() > 0 && LastKeyboardState.GetPressedKeys().Count() > 0) { //check pressed keys List <Keys> pressedKeys = kbState.GetPressedKeys().Except(LastKeyboardState.GetPressedKeys()).ToList(); foreach (Keys p in pressedKeys) { OnKeyPress?.Invoke(this, p, kbState); } //check released keys List <Keys> releasedKeys = LastKeyboardState.GetPressedKeys().Except(kbState.GetPressedKeys()).ToList(); foreach (Keys r in releasedKeys) { OnKeyUp?.Invoke(this, r, kbState); OnKeyPress?.Invoke(this, r, kbState); } } } else { // Leave == Release //Left } LastMouseCheck = mouseState; LastKeyboardState = kbState; }
/// <summary> /// Returns an array of the keys pressed last frame /// </summary> /// <returns></returns> public Keys[] GetLastPressedKeys() { return(LastKeyboardState.GetPressedKeys()); }
public bool IsKeyHeldDown(Keys key) { return(LastKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyDown(key)); }
public bool HasKeyChanged(Key key) { return(LastKeyboardState.IsKeyDown(key) != KeyboardState.IsKeyDown(key)); }
public static bool IsNewKeyRelease(Keys key) { return(LastKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key)); }
protected override void OnUpdateFrame(FrameEventArgs e) { var timespan = _stopwatch.Elapsed; if (timespan.TotalMilliseconds >= (16 + _stopwatchOvershoot) || _fastForward == true) { _stopwatchOvershoot = 16 - timespan.TotalMilliseconds; if (_stopwatchOvershoot > 0) { _stopwatchOvershoot = 0; } _stopwatch.Restart(); _joypadState = GetJoypadState(); for (int i = 0; i < 17000; i++) { _soundPlayer.QueueAudioSample(Channels.Channel1, _gameBoy.GetAudioSample(Channels.Channel1)); _soundPlayer.QueueAudioSample(Channels.Channel2, _gameBoy.GetAudioSample(Channels.Channel2)); _soundPlayer.QueueAudioSample(Channels.Channel3, _gameBoy.GetAudioSample(Channels.Channel3)); _soundPlayer.QueueAudioSample(Channels.Channel4, _gameBoy.GetAudioSample(Channels.Channel4)); _gameBoy.AdvanceMachineCycle(_joypadState); } } if (KeyboardState.IsKeyDown(Key.Escape)) { Close(); } if (KeyboardState.IsKeyDown(Key.Tab)) { _fastForward = true; } else { _fastForward = false; } if (KeyboardState.IsKeyDown(Key.P) && LastKeyboardState.IsKeyUp(Key.P)) { if (Size.X < 640) { Size = new OpenToolkit.Mathematics.Vector2i(ClientSize.X + 160, ClientSize.Y + 144); } } if (KeyboardState.IsKeyDown(Key.M) && LastKeyboardState.IsKeyUp(Key.M)) { if (Size.X > 160) { Size = new OpenToolkit.Mathematics.Vector2i(ClientSize.X - 160, ClientSize.Y - 144); } } if (KeyboardState.IsKeyDown(Key.L) && LastKeyboardState.IsKeyUp(Key.L)) { _gameBoy.EnableLogging(); } base.OnUpdateFrame(e); }
protected bool IsUniquePress(KeyboardState state, Key key) { return(state.IsKeyDown(key) && !LastKeyboardState.IsKeyDown(key)); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { runGame = false; } // We need to load the gamedata // Implement later //TODO: Implement Storage //Update sounds audioEngine.Update(); // Update input LastGamePadState = CurrentGamePadState; CurrentGamePadState = GamePad.GetState(PlayerIndex.One); LastKeyboardState = CurrentKeyboardState; CurrentKeyboardState = Keyboard.GetState(); if (CurrentKeyboardState.IsKeyDown(Keys.F) && LastKeyboardState.IsKeyUp(Keys.F)) { graphics.ToggleFullScreen(); } //we have to wait 3 seconds until we start playing if (gameTime.TotalGameTime.Seconds > 3 && playingGame == false) { playingGame = true; } //after 3 seconds start the game if (playingGame) { //Update players Catapult playerCatapult.Update(gameTime); if (playerCatapult.CurrentState == CatapultState.Reset) { // reset background and log screenPosition = Vector2.Zero; endObjectPos.X = 1000; endObjectPos.Y = 500; } // Move the background if (playerCatapult.CurrentState == CatapultState.ProjectileFlying) { screenPosition.X = (playerCatapult.PumpkinPosition.X - playerCatapult.PumpkinLaunchPosition) * -1.0f; endObjectPos.X = (playerCatapult.PumpkinPosition.X - playerCatapult.PumpkinLaunchPosition) * -1.0f + 1000; } //Calculate the pumpkin flying distance if (playerCatapult.CurrentState == CatapultState.ProjectileFlying || playerCatapult.CurrentState == CatapultState.ProjectileHit) { PumpkinDistance = playerCatapult.PumpkinPosition.X - playerCatapult.PumpkinLaunchPosition; PumpkinDistance /= 15.0f; } //Is this a highscore? if (HighScore < PumpkinDistance) { HighScore = (int)PumpkinDistance; } } // Exit game if (!runGame) { Exit(); } base.Update(gameTime); }
public static bool KeyPress(Keys key) { return(KeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key)); }
public bool IsKeyPressed(Keys key) { return(LastKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key)); }
/// <summary> /// Helper for checking if a key was pressed during this update. /// </summary> public bool IsKeyPressed(Key key) { return(!LastKeyboardState.IsPressed(key) && KeyboardState.IsPressed(key)); }
protected override void Update(GameTime gameTime) { if (Window == null) { return; } if (Window.ClientBounds.Width != _clientWidth || Window.ClientBounds.Height != _clientHeight) { _clientWidth = Window.ClientBounds.Width; _clientHeight = Window.ClientBounds.Height; _graphics.PreferredBackBufferWidth = _clientWidth; _graphics.PreferredBackBufferHeight = _clientHeight; _graphics.ApplyChanges(); var p = GraphicsDevice.PresentationParameters; OnResolutionChanged(new ResolutionEventArgs(p.BackBufferWidth, p.BackBufferHeight)); } ShowFps(gameTime); LastKeyboardState = Keyboard.GetState(); if (!MouseExtras.Instance.IsForeground(this, Window)) { IsMouseVisible = true; Paused = true; if (MouseExtras.Instance.HasCapture(this, Window)) { MouseExtras.Instance.ReleaseCapture(); } MouseDelta = new Vector2I(); } else { bool pauseIsPressed = LastKeyboardState.IsKeyDown(Keys.Tab); if (!_pauseWasPressed && pauseIsPressed) { Paused = !Paused; if (Paused) { if (MouseExtras.Instance.HasCapture(this, Window)) { MouseExtras.Instance.ReleaseCapture(); } } IsMouseVisible = Paused; } _pauseWasPressed = pauseIsPressed; var mouseX = 0; var mouseY = 0; if (!Paused && MouseExtras.Instance.HasCapture(this, Window)) { var centerX = _clientWidth / 2; var centerY = _clientHeight / 2; var mouse = MouseExtras.Instance.GetPosition(Window); mouseX = mouse.X - centerX; mouseY = mouse.Y - centerY; } MouseDelta = new Vector2I(mouseX, mouseY); } if (LastKeyboardState.IsKeyDown(Keys.Escape)) { Exit(); return; } base.Update(gameTime); if (!Paused && MouseExtras.Instance.IsForeground(this, Window)) { if (!MouseExtras.Instance.HasCapture(this, Window)) { MouseExtras.Instance.SetCapture(Window); } var centerX = _clientWidth / 2; var centerY = _clientHeight / 2; MouseExtras.Instance.SetPosition(Window, centerX, centerY); } }
public bool KeyPressed(Keys key) => KeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key);
public bool IsKeyPressed(Key key) { return(KeyboardState.IsKeyDown(key) && !LastKeyboardState.IsKeyDown(key)); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { //Update mouse scroll state. MouseScrollHandler.Update(); UpdateGameActiveState(gameTime); if (IsPaused || IsFocusLost) { base.Update(gameTime); SuppressDraw(); return; } var mouseState = Mouse.GetState(); var keyboardState = Keyboard.GetState(); //Fullscreen toggle if (!IsInEditMode && (keyboardState.IsKeyDown(Keys.LeftAlt) || keyboardState.IsKeyDown(Keys.RightAlt))) { if (keyboardState.IsKeyDown(Keys.Enter) && LastKeyboardState.IsKeyUp(Keys.Enter)) { _graphics.ToggleFullScreen(); Globals.IsFullScreen = !Globals.IsFullScreen; } } //Map layer draw toggle if (IsInEditMode) { if (keyboardState.IsKeyDown(Keys.D1) && LastKeyboardState.IsKeyUp(Keys.D1)) { MapBase.SwitchLayerDraw(0); } if (keyboardState.IsKeyDown(Keys.D2) && LastKeyboardState.IsKeyUp(Keys.D2)) { MapBase.SwitchLayerDraw(1); } if (keyboardState.IsKeyDown(Keys.D3) && LastKeyboardState.IsKeyUp(Keys.D3)) { MapBase.SwitchLayerDraw(2); } if (keyboardState.IsKeyDown(Keys.D4) && LastKeyboardState.IsKeyUp(Keys.D4)) { MapBase.SwitchLayerDraw(3); } if (keyboardState.IsKeyDown(Keys.D5) && LastKeyboardState.IsKeyUp(Keys.D5)) { MapBase.SwitchLayerDraw(4); } } if (ScriptExecuter.IsInPlayingMovie) { //Stop movie when Esc key pressed if (keyboardState.IsKeyDown(Keys.Escape) && LastKeyboardState.IsKeyUp(Keys.Escape)) { ScriptExecuter.StopMovie(); } } else { //Update GUI first, GUI will decide whether user input be intercepted or pass through GuiManager.Update(gameTime); switch (GameState.State) { case GameState.StateType.Start: ScriptManager.RunScript(Utils.GetScriptParser("title.txt")); GameState.State = GameState.StateType.Title; break; case GameState.StateType.Title: break; case GameState.StateType.Playing: if (IsGamePlayPaused) { break; } UpdatePlaying(gameTime); break; default: throw new ArgumentOutOfRangeException(); } } //Update script after GuiManager, because script executing rely GUI state. ScriptManager.Update(gameTime); LastKeyboardState = Keyboard.GetState(); LastMouseState = mouseState; base.Update(gameTime); }
public bool IsKeyReleased(Keys key) { return(LastKeyboardState.IsKeyDown(key) && KeyboardState.IsKeyUp(key)); }