在 c# (wpf) 中捕获 windows 键

Capturing the windows key in c# (wpf)(在 c# (wpf) 中捕获 windows 键)
本文介绍了在 c# (wpf) 中捕获 windows 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

但恐怕开始窗口已经显示出来了.

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

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

解决方案

你需要一个 键盘挂钩.不幸的是,这必须通过 P/Invoke 来完成;托管代码无法做到这一点.

查看 Scott Hanselman 的 Baby Smash!.它托管在 http://www.codeplex.com/babysmashcode plex> Github 在 https://github.com/shanselman/babysmash

或者,查看 CodeProject 上的 ShapeShow,类似.p>

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.

The event I use is the UIElement.KeyDown and all I could came up with so far is : (the e being 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?

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

解决方案

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

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

Alternatively, check out ShapeShow on CodeProject, which is similar.

这篇关于在 c# (wpf) 中捕获 windows 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)