This morning I didn’t have internet access and was trying to find this in my notes (which are also my blog posts), and I couldn’t; so purely for my own benefit, the following is a method of handling touch or mouse input in Monogame:
public void HandleInput()
{
ProcessMouseInput();
ProcessTouchInput();
ProcessKeyboardInput();
}
private void ProcessMouseInput()
{
var currentMouse = Microsoft.Xna.Framework.Input.Mouse.GetState();
if (currentMouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
{
HandlePressed(currentMouse.Position, \_controlledObject);
}
}
private void ProcessTouchInput()
{
var currentTouch = Microsoft.Xna.Framework.Input.Touch.TouchPanel.GetState();
foreach (var touch in currentTouch.Where(t => t.State == Microsoft.Xna.Framework.Input.Touch.TouchLocationState.Pressed))
{
HandlePressed(touch.Position, \_controlledObject);
}
}
private void ProcessKeyboardInput()
{
var currentKeys = Microsoft.Xna.Framework.Input.Keyboard.GetState();
if (currentKeys.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
{
\_handler.GoLeft(\_controlledObject);
By changing Pressed to Released, you can detect when the mouse button click / touch finished; however, if you hold your finger / mouse button down, it will only return a single result.