Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current screen cursor position?

Tags:

c#

My objective is to get current position on the screen (out side the form), and save X, Y coords by press "C" for example.

I google and found some suggestion to use api hooks, but i wonder are there a way we can do this task purely in C# code (.NET Lib) ?

Please give me quick sample if possible because im new to c#.

Thanks

like image 306
Kelly Avatar asked May 29 '11 03:05

Kelly


1 Answers

Just use:

Cursor.Position

or

Control.MousePosition

To get the position. You can then map the KeyPress event of the form:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'c')
        MessageBox.Show(Cursor.Position.ToString());
}

The individual X and Y coordinates are two properties of the Position object.

Documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

like image 173
MPelletier Avatar answered Nov 11 '22 16:11

MPelletier