Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create keyboard shortcut in windows that call function in my app?

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.

like image 659
Togrul Ceferli Avatar asked Aug 25 '10 17:08

Togrul Ceferli


People also ask

How do I program keyboard hotkeys?

You can add a hotkey to any software or website shortcut on the Desktop. Right-click the desktop shortcut and select Properties from the menu. Click the Shortcut key box and enter a new keyboard shortcut for the program or web page. Just enter a letter there to set up the new hotkey.


1 Answers

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.

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
  }

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);
    }
  }
}
like image 113
Chris Taylor Avatar answered Oct 20 '22 13:10

Chris Taylor