在整个应用程序中捕获按键 [英] Capture key press in whole application
本文介绍了在整个应用程序中捕获按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能捕获(我猜是在app.xaml.cs中的某个地方)任何键,如果它按下了打开的窗口?
Is it possible, to capture (somewhere in app.xaml.cs i guess) any key and if it pressed open window?
感谢帮助!
推荐答案
您可以使用此要点注册一个全局钩子.当您的应用程序运行时,只要按下给定的键,它将触发.您可以像这样在App
类中使用它:
You could use something like this gist to register a global hook. It will fire whenever the given keys are pressed while your application is running. You can use it in your App
class like this:
public partial class App
{
private HotKey _hotKey;
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
RegisterHotKeys();
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
UnregisterHotKeys();
}
private void RegisterHotKeys()
{
if (_hotKey != null) return;
_hotKey = new HotKey(ModifierKeys.Control | ModifierKeys.Shift, Key.V, Current.MainWindow);
_hotKey.HotKeyPressed += OnHotKeyPressed;
}
private void UnregisterHotKeys()
{
if (_hotKey == null) return;
_hotKey.HotKeyPressed -= OnHotKeyPressed;
_hotKey.Dispose();
}
private void OnHotKeyPressed(HotKey hotKey)
{
// Do whatever you want to do here
}
}
这篇关于在整个应用程序中捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文