简体   繁体   中英

How do I make it so that when I have a specific Key press it creates an interupt

The reason I'm trying to do this, is that I have a program that when you push one of the buttons it records where your mouse was at the time, and then if you push another it moves your mouse to that location. If there is any simpler way to do this please tell me.

I'm going to make some assumptions since I am missing some of the context into what you are trying to accomplish:

  • When you press the '0' key on the numpad you will grab the mouse coordinates
  • When you press the '1' key on the numpad you will move the mouse.

That being said, you can accomplish what you are trying to do with the following code:

private void YourControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.NumPad0) 
    {
       //Grab mouse coordinates
    }
    if (e.KeyCode == Keys.NumPad1) 
    {
       //Code to move the mouse
    }
}

Of course, this only works if your application is in focus, if you want to accomplish this when you application is minimized you will have to register global hotkeys .

If you would like more details that are specific to your particular problem, please edit you question to include more specific information and I will do my best to help you out.

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