0

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...

4

2 回答 2

2

您可以使用

System.Windows.Forms.Cursor.Position : "它代表屏幕坐标中的当前光标位置"

注意:请参考示例以了解其工作原理

于 2013-10-17T04:36:19.053 回答
1

您可以使用KeyDown表单的事件(您可以从设计器中添加它以确保它正确连接)

由于您不能只等待 button2_Click 中的按键事件,因此我使用了一个私有字段来存储按钮已被按下的事实。现在,每次用户按下 Insert 时,您都会检查按钮是否被按下以及光标位置。如果两者都正确,则生成新表单。

我已经用类顶部的 2 个常量定义了所需的光标位置,您还应该为“hasButton2BeenClicked”选择一个更好的名称,具体取决于您的业务环境哈哈。

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();
    }
}
于 2013-10-17T04:42:42.923 回答