wpf 在整个应用程序中捕获按键

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

Capture key press in whole application

c#wpfkeypress

提问by user13657

Is it possible, to capture (somewhere in app.xaml.cs i guess) any key and if it pressed open window?

是否有可能捕获(我猜在 app.xaml.cs 中的某处)任何键,如果它按下打开的窗口?

Thanks for help!

感谢帮助!

采纳答案by khellang

You could use something like this gistto register a global hook. It will fire whenever the given keys are pressed while your application is running. You can use it in your Appclass like this:

您可以使用类似这个 gist 的东西来注册一个全局钩子。只要在您的应用程序运行时按下给定的键,它就会触发。您可以App像这样在课堂上使用它:

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
    }
}

回答by Aaron McIver

Yes and no.

是和否。

Focus plays a role in the order for which a given key is handled. The control which captures the initial key press can opt to not pass the key along, which would prohibit you from capturing it at the top most level. In addition there are controls within the .NET framework that swallow certain keys under certain scenarios, however I am unable to recall a specific instance.

焦点在处理给定键的顺序中起作用。捕获初始按键的控件可以选择不传递键,这将禁止您在最顶层捕获它。此外,.NET 框架中的控件在某些情况下会吞下某些键,但是我无法回忆起特定的实例。

If your application is small and the depth is nothing more than a Window with buttons, this is certainly attainable and would follow the standard approach to capturing key strokes within a WPF application.

如果您的应用程序很小并且深度只不过是一个带有按钮的窗口,这当然是可以实现的,并且将遵循在 WPF 应用程序中捕获按键的标准方法。

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
          myVariable = true;
    if (ctrl && e.Key == Key.S)
          base.OnKeyDown(e);
}

protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
          myVariable = false;

    base.OnKeyUp(e);
}

If your application is large you can attempt a global hookas detailed here but understand that the aforementioned caveats can still exist.

如果您的应用程序很大,您可以尝试使用此处详述的全局钩子,但要了解上述警告仍然存在。

回答by Adam Calvet Bohl

There is a better way. Found thison a MS forum. Works like a charm.

有个更好的方法。在 MS 论坛上找到了这个。奇迹般有效。

Put this code in Application startup:

将此代码放在应用程序启动中:

EventManager.RegisterClassHandler(typeof(Window),
     Keyboard.KeyUpEvent,new KeyEventHandler(keyUp), true);

private void keyUp(object sender, KeyEventArgs e)
{
      //Your code...
}