如何在不使用触摸屏的情况下模拟触摸事件 [英] How do I Emulate touch events without using touch screen

查看:48
本文介绍了如何在不使用触摸屏的情况下模拟触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试开发一个触摸屏应用程序:

I'm currently trying to develop a touch screen application with:

  • Windows 7
  • Visual Studio 2013
  • C# - WPF

我工作的地方将接收一个触摸屏(实际上是一个放置在平面屏幕上的层).

The place I'm working at is going to receive a touch screen (actually a layer to put on a flat screen).

我希望能够生成触摸输入,以便在没有屏幕的情况下开发和测试应用程序.

I would like to be able to generate touch inputs in order to develop and test the application without the screen.

我找到的所有资源,要么很旧,要么很复杂.

All the resources I find, are either quite old or complicated.

目前在没有触摸屏的情况下开发和测试触摸屏应用程序的最佳方法是什么?

What is the best way today to develop and test touch screen applications, without the touch screen ?

推荐答案

一种方法是将第二只鼠标连接到您的工作站.然后您可以测试多点触控(调整大小、旋转等).几年前我设法做到了.不过,您需要适当的驱动程序.请查看 moultitouch 项目.我想我正在使用那个或类似的东西.

One way to do it is to attach a second mouse to your work station. Then you can test multitouch (resizing, rotating, etc.). I managed to do that years ago. You need appropriate drivers though. Please check the moultitouch project. I think I was using that or something similar.

您可以在此超级用户帖子中找到更多建议.但我从未尝试过.

You can find more suggestions in this SuperUser post. But I never tried them.

编辑

在查看您的评论后,我更了解您的问题.试试这个 StackOverflow 线程.它正在讨论将鼠标重新路由到触摸事件中.另请查看 Blake.NUI 项目 - 它改进了 WPF 4 以更好地处理触摸交互(除其他外).

After checking your comments I understand your issue better. Try this StackOverflow thread. It's discussing rerouting mouse into touch events. Check also Blake.NUI project - it improves WPF 4 to better handle touch interaction (among other things).

在项目中,您将找到 MouseTouchDevice 类,它可以帮助您将鼠标转换为触摸事件:

In the project you will find MouseTouchDevice class that should help you converting mouse into touch 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
}

这篇关于如何在不使用触摸屏的情况下模拟触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆