• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

System Wide Hotkeys

Experts,
Can anyone please provide/point out some code which allows a user to set up hotkeys (in my application, I'd like the user to be able to assign 3-4 hotkeys to turn timers on and off, generate reports etc)

Preferably code which has actually worked for you!

I've tried some code I found on the 'Net, and even an 'MCL' component, but they didn't work out...

Thanks
0
DoctorNash
Asked:
DoctorNash
  • 3
  • 2
1 Solution
 
DoctorNashAuthor Commented:
Could you please check and advise if the following hotkey code works for you:

Thanks in advance

---Form 1---

Private Sub Form_Load()
  Call SetHotKey(Me.hwnd, &H1 + &H2, &H77)
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call RemoveHotKey
End Sub

Private Sub Command1_Click()
  App.TaskVisible = False
  Me.Hide
End Sub

---end Form 1---

 
---Module 1---

Option Explicit

Private Declare Function RegisterHotKey Lib "user32" (ByVal _
        hWnd As Long, ByVal id As Long, ByVal fsModifiers As _
        Long, ByVal vk As Long) As Long
       
Private Declare Function UnregisterHotKey Lib "user32" (ByVal _
        hWnd As Long, ByVal id As Long) As Long

Private Declare Function CallWindowProc Lib "user32" Alias _
        "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _
        hWnd As Long, ByVal Msg As Long, ByVal wParam As _
        Long, ByVal lParam As Long) As Long
       
Private Declare Function SetWindowLong Lib "user32" Alias _
        "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _
        As Long, ByVal dwNewLong As Long) As Long

Private Declare Function GlobalDeleteAtom Lib "kernel32" _
        (ByVal nAtom As Integer) As Integer
       
Private Declare Function GlobalAddAtom Lib "kernel32" Alias _
        "GlobalAddAtomA" (ByVal lpString As String) As Integer

Const GWL_WNDPROC = (-4)
Const WM_HOTKEY = &H312

Public Enum ModKeys
  MOD_ALT = &H1
  MOD_CONTROL = &H2
  MOD_SHIFT = &H4
  MOD_WIN = &H8
End Enum

Dim OldProc As Long, hOwner As Long
Dim iAtom As Integer

Public Sub SetHotKey(hWnd As Long, ModKey As ModKeys, _
                          vKey As Long)
  hOwner = hWnd
 
  iAtom = GlobalAddAtom("MyHotKey")
  Call RegisterHotKey(hOwner, iAtom, ModKey, vKey)
 
  OldProc = SetWindowLong(hOwner, GWL_WNDPROC, _
                          AddressOf WndProc)
End Sub

Public Sub RemoveHotKey()
  If hOwner = 0 Then Exit Sub
  Call UnregisterHotKey(hOwner, iAtom)
  Call SetWindowLong(hOwner, GWL_WNDPROC, OldProc)
  Call GlobalDeleteAtom(iAtom)
End Sub

Public Function WndProc(ByVal hWnd As Long, _
                        ByVal wMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long
                       
  If wMsg = WM_HOTKEY And wParam = iAtom Then
    Form1.Show
  Else
    WndProc = CallWindowProc(OldProc, hWnd, wMsg, _
                             wParam, lParam)
  End If
End Function

---End Module 1---
0
 
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
0
 
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
0
 
DoctorNashAuthor Commented:
Navstar16, you're magnificent! The second 'codeguru' link is one that I hadn't discovered to date, and turns out to be the only code that actually works for me! A thousand thanks. I've since modified it/built on it to do what I require.

BTW, I've posted another HotKey related question:
'Re. Hotkey Creation - Simple Question but Urgent Answer needed' that you may want to take a peek at...
0
 
DoctorNashAuthor Commented:
Experts,

Have set up my user defined hotkeys. It works, but am getting strange behaviour with the hotkey control in the following case:

If user selects Cntrl + a key as the hotkey, then the hotkey is correctly registered ie, when user presses Cntrl + a key, the hotkey is triggered. HOWEVER, if user selects Alt + a key, or Shift + a key, the reciprocal hotkey to the one expected gets registered.

ie user thinks he has registered Alt + a key, but hotkey actually triggers when user presses Shift + a key, NOT Alt + a key.

I believe I have correctly set up the constants as
Cntrl = 2
Shift = 4
Alt = 1

Thanx

Similarly, user thinks he has registered Shift + a key, but hotkey actually triggers when user presses Alt + a key NOT Shift + a key.

Why is the recognition of Alt and Shift being reversed by the system?? What's going on here?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now