Advertisement
Guest User

Global Hotkey Class for C#

a guest
Jul 6th, 2010
4,571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement