Working with Touch Input (Windows Phone)

Complete Sample

The code in this topic shows you the technique for detecting and using multitouch input. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download InputToyWP7

XNA Game Studio 4.0 Refresh supports multitouch input on Windows Phone OS 7.1. The primary class that provides this support is TouchPanel, which can:

Note

If you are interested in responding to only the first (primary) touch on the screen, use the mouse technique described in Getting the Mouse Position (Windows, Windows Phone). This technique, in addition to being simpler, allows you to use the same code for Windows Phone and Windows.

Determining the Capabilities of the Touch Input Device

By using TouchPanel.GetCapabilities you can determine if the touch panel is available. You also can determine the maximum touch count (the number of touches that can be detected simultaneously).

To determine the capabilities of the touch device

  1. Call TouchPanel.GetCapabilities, which returns a TouchPanelCapabilities structure.

  2. Ensure TouchPanelCapabilities.IsConnected is true, indicating that the touch panel is available for reading.

  3. You then can use the TouchPanelCapabilities.MaximumTouchCount property to determine how many touch points are supported by the touch panel.

Note

All touch panels for Windows Phone return a MaximumTouchCount value of 4 on XNA Game Studio 4.0 Refresh.

The following code demonstrates how to determine if the touch panel is connected, and then reads the maximum touch count.

TouchPanelCapabilities tc = TouchPanel.GetCapabilities();
if(tc.IsConnected)
{
    return tc.MaximumTouchCount;
}

Getting Multitouch Data from the Touch Input Device

You can use TouchPanel.GetState to get the current state of the touch input device. It returns a TouchCollection structure that contains a set of TouchLocation structures, each containing information about position and state for a single touchpoint on the screen.

To read multitouch data from the touch input device

  1. Call TouchPanel.GetState to get a TouchCollection representing the current state of the device.

  2. For each TouchLocation in the TouchCollection, read the location and state data provided for each touchpoint.

The following code demonstrates how to get the current state of the touch input device and read touch data from each TouchLocation. It checks to see if a touch location has been pressed or has moved since the last frame, and if so, draws a sprite at the touch location.

// Process touch events
TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
{
    if ((tl.State == TouchLocationState.Pressed)
            || (tl.State == TouchLocationState.Moved))
    {

        // add sparkles based on the touch location
        sparkles.Add(new Sparkle(tl.Position.X,
                 tl.Position.Y, ttms));

    }
}

See Also

Reference

Microsoft.Xna.Framework.Input.Touch
TouchPanel
TouchPanelCapabilities
TouchLocation
TouchLocationState