且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

请帮助我纠正以下C#代码

更新时间:2023-02-07 22:59:58

正如吉姆·拉希(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]