简体   繁体   中英

C# how to get mouse position after INSERT key if pressed, after button click?

I need to know how to get mouse position when I press a key (insert).

This is what I trying to do:

I have a form1 with one buuton, when you press that button it call another form. But before call the form2 i need to get mouse position from an external application. To do this, the user must hover the cursor over requested position and press 'INSERT'.

public partial class _CalibrateGeneralStep2 : Form
{
    public _CalibrateGeneralStep2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Restart();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();      

        ///// HERE I NEED TO WAIT UNTIL USER PRESS 'INSERT' KEY BEFORE CALL  _CalibrateGeneralStep3 /////   

        _CalibrateGeneralStep3 frm = new _CalibrateGeneralStep3();
        frm.Show();
    }
}

I try with keypress and keydown but I dont know use it well.

Thanks... sorry if my english is not good...

You can use

System.Windows.Forms.Cursor.Position : "It represents the current cursor position in screen co-ordinates"

Note: Please refer to the example to see how it works

You can use the KeyDown Event of the form (You can add it from the Designer to be sure it's wired properly)

Since you cannot just wait for the key press event inside your button2_Click, I've used a private field to store the fact that the button have been pressed. Now each time the user press Insert, you check if the button have been pressed and the cursor position. If both are correct, generate the new form.

I've defined the needed cursor position with the 2 constants at the top of the class, and you should also choose a better name for "hasButton2BeenClicked", depending of your business context haha.

public partial class _CalibrateGeneralStep2 : Form
{
    private const int NEEDED_X_POSITION = 0;
    private const int NEEDED_Y_POSITION = 0;

    private bool hasButton2BeenClicked = false;

    public _CalibrateGeneralStep2()
    {
        InitializeComponent();
        KeyPreview = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Restart();
    }

    private void button2_Click(object sender, EventArgs e)
    {      
        hasButton2BeenClicked = true;  
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Insert && IsCursorAtTheCorrectPosition() && hasButton2BeenClicked)
        {
            GoToNextStep();
        }
    }

    private bool IsCursorAtTheCorrectPosition()
    {
        return Cursor.Position.X == NEEDED_X_POSITION && Cursor.Position.Y == NEEDED_Y_POSITION;
    }

    private void GoToNextStep()
    {
        this.Hide();
        new _CalibrateGeneralStep3().Show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM