Im trying to make a program so that when I hold down shift or something and press an arow key it moves the cursor. Is it posible to move the cursor using c#?
How can I move the cursor with c#?
Collapse
X
-
Ok so this worked but I have 3 new questions naw. First haw can I click.?Second it only works on the form but not outside it the how do I make it work outside the form? And third how can I get location on the hole screen?Comment
-
With the mouse. What is this question supposed to even be asking?First haw can I click.?
"Global Hooks" is a much more advanced subject - but I've just told you what to research.Second it only works on the form but not outside it the how do I make it work outside the form?
Again, do a little research on MSDN. You can easily take a location and convert it To Screen Location. <hint>And third how can I get location on the hole screen?Comment
-
Sory I was not cleer on what I ment by how can I click. What I ment was haw can I simulate a mouse click with c#.Comment
-
There's a couple quick library functions you can import,eg :Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.Code:[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")] static extern bool GetCursorPos(ref Point lpPoint); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); /// <summary> /// Not sure if we're just supposed to create our own point class. /// </summary> struct Point { public int x; public int y; } private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10; public static void DoMouseClick(long x, long y) { mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0); } public static void MoveMouse(int x, int y) { SetCursorPos(x, y); }Comment
-
Thanks this is exactly what I was looking for.There's a couple quick library functions you can import,eg :Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.Code:[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")] static extern bool GetCursorPos(ref Point lpPoint); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); /// <summary> /// Not sure if we're just supposed to create our own point class. /// </summary> struct Point { public int x; public int y; } private const int MOUSEEVENTF_LEFTDOWN = 0x02; private const int MOUSEEVENTF_LEFTUP = 0x04; private const int MOUSEEVENTF_RIGHTDOWN = 0x08; private const int MOUSEEVENTF_RIGHTUP = 0x10; public static void DoMouseClick(long x, long y) { mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0); } public static void MoveMouse(int x, int y) { SetCursorPos(x, y); }Comment
Comment