EditCategory
Input
EditQuestion
How can I simulate basic mouse events?
EditAnswer
In order to simulate basic mouse events, you simply need to build a MouseEventArgs object with the appropriate parameters and send it either directly to the element on which you wish to simulate the input or access the InputManager if the input needs to be simulated on the normal input recipient.
There is 1 base type and 3 types of specialized MouseEventArgs availlable. Be aware that you should pick the one that fits your needs.
1. MouseEventArgs: This generic class represents the basic mouse event and should be used when simulating events such as MouseEnterEvent, MouseLeaveEvent, MouseMoveEvent, GotMouseCaptureEvent, etc.
2. MouseButtonEventArgs: This class represents an event where the state of one of the buttons was modified. Use this class to represent the following events: PreviewMouseDownEvent, MouseDownEvent, PreviewMouseUpEvent, MouseUpEvent.
3. MouseWheelEventArgs: This class represents an event where the status of the mouse wheel changed. Use this to simulate mouse wheel events such as MouseWheelEvent.
4. QueryCursorEventArgs: This class represents a QueryCursor event. Use this to simulate a QueryCursorEvent.
To build the MouseEventArgs, you need to specify the following at construction:
1. The MouseDevice that should be "simulated": If there is no specific device you wish to simulate, simply pass Mouse.PrimaryDevice.
2. A timestamp (can be 0).
3. For a specialized class, you might have to specify an additional parameter (depending on the specialized class).
Once the MouseEventArgs object is constructed, you need to set the RoutedEvent member of the object to the input you wish to simulate. For example:
MouseEventArgs e = new MouseEventArgs(Mouse.PrimaryDevice, 0);
e.RoutedEvent = Mouse.MouseEnterEvent;
You can then send the input simulation either by:
targetElement.RaiseEvent(e);
or
InputManager.Current.ProcessInput(e);
This will simulate the MouseEnter event for the "targetElement" object or the mouse's currently active element.
The types of input that can be simulated are as follows:
MouseEnter, MouseLeave, MouseMove, PreviewMouseDown, MouseDown, PreviewMouseUp, MouseUp, QueryCursor.
Geometry Help