简体   繁体   English

我该如何做,以便当我按下特定的按键时会产生中断

[英]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 当您按下数字键盘上的“ 0”键时,您将获取鼠标坐标
  • When you press the '1' key on the numpad you will move the mouse. 当您按下数字键盘上的“ 1”键时,将移动鼠标。

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. 如果您想了解特定于您的特定问题的更多详细信息,请编辑您的问题以包括更多特定的信息,我们将尽力为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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