How to get the current cursor position on the screen?

My goal is to get the current position on the screen (outside the form) and save the X, Y coordinates, for example, by pressing "C".

I google and found some suggestion to use api hooks, but I wonder if there is a way that we can perform this task exclusively in C # code (.NET Lib)?

Please give me a quick sample if possible, because im new for C #.

thank

+5
source share
1 answer

Just use:

Cursor.Position

or

Control.MousePosition

To get a position. Then you can 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

+9

All Articles