title: Simulate touch input on Microsoft Surface Window
body: <p>i am trying to use a Anoto-Pen as a <code>TouchDevice</code> with <code>SurfaceInkCanvas</code>.<br>
  The pen uses a coordinate-system printed on a sheet of paper to derive its position and this positional data then is send to my application. There i try to transform it to <code>TouchInput</code> by subclassing <code>TouchDevice</code> and convert the send positional data and events to .NET Touch-Events using <code>TouchDevice.ReportDown();</code>, <code>TouchDevice.ReportMove()</code>, etc. Moving <code>ScatterViewItems</code> around and handling button "clicks" works so far. </p>
  
  <p>The problem now is that when I try to write on the <code>InkCanvas</code> only dots are drawn. After observing the events that are fired it seems that the <code>InkCanvas</code> does not receive <code>OnTouchMove</code> events. </p>
  
  <p>I registered event handlers for <code>TouchDown</code>, <code>TouchMove</code> and <code>TouchUp</code> on my <code>SurfaceInkCanvas</code>. <code>TouchDown</code> is never triggered. <code>TouchMove</code> and <code>TouchUp</code> only when I start outside of the <code>SurfaceInkCanvas</code> and then move to a point inside of it.</p>
  
  <p>Here is the code for my <code>TouchDevice</code>:</p>
  
  <pre><code>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)(() =&gt;
          {
              SetActiveSource(PresentationSource.FromVisual(Person.Window));
              Position = GetPosition(args);
              if (!IsActive)
              {
                  Activate();
              }
              ReportDown();
          }));
      }
  
      public void PenUp(PenPointInputArgs args, Dispatcher dispatcher)
      {
          dispatcher.BeginInvoke((Action)(() =&gt;
          {
              Position = GetPosition(args);
              if (IsActive)
              {
                  ReportUp();
                  Deactivate();
              }
          }));
  
      }
  
      public void PenMove(PenPointInputArgs args, Dispatcher dispatcher)
      {
          dispatcher.BeginInvoke((Action)(() =&gt;
          {
              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);
      }
  }
  }
  </code></pre>
  
  <p>I have the following code in my <code>App.xaml.cs</code> and it is called every time a pen input occurs:</p>
  
  <pre><code>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;
              }
  
          }
      }
  </code></pre>
  
  <p>Thank you in advance.</p>