Can any body tell me how can I detect Key press when my focus is not on the Form, Like I've Hidden The Form and I want to make it visible by pressing a single key or combination of keys I tried VBKEY case but that works fine unless I lose focus on Form?

Dani AI

Generated

A compact, reliable approach is to register a system‑wide hotkey and have a hidden form (or message window) receive WM_HOTKEY. That follows ' suggestion but adds a safe VB6 pattern: declare the APIs, subclass the form to catch WM_HOTKEY, register the hotkey at load, and always restore/unregister at unload. The timer polling idea from is fragile and wastes cycles; it also won’t detect keys when another app has exclusive input.

' Standard module (modHotKey)
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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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

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

Public PrevWndProc As Long
Public HotKeyID As Long

Public 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
        If wParam = HotKeyID Then
            Form1.Visible = True
            Form1.SetFocus
        End If
    End If
    WindowProc = CallWindowProc(PrevWndProc, hwnd, uMsg, wParam, lParam)
End Function
' In Form1
Private Sub Form_Load()
    HotKeyID = 100
    PrevWndProc = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    If RegisterHotKey(Me.hWnd, HotKeyID, MOD_CONTROL Or MOD_ALT, vbKeyF12) = 0 Then
        MsgBox "RegisterHotKey failed"
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    UnregisterHotKey Me.hWnd, HotKeyID
    SetWindowLong Me.hWnd, GWL_WNDPROC, PrevWndProc
End Sub

Notes and cautions: always check RegisterHotKey’s return value (registration can fail if another app already owns the combo), use a unique ID per hotkey, and restore the original WndProc before the form/process ends. Subclassing in VB6 is brittle—if the app crashes while subclassed, instability can occur—so prefer a mature subclass helper library or a small helper process if stability is critical. ’s warning about cleanup is valid in spirit: unregistering is essential (a crash usually releases the hotkey when the process exits, but it’s bad practice to rely on that). For highly sensitive needs, a low‑level keyboard hook or external service can be used, but those are more complex and riskier than RegisterHotKey.

Recommended Answers

All 3 Replies

Hi

You will need to register a global hot key and ideally unregister it when your program closes. Take a look at the following Win32 API functions:

HTH

or what you can do is use a timmer in Hidden Form when ever you lose focus of that hidden form the timmer becomes activated and then you can get back your focus on the hidden form and then you can use your key or combination of keys to unhide your form and when your form is not hidden timmer is deactivated.

If you do not unregister your hotkey before shutting down your program, you can cause a Big Blue Screen of Death. I did that a few times when I started working with HotKeys years ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.