8

I'm trying to manually fire a MouseLeftButtonDown event on a WPF control programmatically, as I am using the Microsoft Surface SDK, which does not fire MouseLeftButtonDown events, but ContactDown events. Basically I'm trying to push the MouseLeftButtonDown event down to the control, to fire off the correct behavior on the control, while handling a ContactDown event.

I'm guessing I have to somehow use the RaiseEvent method on the control to do this with MouseButtonEventArgs, but I'm having some trouble figuring out the parameters.

Thanks in advance for your help!

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
RajenK
  • 1,403
  • 3
  • 15
  • 25

4 Answers4

6

You can spoof mouse and key events using Win32 interop. Investigate the SendInput function on MSDN/pinvoke.net.

Note that this will cause the system and other applications to think the mouse was actually clicked. If you just want to initiate a WPF event, try RaiseEvent( new RoutedEventArgs( UIElement.MouseLeftButtonDownEvent ) ).

devios1
  • 36,899
  • 45
  • 162
  • 260
  • 3
    Thanks for the tip. I'm getting an exception with that RaiseEvent method you posted: Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Input.MouseButtonEventArgs'. – RajenK Jun 15 '10 at 11:18
  • Try `RaiseEvent( new MouseButtonEventArgs( Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left ) );` – devios1 Jun 15 '10 at 12:08
  • 1
    InvalidOperationException occurs with that one: Every RoutedEventArgs must have a non-null RoutedEvent associated with it. – RajenK Jun 21 '10 at 14:23
  • 9
    Ok this works: `startOpBtn.RaiseEvent( new MouseButtonEventArgs( Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left ) { RoutedEvent = Button.ClickEvent } );` however, it uses the click event. `UIElement.MouseLeftButtonDown` and `UIElement.MouseLeftButtonUp` compile and run, but they won't "click" the button, probably because the mouse cursor needs to actually be over the button to make the hit-testing work. – devios1 Jun 21 '10 at 16:48
  • Thanks chaiguy, I guess I'll look for an alternative solution. – RajenK Jul 11 '10 at 08:49
  • Has anyone found a workaround way to raise this event? What @devios1 said is helpful but still doesn't get it to fire. – xandermonkey Mar 15 '17 at 19:29
5

The wish to trigger a certain event in a control is quite often an indicator of a design problem in the code. Event handlers should trigger behavior, not perform it. I would suggest that you move the code that performs the action triggered by the MouseLeftButtonDown event handler into a separate method. Then the same method can be called from the ContactDown event handler.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • 1
    I agree that would be the best solution, but as I do not have access to the source code of this particular control (only the binary), I'm afraid that won't work. – RajenK Jun 13 '10 at 20:53
4
var grid = new Grid();            

int timestamp = new TimeSpan(DateTime.Now.Ticks).Milliseconds;
const MouseButton mouseButton = MouseButton.Left;
var mouseDownEvent =
   new MouseButtonEventArgs(Mouse.PrimaryDevice, timestamp, mouseButton) {
       RoutedEvent = UIElement.MouseLeftButtonDownEvent,
       Source = grid,
   };

This is how I fire the event in my test code.

Yuck
  • 49,664
  • 13
  • 105
  • 135
viktorg
  • 41
  • 1
1

This is the static method I use to raise click events for any UIElement.

using System;
using System.Windows;
using System.Windows.Input;

    
public static void RaiseMouseClickEvent(UIElement elementToClick, MouseButton buttonToUse)
{
    elementToClick.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, buttonToUse) { RoutedEvent = Button.ClickEvent });
}
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52