Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Global Hotkey Class for C#

By: a guest on Jul 6th, 2010  |  syntax: C#  |  size: 1.07 KB  |  views: 1,411  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace Hotkeys
  6. {
  7.     public class GlobalHotkey
  8.     {
  9.         private int modifier;
  10.         private int key;
  11.         private IntPtr hWnd;
  12.         private int id;
  13.  
  14.         public GlobalHotkey(int modifier, Keys key, Form form)
  15.         {
  16.             this.modifier = modifier;
  17.             this.key = (int)key;
  18.             this.hWnd = form.Handle;
  19.             id = this.GetHashCode();
  20.         }
  21.  
  22.         public bool Register()
  23.         {
  24.             return RegisterHotKey(hWnd, id, modifier, key);
  25.         }
  26.  
  27.         public bool Unregiser()
  28.         {
  29.             return UnregisterHotKey(hWnd, id);
  30.         }
  31.  
  32.         public override int GetHashCode()
  33.         {
  34.             return modifier ^ key ^ hWnd.ToInt32();
  35.         }
  36.  
  37.         [DllImport("user32.dll")]
  38.         private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
  39.  
  40.         [DllImport("user32.dll")]
  41.         private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  42.     }
  43. }