C# 如何检测是否按下了任何键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12076107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:17:21  来源:igfitidea点击:

How to detect if any key is pressed

c#wpfc#-4.0

提问by RobHurd

How can I detect if any keyboard key is currently being pressed? I'm not interested in what the key is, I just want to know if any key is still pressed down.

如何检测当前是否按下了任何键盘键?我对键是什么不感兴趣,我只想知道是否还有任何键被按下。

if (Keyboard.IsKeyDown(Key.ANYKEY??)
{

}

采纳答案by Servy

public static IEnumerable<Key> KeysDown()
{
    foreach (Key key in Enum.GetValues(typeof(Key)))
    {
        if (Keyboard.IsKeyDown(key))
            yield return key;
    }
}

you could then do:

然后你可以这样做:

if(KeysDown().Any()) //...

回答by franssu

Iterate over the System.Windows.Input.Keyenum values.

迭代System.Windows.Input.Key枚举值。

public static bool IsAnyKeyDown()
{
    var values = Enum.GetValues(typeof(Key));

    foreach (var v in values)
        if (((Key)v) != Key.None && Keyboard.IsKeyDown((Key)v)
            return true;

    return false;
}

回答by Colin Smith

Good answer here...get the set of all the keystates at once using GetKeyboardState():

这里的好答案...使用GetKeyboardState()以下方法一次获取所有关键状态的集合:

The above is checking the live state of the keyboard.

以上是检查键盘的实时状态。

If you rely on hooking events up to the KeyDown/KeyUp events to track the state of the keyboard...then this may not be so accurate.

如果您依靠将事件与 KeyDown/KeyUp 事件挂钩来跟踪键盘的状态……那么这可能不太准确。

That's becuase you are relying on the message pumping to process and dispatch those KeyDown/KeyUp messages....they may be delivered after the real keyboard state has changed again.

那是因为您依赖于消息泵来处理和分发那些 KeyDown/KeyUp 消息......它们可能会在真正的键盘状态再次改变后传递。

Also because when your bit of code that is interested in the keyboard state is running (usually on the UI thread)...the KeyDown or KeyUp can't interrupt you...as they are dispatched on the UI thread too....that's why using GetKeyBoardState()or the Keyboard.IsKeyDownshould be used.

还因为当您对键盘状态感兴趣的代码正在运行时(通常在 UI 线程上)...... KeyDown 或 KeyUp 不能打断你......因为它们也是在 UI 线程上调度的...... .这就是为什么使用GetKeyBoardState()Keyboard.IsKeyDown应该使用。

(the above is assuming you want and care about the live state)

(以上假设您想要并关心实时状态)

回答by Ionic? Biz?u

If you want to detect key pressed only in our application (when your WPF window is activated) add KeyDownlike below:

如果您只想检测在我们的应用程序中按下的键(当您的 WPF 窗口被激活时),请添加KeyDown如下所示:

public MainWindow()
{
    InitializeComponent();
    this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
}

void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show("You pressed a keyboard key.");
}

If you want to detect when a key is pressed even your WPF window is not active is a little harder but posibile. I recomend RegisterHotKey(Defines a system-wide hot key) and UnregisterHotKeyfrom Windows API. Try using these in C# from pinvoke.netor these tutorials:

如果您想检测某个键何时被按下,即使您的 WPF 窗口未处于活动状态,这也有点困难,但可能是可行的。我推荐RegisterHotKey(定义系统范围的热键)和UnregisterHotKeyWindows API。尝试在来自pinvoke.net或这些教程的C# 中使用这些:

Thse is a sample in Microsoft Forums.

这是Microsoft 论坛中的示例。

You will use Virtual-Key Codes. I Hope that I was clear and you will understand my answer.

您将使用虚拟键代码。我希望我说的很清楚,你会明白我的回答。