7

I have a two dimensional array with coordinates and i want to make the mouse move with the specific pattern those coordinates create in a WPF application. Can you help me? I've tried the Cursor class but it will not work. Obviously I am doing something wrong.

private void SetPosition( int a, int b)
{
    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = new Point(a, b);
}

This is the method i use the a and b are from the array. thanks in advance!

PS that method is inside an event that fires 20 times a second.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Kwstas
  • 129
  • 1
  • 1
  • 7
  • 1
    Please define "it will not work" in more detail. What's the pattern you expect and what's the pattern you are getting? If the cursor isn't moving at all that's also relevant information. – ChrisF Nov 18 '11 at 16:53
  • its not moving at all!! the pattern is random it does not matter. i am trying to make it move with ramdom numbers to incorporate it in a project that will feed it specific coordinates. – Kwstas Nov 18 '11 at 17:03
  • Does this answer your question? [How to move mouse cursor using C#?](https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c) – StayOnTarget Sep 16 '21 at 14:32
  • Given that there doesn't appear to be a WPF-specific method, this question seems to be redundant o https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c – StayOnTarget Sep 16 '21 at 14:32

2 Answers2

15

I'm not entirely sure if there is a better way to do it in WPF (It seems the code you are using is targeted at WinForms), but using Platform Invoke on SetCursorPos seems to do the trick:

private void SetPosition(int a, int b)
{
    SetCursorPos(a, b);
}

[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
vcsjones
  • 138,677
  • 31
  • 291
  • 286
3

You have to use SendInput

http://inputsimulator.codeplex.com/ makes it somewhat easy

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85