如何调用函数在我的应用程序窗口创建键盘快捷键? [英] How to create keyboard shortcut in windows that call function in my app?

查看:154
本文介绍了如何调用函数在我的应用程序窗口创建键盘快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建执行与键盘快捷键的操作的应用程序(App必须unvisible)。例如显示消息框,当用户按下<大骨节病>控制 + <大骨节病>替代 + <大骨节病>是W

How can I create an application that performs an action with keyboard shortcut (App must be unvisible). For example Shows MessageBox when user press Ctrl + Alt + W.

推荐答案

一个解决办法是使用互操作和使用Win32的 RegisterHotKey API。这里是一个快速和肮脏的例子,我只是放在一起,因此没有得到很好的测试,我不知道有没有unexepcted副作用,但它应该工作。

One solution would be to use interop and use the Win32 RegisterHotKey API. Here is a quick and dirty example I just put together so it is not well tested and I am not sure that there are no unexepcted side effects, but it should work.

首先这里是一个简单的 HotKeyManager 这需要照顾的基本互操作的,提供了一个隐藏的窗口办理本机Windows消息(WM_HOTKEY),它被翻译成.NET事件 HotKeyPressed

First here is a simple HotKeyManager this takes care of the basic interop, provides a hidden window to handle the native Windows messages (WM_HOTKEY) which is translated into a .NET event HotKeyPressed

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

  public class HotKeyManager
  {
    public static event EventHandler<HotKeyEventArgs> HotKeyPressed;

    public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
    {
      int id = System.Threading.Interlocked.Increment(ref _id);
      RegisterHotKey(_wnd.Handle, id, (uint)modifiers, (uint)key);
      return id;
    }

    public static bool UnregisterHotKey(int id)
    {
      return UnregisterHotKey(_wnd.Handle, id);
    }

    protected static void OnHotKeyPressed(HotKeyEventArgs e)
    {
      if (HotKeyManager.HotKeyPressed != null)
      {
        HotKeyManager.HotKeyPressed(null, e);
      }
    }

    private static MessageWindow _wnd = new MessageWindow();

    private class MessageWindow : Form
    {
      protected override void WndProc(ref Message m)
      {
        if (m.Msg == WM_HOTKEY)
        {
          HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
          HotKeyManager.OnHotKeyPressed(e);
        }

        base.WndProc(ref m);
      }

      private const int WM_HOTKEY = 0x312;
    }

    [DllImport("user32")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private static int _id = 0;
  }


  public class HotKeyEventArgs : EventArgs
  {
    public readonly Keys Key;
    public readonly KeyModifiers Modifiers;

    public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
    {
      this.Key = key;
      this.Modifiers = modifiers;
    }

    public HotKeyEventArgs(IntPtr hotKeyParam)
    {
      uint param = (uint)hotKeyParam.ToInt64();
      Key = (Keys)((param & 0xffff0000) >> 16);
      Modifiers = (KeyModifiers)(param & 0x0000ffff);
    }
  }

  [Flags]
  public enum KeyModifiers
  {
    Alt = 1,
    Control = 2,
    Shift = 4,
    Windows = 8,
    NoRepeat = 0x4000
  }

下面显示了一个简单的Windows窗体应用程序,这将保持主要形式隐藏和热键事件。我没有处理应用程序的关闭和热键的注销,您可以处理

The following shows a simple windows forms application which will keep the main form hidden and respond to the hot key events. I did not handle the closing of the application and the unregistering of the hot key, you can handle that.

using System;
using System.Windows.Forms;

namespace HotKeyManager
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
      HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);     
    }

    void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
    {
      MessageBox.Show("Hello");
    }

    protected override void SetVisibleCore(bool value)
    {      
      // Quick and dirty to keep the main window invisible      
      base.SetVisibleCore(false);
    }
  }
}

这篇关于如何调用函数在我的应用程序窗口创建键盘快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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