简体   繁体   English

如何在不使用 TextBox 的情况下检测是否按下了某个键?

[英]How do I detect if a key is pressed without using a TextBox?

I have looked on Stack Overflow and Google but I have yet to find an answer to my question.我查看了 Stack Overflow 和 Google,但我还没有找到我的问题的答案。 I want to detect when a key is pressed without the need of a text box.我想在不需要文本框的情况下检测何时按下了一个键。

Override OnKeyPress main Form , this will catch the key presses覆盖OnKeyPressForm ,这将捕获按键

Winforms: Winforms:

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
    }

WPF: (will have to use OnKeyDown or OnKeyUp WPF:(必须使用OnKeyDownOnKeyUp

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
    }

All controls (including forms) have the KeyPress , KeyDown and KeyUp events that you may use.所有控件(包括表单)都有您可以使用的KeyPressKeyDownKeyUp事件。

And there are corresponding OnKeyPress , OnKeyDown and OnKeyUp methods that you can override in your own controls.并且有相应的OnKeyPressOnKeyDownOnKeyUp方法,您可以在自己的控件中覆盖它们。

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

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