

In this example we simply loop through the pressed keys, writing them out to debug. It also contains a method named GetPressedKeys() which returns an array of all the keys that are currently pressed. It all starts with a call to Keyboard.GetState(), this returns a struct containing the current state of the keyboard, including modifier keys like Control or Shift. Now let’s take a look at the keyboard specific code. In this particular example, when the user hits keys, they are logged to the debug console: It simply draws a sprite centered to the screen, then we manipulate the position in Update() We are going to re-use the same basic example for all the examples in this chapter. GraphicsDevice.Clear(Color.CornflowerBlue) Protected override void Draw(GameTime gameTime) Move our sprite based on arrow keys being pressed: Sb.Append("Key: ").Append(key).Append(" pressed ") sb = new StringBuilder() įoreach (var key in state.GetPressedKeys()) Print to debug console currently pressed keys KeyboardState state = Keyboard.GetState()

Protected override void Update(GameTime gameTime) SpriteBatch = new SpriteBatch(GraphicsDevice) Graphics = new GraphicsDeviceManager(this)
MONOGAME FOR VISUAL STUDIO 2013 CODE
Let’s start straight away with a code sample: using On the other hand, as you are about to see, the provided interfaces are incredibly consistent and easy to learn. All input in XNA is done via polling, if you want an event layer, you build it yourself or use one of the existing 3rd party implementations. If you come from another game engine or library you may be shocked to discovered there is no event driven interface out of the box for example. XNA input capabilities were at once powerful, straightforward and a bit lacking. There is an HD video of this chapter available here. XNA/MonoGame also have support for mobile specific input such as motion and touch screens, we will cover theses topics in a later topic. In this chapter we are going to explore handling input from the keyboard, mouse and gamepad in your MonoGame game.
