请帮助我纠正以下C#代码 [英] please help me to correct the following c# code

查看:86
本文介绍了请帮助我纠正以下C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用&&检查数字键盘pad3和1的键条目.但它不起作用

我的代码:

I want to check the key entries of number pad3 and 1 using && but its not working

My code :

private void Form1KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.NumPad1 && e.KeyCode == Keys.NumPad3)
  {
    MessageBox.Show("The device is not yet connected");
  }
}

推荐答案

正如吉姆·拉希(Jim lahey)所说,使用if(e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.NumPad3)来传递NumPad1或NumPad3的条件
As Jim lahey said use if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.NumPad3) to pass the condition either NumPad1 or NumPad3


首先,您需要按照Albin的建议修复检查.这还不够,因为当您将焦点放在任何其他控件上时,带有go的键盘输入将不会到达窗体.

一种简单的解决方法是:在代码中,递归地遍历所有子控件(使用Control.Controls),并添加相同的按键事件处理程序.
但是,使用此按键的空洞想法建议您需要改进UI样式.为什么不简单地拥有一个按钮和/或菜单项.至少它们是自我描述的,但是对于您的按键,您将需要记录此非标准按键.此外,您可以更成功地使用菜单热键.不需要不自然的解决方法.

—SA
First, you need to fix the check as Albin advised. This is not enough, because when you focus any other control, the keyboard input with go there and will not reach the Form.

One simple work-around would: in the code, traverse all child controls (using Control.Controls) recursively and add the same key press event handler.

However, the hole idea of using this key press suggests you need to improve your UI style. Why not simply having a button and/or menu item. It least they would be self-describing, but for your key press your will need to document this non-standard key press. Also, you can more successfully use menu hot key. It would not require an unnatural work-around.

—SA


我不知道您是否已经解决了问题,但是我知道唯一可靠地解决多个按键问题的方法是使用Windows API.

具体来说, GetKetstate() [如何在程序中声明 [
I don''t know if you have solved your problem yet but the only method that I know of resolving multiple keypresses reliably is by the use of the Windows API.

Specifically the GetKetstate()[^] function. How to declare it in your program[^].

Use it something like:
bool np1 = (((ushort) GetKeyState(VK_NUMPAD1)) & 0xffff) != 0;
bool np3  = (((ushort) GetKeyState(VK_NUMPAD3)) & 0xffff) != 0;

if (np1 && np3)
{
  // do whatever you want to do if they are both pressed
}



希望这会有所帮助.


看看
当KeyPressed/KeyDown还不够时. [ ^ ],其中显示了如何使用GetKeyState(),并且还提供了一个可以检查的小应用程序.
[/Edit]



Hope this helps.


Take a look at When KeyPressed / KeyDown just isn''t enough. An adventure in GetKeyboardState.[^] which shows how to use GetKeyState() and also has a small app that you can examine.
[/Edit]


这篇关于请帮助我纠正以下C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆