Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Receiving Hotkeys

Posted on 1998-06-19
2
Medium Priority
?
415 Views
Last Modified: 2010-05-03
With RegisterHotkey(...) a HotKey is registered.
But how can I receive notification, when the selected HotKey is pressed (anywhere in the system)?
0
Comment
Question by:soeding1
2 Comments
 
LVL 4

Accepted Solution

by:
yowkee earned 400 total points
ID: 1463832
soeding1,

  When your application, you use RegisterHotkey to register a hotkey, then when the seleted hotkey is pressed, it will be send to your application a message WM_HOTKEY. Then your application must handle this message and do what you want to do. In VB this required subclassing of windows procedure.

  Here's an example to register hotkey Ctrl-1, when the hotkey pressed, restore the minimized form and set it focus.

---- Codes in a BAS module
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Public Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
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
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, _
     ByVal nIndex As Long, _
     ByVal dwNewLong As Long) As Long
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Public Const SW_NORMAL = 1
Public Const GWL_WNDPROC = -4
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4

Public lpPrevWndProc As Long
Public AtomID As Integer

Public Sub SubClass(ByVal hwnd As Long)
    lpPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub UnSubClass(ByVal hwnd As Long)
    Dim lngReturnValue As Long
    If lpPrevWndProc Then _
        Call SetWindowLong(hwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hwnd As Long, _
                    ByVal uMsg As Long, _
                    ByVal wParam As Long, _
                    ByVal lParam As Long) As Long
       
    If uMsg = WM_HOTKEY Then
        Call ShowWindow(hwnd, SW_NORMAL)
        Call SetFocus(hwnd)
        Call SetForegroundWindow(hwnd)
    End If
    WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)

End Function

----Code in Form module
Private Sub Form_Load()
     AtomID = GlobalAddAtom("Atom1")
     Call RegisterHotKey(hwnd, AtomID, MOD_CONTROL, vbKey1)
     SubClass Me.hwnd
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    UnSubClass Me.hwnd
    Call UnregisterHotKey(hwnd, AtomID)
    Call GlobalDeleteAtom(AtomID)
End Sub

-----

Remember to unregister the hotkey and delete the added atom when application exit.

Regards.

0
 

Author Comment

by:soeding1
ID: 1463833
Thank you, yowkee.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question