Firing MouseLeftButtonDown event programmatically
Asked Answered
B

4

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!

Brainwash answered 13/6, 2010 at 20:44 Comment(0)
D
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 ) ).

Donough answered 13/6, 2010 at 21:58 Comment(6)
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'.Brainwash
Try RaiseEvent( new MouseButtonEventArgs( Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Left ) );Donough
InvalidOperationException occurs with that one: Every RoutedEventArgs must have a non-null RoutedEvent associated with it.Brainwash
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.Donough
Thanks chaiguy, I guess I'll look for an alternative solution.Brainwash
Has anyone found a workaround way to raise this event? What @Donough said is helpful but still doesn't get it to fire.Squawk
C
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.

Cognizance answered 13/6, 2010 at 20:49 Comment(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.Brainwash
M
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.

Medick answered 15/12, 2011 at 15:0 Comment(0)
M
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 });
}
Margo answered 27/11, 2020 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.