Catch Keystrokes at the FORM level

Started by Mark Strickland, December 12, 2005, 04:36:54 PM

Previous topic - Next topic

Mark Strickland

Is it possible to catch keystrokes at the form level not the individual control level.

I have a large number of forms and controls in a project.  It would be nice to hot have to put code in every control to just catch the F1 key for help.

I have played a little with the various events available in a FORM but it seems there is no message that will fire for pressing keys like F1,F2,F3, etc.  (Actually F1 does fire a message but other Fn keys don't).

Thanks.

TechSupport

I think that the easiest solution is to use a keyboard hook. Below is some sample code that you can use:



Global ghHook As Long


'------------------------------------------------------------------------
Function FORM1_WM_CREATE ( _
                        hWndForm As Dword, _  ' handle of Form
                        ByVal UserData As Long _  'optional user defined Long value
                        ) As Long

   'Install hook Procedure:
   ghHook = SetWindowsHookEx( %WH_KEYBOARD, _
                              CodePtr(KeyBoardHookFunction), _
                              App.hInstance, _
                              0)

End Function



'------------------------------------------------------------------------
Function KeyBoardHookFunction( ByVal HookCode As Long, _
                              ByVal wParam As Long, _
                              ByVal lParam As Long _
                              ) As Long
                             
 If Bit(lParam, 31) Then   'Only grab the keyup
   
    Select Case wParam
       Case %VK_F1
          MsgBox "F1 key has been pressed"
    End Select
   
 End If
 
End Function                  


'------------------------------------------------------------------------
Function FORM1_WM_DESTROY ( _
                         hWndForm      As Dword _  ' handle of Form
                         ) As Long
                         
  If ghHook Then UnhookWindowsHookEx ghHook

End Function


Mark Strickland

Paul,

Thanks --- that works great.

I keep specific "field" edit and help info in the TAG of a control.  Now I just need to know the handle of the active control when I pressed the F key.

Thanks.

TechSupport

Quote from: Mark StricklandNow I just need to know the handle of the active control when I pressed the F key.


hActiveControl = GetFocus()

Mark Strickland

Paul,

Once again you come through!!!

That did the trick.  I had just spent the last hour or so looking at the Win32API help file.

Sometimes Windows is Soooo Easy and sometimes it is SOOOOO HARD!

That is going to save me adding one line of code to about 500 controls.

I had already found the GETACTIVEWINDOW but two functions with no params --- how simple can you get.

Paul --- We should write a book and fill it with all of these little tidbits.  I am sure it would sell.  Most of this is fairly simple but you just can't find it written anywhere.

Thanks.