我如何注册一个全局热键说CTRL + SHIFT +(函)使用WPF和.NET 3.5? [英] How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?

查看:371
本文介绍了我如何注册一个全局热键说CTRL + SHIFT +(函)使用WPF和.NET 3.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF构建应用程序在C#。我怎么可以绑定到某些键?

另外,我怎么可以绑定到 Windows键

解决方案

我不知道你所说的全球性的意思是在这里,但这里有云(我假设你在应用层面的意思是一个命令,例如,保存所有的,可以从任何地方通过<大骨节病>控制 + <大骨节病>移 + <大骨节病>取值

您找到全局的UIElement 您的选择,例如,顶层窗口这是所有你需要这个绑定控件的父。由于冒泡的WPF事件,在子元素的事件将泡沫一路攀升到控制树的根。

现在,首先需要

  1. 要使用Key-组合绑定命令的 InputBinding 像这样
  2. 您就可以通过的CommandBinding 白水回收) $ C>。

对于<大骨节病>窗口键,你使用了正确的重点枚举成员, Key.LWin Key.RWin

 公共WindowMain()
    {
       的InitializeComponent();
       //绑定密钥
       InputBinding IB =新InputBinding(
           MyAppCommands.SaveAll,
           新KeyGesture(Key.S,ModifierKeys.Shift | ModifierKeys.Control));
       this.InputBindings.Add(IB);
       //绑定处理器
       CB的CommandBinding =新的CommandBinding(MyAppCommands.SaveAll);
       cb.Executed + =新ExecutedRoutedEventHandler(HandlerThatSavesEverthing);
       this.CommandBindings.Add(CB);
    }

    私人无效HandlerThatSavesEverthing(对象obSender,ExecutedRoutedEventArgs E)
    {
      //这里做保存所有的事情。
    }
 

I'm building an application in C# using WPF. How can I bind to some keys?

Also, how can I bind to the Windows key?

解决方案

I'm not sure of what you mean by "global" here, but here it goes (I'm assuming you mean a command at the application level, for example, Save All that can be triggered from anywhere by Ctrl + Shift + S.)

You find the global UIElement of your choice, for example, the top level window which is the parent of all the controls where you need this binding. Due to "bubbling" of WPF events, events at child elements will bubble all the way up to the root of the control tree.

Now, first you need

  1. to bind the Key-Combo with a Command using an InputBinding like this
  2. you can then hookup the command to your handler (e.g. code that gets called by SaveAll) via a CommandBinding.

For the Windows Key, you use the right Key enumerated member, Key.LWin or Key.RWin

    public WindowMain()
    {
       InitializeComponent();
       // Bind Key
       InputBinding ib = new InputBinding(
           MyAppCommands.SaveAll,
           new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control));
       this.InputBindings.Add(ib);
       // Bind handler
       CommandBinding cb = new CommandBinding( MyAppCommands.SaveAll);
       cb.Executed += new ExecutedRoutedEventHandler( HandlerThatSavesEverthing );
       this.CommandBindings.Add (cb );
    }

    private void HandlerThatSavesEverthing (object obSender, ExecutedRoutedEventArgs e)
    {
      // Do the Save All thing here.
    }

这篇关于我如何注册一个全局热键说CTRL + SHIFT +(函)使用WPF和.NET 3.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆