473,320 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I move the cursor with c#?

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#?
Apr 16 '10 #1

✓ answered by jkmyoung

There's a couple quick library functions you can import,eg :
Expand|Select|Wrap|Line Numbers
  1.        [DllImport("user32.dll", SetLastError = true)]
  2.         [return: MarshalAs(UnmanagedType.Bool)]
  3.         public static extern bool SetCursorPos(int X, int Y);
  4.         [DllImport("user32.dll")]
  5.         static extern bool GetCursorPos(ref Point lpPoint);
  6.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  7.         public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
  8.  
  9.         /// <summary>
  10.         /// Not sure if we're just supposed to create our own point class.
  11.         /// </summary>
  12.         struct Point
  13.         {
  14.             public int x;
  15.             public int y;
  16.         }
  17.  
  18.  
  19.         private const int MOUSEEVENTF_LEFTDOWN = 0x02;
  20.         private const int MOUSEEVENTF_LEFTUP = 0x04;
  21.         private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
  22.         private const int MOUSEEVENTF_RIGHTUP = 0x10;
  23.  
  24.  
  25.         public static void DoMouseClick(long x, long y)
  26.         {   
  27.             mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
  28.         }
  29.  
  30.  
  31.         public static void MoveMouse(int x, int y)
  32.         {
  33.             SetCursorPos(x, y);
  34.         }
Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.

6 13807
tlhintoq
3,525 Expert 2GB
Yes it is.
Look on MSDN for Cursor and change the Cursor.Location property
Apr 17 '10 #2
@tlhintoq
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?
Apr 18 '10 #3
tlhintoq
3,525 Expert 2GB
First haw can I click.?
With the mouse. What is this question supposed to even be asking?

Second it only works on the form but not outside it the how do I make it work outside the form?
"Global Hooks" is a much more advanced subject - but I've just told you what to research.

And third how can I get location on the hole screen?
Again, do a little research on MSDN. You can easily take a location and convert it To Screen Location. <hint>
Apr 18 '10 #4
@tlhintoq
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#.
Apr 18 '10 #5
jkmyoung
2,057 Expert 2GB
There's a couple quick library functions you can import,eg :
Expand|Select|Wrap|Line Numbers
  1.        [DllImport("user32.dll", SetLastError = true)]
  2.         [return: MarshalAs(UnmanagedType.Bool)]
  3.         public static extern bool SetCursorPos(int X, int Y);
  4.         [DllImport("user32.dll")]
  5.         static extern bool GetCursorPos(ref Point lpPoint);
  6.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  7.         public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
  8.  
  9.         /// <summary>
  10.         /// Not sure if we're just supposed to create our own point class.
  11.         /// </summary>
  12.         struct Point
  13.         {
  14.             public int x;
  15.             public int y;
  16.         }
  17.  
  18.  
  19.         private const int MOUSEEVENTF_LEFTDOWN = 0x02;
  20.         private const int MOUSEEVENTF_LEFTUP = 0x04;
  21.         private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
  22.         private const int MOUSEEVENTF_RIGHTUP = 0x10;
  23.  
  24.  
  25.         public static void DoMouseClick(long x, long y)
  26.         {   
  27.             mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
  28.         }
  29.  
  30.  
  31.         public static void MoveMouse(int x, int y)
  32.         {
  33.             SetCursorPos(x, y);
  34.         }
Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.
Apr 19 '10 #6
@jkmyoung
Thanks this is exactly what I was looking for.
Apr 19 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Fereshteh Shojaei | last post by:
Hi, I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>. Regards, Fereshteh
8
by: Hung Huynh | last post by:
Hi, I'm trying to use either one of these methods to position the cursor in a specific position inside a recordset, but neither one seems to work. For example, I have 10 records in my rsData...
7
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
1
by: gmtongar | last post by:
Hi, I've made a custom scrollbar which consists of three buttons and a panel. My problem is: How do I move it like a scrollbar slider? I've tried DoDragDrop, but it does not appear to be the same,...
0
by: Muhammad Aftab Alam | last post by:
Hi All, I am facing a problem during changing the cursor on the mouse move event of a List View control. the event is captured but when I try to change the cursor as with the following code...
1
by: Phil Endecott | last post by:
Dear Postgresql experts, According to the documentation for MOVE, it returns the number of rows that it has moved over. It seems to me that this is true for MOVE FORWARD n, but not for MOVE...
1
by: Stefan Mueller | last post by:
With the following code I can change the mouse pointer. However, if you click in Mozilla (with IE it works perfect) on 'Show hourglass' the mouse pointer changes only if you move the mouse at least...
0
by: Rahna | last post by:
Hi, I'm using the DirectSS1 Speech Control in my VB 6 application. I tried to use the WordPosition Event to move the cursor along with the speech. the code goes like this Private Sub...
4
by: yan.python | last post by:
i have a question. when i run Interactive Interpreter in linux command promt,how can i move the cursor. for example,when i enter a string,i often enter the quotation mark "" first,and the move...
5
by: michels287 | last post by:
Right now I have a touchscreen with a virual keyboard. I would like to create arrow keys. If the user messed up the inputted text, he can move the blinking cursor left one space at a time between...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.