Capturing touch events
While developing an iPhone application I needed to detect all touch events. I found some tips on how to do it in objective-c but I’m mostly using MonoTouch.
Here is my solution:
Since I’m doing a Window based application, I thought that I could probably get the touch events by overriding the standard UIWindow with my own version.
Taking a look to the default generated code (MainWindow.xib) we can see that the application is based on a UIWindow:
// Base type probably should be MonoTouch.Foundation.NSObject or subclass
[MonoTouch.Foundation.Register("AppDelegate")]
public partial class AppDelegate {
private UIWindow __mt_window;
#pragma warning disable 0169
[MonoTouch.Foundation.Connect("window")]
private UIWindow window {
get {
this.__mt_window = ((UIWindow)(this.GetNativeField("window")));
return this.__mt_window;
}
set {
this.__mt_window = value;
this.SetNativeField("window", value);
}
}
}
To create our custom windows class we can double-click the MainWindows.xib file and do it on InterfaceBuilder.
Step by step what to do on Interface Builder:
- Click on the Window and change the class to “EventUIWindow”;
- In the “Library” select “EventUIWindow”
- Set the inheritance super class to UIWindow
- Save and close Interface Builder
Now if we look at the generated code we have this:
// Base type probably should be MonoTouch.Foundation.NSObject or subclass
[MonoTouch.Foundation.Register("EventUIWindow")]
public partial class EventUIWindow { }
[MonoTouch.Foundation.Register("AppDelegate")]
public partial class AppDelegate {
private EventUIWindow __mt_window;
#pragma warning disable 0169
[MonoTouch.Foundation.Connect("window")]
private UIWindow window {
get {
this.__mt_window = ((EventUIWindow)(this.GetNativeField("window")));
return this.__mt_window;
}
set {
this.__mt_window = value;
this.SetNativeField("window", value);
}
}
}
Now the only thing we need to do is to implement the EventUIWindow. We can do this since it is a partial class. On the implementation we need to override the SendEvent method.
Some sample code:
public partial class EventUIWindow : UIWindow
{
public EventUIWindow () : base() {}
public EventUIWindow (IntPtr handle) : base(handle) {}
public override void SendEvent (UIEvent evt)
{
// Send the event to its original destination
base.SendEvent (evt);
// Get touchs from the event
var touchs = evt.AllTouches;
// Count will be one most of the time (single touch) but multitouch will appear here. E.g. Pinch touchs.Count = 2
if (touchs.Count > 0)
{
// Get one touch event from the array
UITouch touch = touchs.AnyObject as UITouch;
if (touch != null)
{
// You can get the touch phase
// UITouchPhase.Began || UITouchPhase.Cancelled || UITouchPhase.Ended || UITouchPhase.Moved || UITouchPhase.Stationary
// touch.Phase;
// How many taps the click had
// touch.TapCount
// Target view
// touch.View
// Target Window
// touch.Window
// Touch event timestamp
// touch.Timestamp
// And a few more things
}
}
}
}
You can find the full source code for this sample at http://github.com/reistiago/monotouch-samples/
Hope it helps

Great article thanks alot.
One note:
Shouldn’t line 14 be if (touchs.Count > 0)
Hi,
thanks for reading.
Yep you are right. It was a typo. Thanks you for the note. The source on github was correct but the post was wrong.