Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. RegisterHotKey

RegisterHotKey

Scheduled Pinned Locked Moved Visual Basic
json
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JR212
    wrote on last edited by
    #1

    Hi, I use for some years this code that worked.

    Public Class HotKey
    ' Windows API functions and constants
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
    Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short

        Public Const MOD\_ALT As Integer = 1
        Public Const MOD\_CONTROL As Integer = 2
        Public Const MOD\_SHIFT As Integer = 4
        Public Const MOD\_WIN As Integer = 8
        Private f As Form
        Public Enum EKeyModifier
            Neutral
            Alt
            Control
            Control\_Alt
            Shift
            Shift\_Alt
            Shift\_Control
            Shift\_Alt\_Control
            Windows
            Win\_Alt
            Win\_Control
            Win\_Control\_Alt
            Win\_Shift
            Win\_Shift\_Alt
            Win\_Shift\_Control
            Win\_Shift\_Alt\_Control
            Norepeat = &H4000
        End Enum
        Private \_Modifier As EKeyModifier
        Private \_Key As System.Windows.Forms.Keys        ' the id for the hotkey
        Dim hotkeyID As Short
        Sub SDummy()
            MsgBox("Dummy routine ShortKey")
        End Sub
        Public Property Key() As System.Windows.Forms.Keys
            Get
                Return \_Key
            End Get
            Set(ByVal value As System.Windows.Forms.Keys)
                UnregisterGlobalHotKey()
                RegisterGlobalHotKey(value, \_Modifier, f)
                \_Key = value
            End Set
        End Property
        Public Property Modifier() As EKeyModifier
            Get
                Return \_Modifier
            End Get
            Set(ByVal value As EKeyModifier)
                UnregisterGlobalHotKey()
                RegisterGlobalHotKey(\_Key, value, f)
                \_Modifier = value
            End Set
        End Property
    
        ' register a global hot key
        Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean
            Try
                f = frm
                ' use the GlobalAddAtom API to get a unique ID (as suggested by M
    
    J 1 Reply Last reply
    0
    • J JR212

      Hi, I use for some years this code that worked.

      Public Class HotKey
      ' Windows API functions and constants
      Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
      Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
      Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
      Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short

          Public Const MOD\_ALT As Integer = 1
          Public Const MOD\_CONTROL As Integer = 2
          Public Const MOD\_SHIFT As Integer = 4
          Public Const MOD\_WIN As Integer = 8
          Private f As Form
          Public Enum EKeyModifier
              Neutral
              Alt
              Control
              Control\_Alt
              Shift
              Shift\_Alt
              Shift\_Control
              Shift\_Alt\_Control
              Windows
              Win\_Alt
              Win\_Control
              Win\_Control\_Alt
              Win\_Shift
              Win\_Shift\_Alt
              Win\_Shift\_Control
              Win\_Shift\_Alt\_Control
              Norepeat = &H4000
          End Enum
          Private \_Modifier As EKeyModifier
          Private \_Key As System.Windows.Forms.Keys        ' the id for the hotkey
          Dim hotkeyID As Short
          Sub SDummy()
              MsgBox("Dummy routine ShortKey")
          End Sub
          Public Property Key() As System.Windows.Forms.Keys
              Get
                  Return \_Key
              End Get
              Set(ByVal value As System.Windows.Forms.Keys)
                  UnregisterGlobalHotKey()
                  RegisterGlobalHotKey(value, \_Modifier, f)
                  \_Key = value
              End Set
          End Property
          Public Property Modifier() As EKeyModifier
              Get
                  Return \_Modifier
              End Get
              Set(ByVal value As EKeyModifier)
                  UnregisterGlobalHotKey()
                  RegisterGlobalHotKey(\_Key, value, f)
                  \_Modifier = value
              End Set
          End Property
      
          ' register a global hot key
          Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean
              Try
                  f = frm
                  ' use the GlobalAddAtom API to get a unique ID (as suggested by M
      
      J Offline
      J Offline
      JR212
      wrote on last edited by
      #2

      Really nobody that can help?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups