3

I have some straight WPF 3.5 controls handling left mouse clicks that I need to use within a Surface app (SDK 1.0). The problem I am facing is that do not work by default. I am thinking of wrapping each control in a SurfaceContentControl and translating ContactTouchDown or ContactTapGesture events to corresponding MouseDown events.

The problem boils down to - how to "inject" or simulate arbitrary routed mouse events? I have tried InputManager.Current.ProcessInput() but didn't get very far. Any help is appreciated.

wpfwannabe
  • 14,587
  • 16
  • 78
  • 129

2 Answers2

1

Try to use AutomationPeer classes. For example ButtonAutomationPeer is for Button. The code below initiates a click.

ButtonAutomationPeer peer = new ButtonAutomationPeer(button);
IInvokeProvider provider = (IInvokeProvider)peer.GetPattern(PatternInterface.Invoke);
provider.Invoke();
evpo
  • 2,436
  • 16
  • 23
  • Thanks. What if I want to click on a `Path` or anything non-`Button`? What if I do not want to click on anything in particular but just want to send a click at (X,Y) and see who gets it? Your solution is good if you know that the target is really a `Button`. – wpfwannabe Sep 29 '11 at 09:00
1

evpo's idea is an interesting one (though if you're working with custom controls, they rarely come with AutomationPeer classes).

You can't simply 'inject' mouse input by sending WM_MOUSE* events to your app... WPF would indeed process the message but when it goes to figure out the position of mouse for that event, it will query the actual mouse API instead of trying what you stick in the WM.

So really all you can do is tell windows to move the actual mouse cursor and act as though the button is being clicked/released. Some code you can use for that is in http://www.codeproject.com/KB/system/globalmousekeyboardlib.aspx

That said, while you can technically do this, it sucks... you've got an expensive multitouch device but are 1) showing a mouse cursor on it 2) limiting arbitrary parts of it to being used 'single touch' (and only one of those arbitrary parts at a time and 3) coming up with an arbitrary method of determining which finger you will treat as the mouse-controlling one

Robert Levy
  • 28,747
  • 6
  • 62
  • 94