在 c# (wpf) 中捕获 windows 键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/767513/
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-05 00:19:58  来源:igfitidea点击:

Capturing the windows key in c# (wpf)

c#wpfkeyboard

提问by Peter

I have written a small program displaying sounds and images on the screen when pushing any button. I always start it when one of my little kids crawls onto my lap and start hitting the keys, of course, randomly.

我编写了一个小程序,在按下任何按钮时在屏幕上显示声音和图像。我总是在我的一个小孩爬到我腿上并开始敲击琴键时开始它,当然,这是随机的。

It works fine except for 2 keys, one of them being the ON/OFF switch, the other being the Windows-key. (the CTRL-ESC equivalent I believe) I can intercept it as it is pressed, but only after the startmenu is showing.

除了 2 个键外,它工作正常,其中一个是 ON/OFF 开关,另一个是 Windows 键。(我相信 CTRL-ESC 等效项)我可以在按下它时拦截它,但只有在开始菜单显示之后。

The event I use is the UIElement.KeyDown and all I could came up with so far is : (the e being KeyEventArgs)

我使用的事件是 UIElement.KeyDown 到目前为止我能想到的是:(e 是 KeyEventArgs)

            if (e.Key == Key.LWin) e.Handled = true;

but than the start window is already showing I'm afraid.

但比开始窗口已经显示我很害怕。

I have already 1 answer but would very much like to know if there's any wpf-support?

我已经有 1 个答案,但很想知道是否有任何 wpf 支持?

I suspect programming the main on/off switch might not be possible? Otherwise, any help there would be welcome too..

我怀疑可能无法对主开关进行编程?否则,任何帮助也将受到欢迎..

采纳答案by abhilash

You'll need a keyboard hook. Unfortunately, this has to be done with P/Invoke; it can't be done with managed code.

你需要一个键盘挂钩。不幸的是,这必须通过 P/Invoke 来完成;它不能用托管代码来完成。

Check out Baby Smash!by Scott Hanselman. It's hosted on code plex at http://www.codeplex.com/babysmashGithub at https://github.com/shanselman/babysmash

看看婴儿粉碎!斯科特·汉塞尔曼。它托管http://www.codeplex.com/babysmashGithub上的code plexhttps://github.com/shanselman/babysmash

Alternatively, check out ShapeShowon CodeProject, which is similar.

或者,查看CodeProject上的ShapeShow,它是类似的。

回答by merrick

See http://msdn.microsoft.com/en-us/library/system.windows.input.key(v=VS.90).aspx

请参阅http://msdn.microsoft.com/en-us/library/system.windows.input.key(v=VS.90).aspx

At the bottom you'll see a simple example, I think what you're looking for is something along these lines:

在底部你会看到一个简单的例子,我认为你正在寻找的是这样的东西:

left windows key: System.Windows.Input.Key.LWin

左窗口键:System.Windows.Input.Key.LWin

right windows key: System.Windows.Input.Key.RWin

Windows 右键:System.Windows.Input.Key.RWin

example:

例子:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.LWin) {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}