【问题标题】:Simulate touch input on Microsoft Surface Window在 Microsoft Surface Window 上模拟触摸输入
【发布时间】:2012-05-12 15:14:46
【问题描述】:

我正在尝试将 Anoto-Pen 用作 TouchDeviceSurfaceInkCanvas
笔使用打印在一张纸上的坐标系来推导它的位置,然后这个位置数据被发送到我的应用程序。在那里我尝试通过子类化TouchDevice 将其转换为TouchInput,并使用TouchDevice.ReportDown();TouchDevice.ReportMove() 等将发送位置数据和事件转换为.NET Touch-Events。移动ScatterViewItems 并处理按钮“点击” “到目前为止有效。

现在的问题是,当我尝试在InkCanvas 上写字时,只画了点。观察触发的事件后,InkCanvas 似乎没有收到OnTouchMove 事件。

我在SurfaceInkCanvas 上注册了TouchDownTouchMoveTouchUp 的事件处理程序。 TouchDown 永远不会被触发。 TouchMoveTouchUp 仅当我从 SurfaceInkCanvas 之外开始然后移动到其中的某个点时。

这是我的TouchDevice的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Text.RegularExpressions;
using System.Windows;
using PaperDisplay.PenInput;
using System.Windows.Media;
using System.Windows.Threading;

namespace TouchApp
{
public class PenTouchDevice : TouchDevice
{
    public Point Position { get; set; }
    public Person Person { get; set; }

    public PenTouchDevice(Person person)
        : base(person.GetHashCode())
    {
        Person = person;
    }

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

    public override TouchPoint GetTouchPoint(System.Windows.IInputElement relativeTo)
    {
        Point point = Position;
        if (relativeTo != null)
        {
            point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position);
        }
        return new TouchPoint(this, point, new Rect(point, new Size(2.0, 2.0)), TouchAction.Move);
    }

    public void PenDown(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            SetActiveSource(PresentationSource.FromVisual(Person.Window));
            Position = GetPosition(args);
            if (!IsActive)
            {
                Activate();
            }
            ReportDown();
        }));
    }

    public void PenUp(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            Position = GetPosition(args);
            if (IsActive)
            {
                ReportUp();
                Deactivate();
            }
        }));

    }

    public void PenMove(PenPointInputArgs args, Dispatcher dispatcher)
    {
        dispatcher.BeginInvoke((Action)(() =>
        {
            if (IsActive)
            {
                Position = GetPosition(args);
                ReportMove();
            }
        }));
    }

    public Point GetPosition(PenPointInputArgs args)
    {
        double adaptedX = args.Y - 0.01;
        double adaptedY = (1 - args.X) - 0.005;
        return new Point(adaptedX * Person.Window.ActualWidth, adaptedY * Person.Window.ActualHeight);
    }
}
}

我的App.xaml.cs 中有以下代码,每次出现笔输入时都会调用它:

public void HandleEvent(object sender, EventArgs args)
    {
        if (typeof(PointInputArgs).IsAssignableFrom(args.GetType()))
        {
            PenPointInputArgs pointArgs = (PenPointInputArgs)args;
            switch (pointArgs.EventType)
            {
                case InputEvent.Down: touchDevice1.PenDown(pointArgs, this.Dispatcher); break;
                case InputEvent.Up: touchDevice1.PenUp(pointArgs, this.Dispatcher); break;
                case InputEvent.Move:
                case InputEvent.MoveDown: touchDevice1.PenMove(pointArgs, this.Dispatcher); break;
            }

        }
    }

提前谢谢你。

【问题讨论】:

    标签: c# touch device pixelsense inkcanvas


    【解决方案1】:

    GetIntermediateTouchPoints 不能返回一个空数组,它应该至少包含一个点

    public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo)
    {
        TouchPointCollection refCollection = new TouchPointCollection();
        refCollection.Add(GetTouchPoint(relativeTo));
        return refCollection;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多