daily pastebin goal
33%
SHARE
TWEET

Global Hotkeys - VB.NET

coderail Sep 15th, 2013 799 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Out of date, will update eventually.
  2.  
  3.     Class Shortcut
  4.         Inherits NativeWindow
  5.         Implements IDisposable
  6.  
  7. #Region " Declarations "
  8.         Protected Declare Function UnregisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
  9.         Protected Declare Function RegisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer, ByVal modifier As Integer, ByVal vk As Integer) As Boolean
  10.  
  11.         Event Press(ByVal sender As Object, ByVal e As HotKeyEventArgs)
  12.         Protected EventArgs As HotKeyEventArgs, ID As Integer
  13.  
  14.         Enum Modifier As Integer
  15.             None = 0
  16.             Alt = 1
  17.             Ctrl = 2
  18.             Shift = 4
  19.         End Enum
  20.         Class HotKeyEventArgs
  21.             Inherits EventArgs
  22.             Property Modifier As Shortcut.Modifier
  23.             Property Key As Keys
  24.         End Class
  25.         Class RegisteredException
  26.             Inherits Exception
  27.             Protected Const s As String = "Shortcut combination is in use."
  28.             Sub New()
  29.                 MyBase.New(s)
  30.             End Sub
  31.         End Class
  32. #End Region
  33.  
  34. #Region " IDisposable "
  35.         Private disposed As Boolean
  36.         Protected Overridable Sub Dispose(ByVal disposing As Boolean)
  37.             If Not disposed Then UnregisterHotKey(Handle, ID)
  38.             disposed = True
  39.         End Sub
  40.         Protected Overrides Sub Finalize()
  41.             Dispose(False)
  42.             MyBase.Finalize()
  43.         End Sub
  44.         Sub Dispose() Implements IDisposable.Dispose
  45.             Dispose(True)
  46.             GC.SuppressFinalize(Me)
  47.         End Sub
  48. #End Region
  49.  
  50.         Sub New(ByVal modifier As Modifier, ByVal key As Keys)
  51.             CreateHandle(New CreateParams)
  52.             ID = GetHashCode()
  53.             EventArgs = New HotKeyEventArgs With {.Key = key, .Modifier = modifier}
  54.             If Not RegisterHotKey(Handle, ID, modifier, key) Then Throw New RegisteredException
  55.         End Sub
  56.         Shared Function Create(ByVal modifier As Modifier, ByVal key As Keys) As Shortcut
  57.             Return New Shortcut(modifier, key)
  58.         End Function
  59.  
  60.         Protected Sub New()
  61.         End Sub
  62.         Protected Overrides Sub WndProc(ByRef m As Message)
  63.             Select Case m.Msg
  64.                 Case 786
  65.                     RaiseEvent Press(Me, EventArgs)
  66.                 Case Else
  67.                     MyBase.WndProc(m)
  68.             End Select
  69.         End Sub
  70.     End Class
  71.  
  72.    
  73.     'Usage:
  74.     Dim WithEvents T As Shortcut
  75.     Private Sub Form_Load() Handles MyBase.Load
  76.         T = Shortcut.Create(Shortcut.Modifier.Alt, Keys.E)
  77.         'T = Shortcut.Create(Shortcut.Modifier.Alt Or Shortcut.Modifier.Shift, Keys.E)
  78.     End Sub
  79.     Private Sub T_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles T.Press
  80.         MessageBox.Show("hello")
  81.     End Sub
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top