简体   繁体   English

C#中系统键的KeyDown事件

[英]KeyDown event in C# for system keys

I'm trying to capture any KeyDown event regardless of what item has the focus. 我试图捕获任何KeyDown事件,而不管焦点在哪个项目上。 I have the following code in my constructor: 我的构造函数中包含以下代码:

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

And my function here: 我的功能在这里:

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    //Stuff in here
}

This works fine for regular keys, (arrows, letters) but does not receive events for special keys (like Enter or Tab ). 这对于常规键(箭头,字母)工作正常,但不接收特殊键(例如EnterTab )的事件。

How do I intercept the event for the Enter or Tab keypress and keep the system from handling it? 如何拦截EnterTab键的事件,并阻止系统处理该事件? I need to get the event all the time, regardless of what object has the focus. 无论什么对象具有焦点,我都需要一直获取事件。

The process for handling keys is quite complex. 处理密钥的过程非常复杂。 KeyDown is actually quite late in the process and does not include "control" keys. KeyDown实际上很晚,并且不包含“控制”键。 To get those keys you need to override IsInputKey (for Tab and Control keys) or ProcessDialogKey (for arrow keys). 要获取这些键,您需要覆盖IsInputKey(用于Tab和Control键)或ProcessDialogKey(用于箭头键)。 See some excellent documentation here https://msdn.microsoft.com/en-us/library/vstudio/ms171535%28v=vs.100%29.aspx 在此处查看一些出色的文档https://msdn.microsoft.com/zh-cn/library/vstudio/ms171535%28v=vs.100%29.aspx

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

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