17

I'm currently developing a touch screen application using C# (.NET 4.0) and WPF for Windows 7. My problem is that the driver of the touch screen I have available at the moment only generates mouse events. (The manufacturer unfortunately does not provide a genuine Windows 7 driver) So, currently I'm not able to do tests properly.

Is there a generic way to tell Windows 7 that a certain device is supposed to be a touch device (although this -- of course -- could only provide single touch events)?

Seven
  • 4,353
  • 2
  • 27
  • 30

2 Answers2

14

Check this. http://blakenui.codeplex.com/. There is a MouseTouchDevice.cs file that looks like this. It converts normal mouse events to Manipulation events.

/// <summary>
/// Used to translate mouse events into touch events, enabling a unified 
/// input processing pipeline.
/// </summary>
/// <remarks>This class originally comes from Blake.NUI - http://blakenui.codeplex.com</remarks>
public class MouseTouchDevice : TouchDevice, ITouchDevice
{
    #region Class Members

    private static MouseTouchDevice device;

    public Point Position { get; set; }

    #endregion

    #region Public Static Methods

    public static void RegisterEvents(FrameworkElement root)
    {
        root.PreviewMouseDown += MouseDown;
        root.PreviewMouseMove += MouseMove;
        root.PreviewMouseUp += MouseUp;
        root.LostMouseCapture += LostMouseCapture;
        root.MouseLeave += MouseLeave;
    }

    #endregion

    #region Private Static Methods

    private static void MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (device != null &&
            device.IsActive)
        {
            device.ReportUp();
            device.Deactivate();
            device = null;
        }
        device = new MouseTouchDevice(e.MouseDevice.GetHashCode());
        device.SetActiveSource(e.MouseDevice.ActiveSource);
        device.Position = e.GetPosition(null);
        device.Activate();
        device.ReportDown();
    }

    private static void MouseMove(object sender, MouseEventArgs e)
    {
        if (device != null &&
            device.IsActive)
        {
            device.Position = e.GetPosition(null);
            device.ReportMove();
        }
    }

    private static void MouseUp(object sender, MouseButtonEventArgs e)
    {
        LostMouseCapture(sender, e);
    }

    static void LostMouseCapture(object sender, MouseEventArgs e)
    {
        if (device != null &&
            device.IsActive)
        {
            device.Position = e.GetPosition(null);
            device.ReportUp();
            device.Deactivate();
            device = null;
        }
    }

    static void MouseLeave(object sender, MouseEventArgs e)
    {
        LostMouseCapture(sender, e);
    }

    #endregion

    #region Constructors

    public MouseTouchDevice(int deviceId) :
        base(deviceId)
    {
        Position = new Point();
    }

    #endregion

    #region Overridden methods

    public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo)
    {
        return new TouchPointCollection();
    }

    public override TouchPoint GetTouchPoint(IInputElement relativeTo)
    {
        Point point = Position;
        if (relativeTo != null)
        {
            point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position);
        }

        Rect rect = new Rect(point, new Size(1, 1));

        return new TouchPoint(this, point, rect, TouchAction.Move);
    }

    #endregion
}

}

I am hoping this is what you are looking for.

Rana
  • 156
  • 1
  • 3
  • Exactly what I was going to post, except that you beat me to the punch! Well done :) – Hugo Jan 10 '11 at 22:20
  • What if I have no MouseButtonEventArgs ?? How would I generate the touch events ? please see my question here: http://stackoverflow.com/questions/9716439/wpf-how-to-synthesize-a-touch-event-from-scratch – simo Mar 15 '12 at 09:36
  • If you do not want to fire Touch promoted mouse events twice. Use this one if (e.StylusDevice != null) return; – Andreas Oct 22 '13 at 16:56
0

You would need to rewrite the mouse driver to act like a touch device to do this. A simpler workaround would be to get a device like the Wacom Bamboo Touch? It's a real touch device (not a touch screen).

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • OK, so there is no such thing as an in-between-mouse-driver which can be configured to produce mouse or touch events? (May "somebody" should write one :) ) The idea with a Wacom tablet is not bad, so you could take the device along to different PCs. – Seven Aug 11 '10 at 15:06
  • It's certainly _possible_ to write a filter driver that wraps the mouse driver and converts it to touch data. It's not trivial to do so, though. (I wouldn't do it, but if you have a talented developer who has already written a couple of drivers, then go for it!) Certainly the touch device will be cheaper. :) – Eric Brown Aug 11 '10 at 16:53