Processing WM_KEYDOWN for Form or Control

Started by TechSupport, November 29, 2007, 01:31:43 PM

Previous topic - Next topic

TechSupport

Here is some info that I just sent a customer about handling the WM_KEYDOWN message in a Form (with no controls) and for Controls themselves. I thought that maybe it would beneficial to others as well:

To capture arrow keys in a Form by itself, create a command button on the form and set its WS_VISIBLE style to FALSE (via the WindowStyles property of the command button). Go to the "Special Functions" tree branch in the FireFly Project Explorer. Select the FF_PUMPHOOK function and add code like the following below.


Function FF_PUMPHOOK(Msg As tagMsg) As Long


   'If this function returns FALSE (zero) then the processing of the regular
   'FireFly message pump will continue. Returning TRUE (non-zero) will bypass
   'the regular message pump.

   Function = %FALSE    'return %TRUE if you need to bypass the FireFly message pump

   'If you are dealing with a 'normal' OCX control then the following code will
   'allow the message to be forwarded to the OCX. Some OCX's have child windows
   'so it may be necessary to modify the GetFocus() to properly reflect the correct window.
   'If you are not using any OCX's then you can simply comment or delete this line.

   'Function = SendMessage( GetFocus(), &H37F, 0, VarPtr(Msg))

   
   ' Trap a keydown message for a specific form. The form itself does
   ' not have keyboard focus so we need to intercept the keyboard
   ' messages intended for the command button with WS_VISIBLE = FALSE
   Select Case Msg.hWnd
      Case HWND_FORM1_COMMAND1

         Select Case Msg.message
           
            Case %WM_GETDLGCODE
               Function = %DLGC_WANTALLKEYS
               
            Case %WM_KEYDOWN
               MsgBox "keydown"
         End Select

   End Select
   
   
End Function




To capture an arrow key via WM_KEYDOWN in a specific control that has focus, add code like the following below to that control's CUSTOM message handler:


Function FORM1_COMMAND1_CUSTOM ( _
                               ControlIndex  As Long,  _  ' index in Control Array
                               hWndForm      As Dword, _  ' handle of Form
                               hWndControl   As Dword, _  ' handle of Control
                               wMsg          As Long,  _  ' type of message
                               wParam        As Dword, _  ' first message parameter
                               lParam        As Long   _  ' second message parameter
                               ) As Long

   Select Case wMsg   
     
      Case %WM_GETDLGCODE
         Function = %DLGC_WANTALLKEYS
         Exit Function
         
      Case %WM_KEYDOWN
         MsgBox "keydown"
   End Select
   
     
End Function


The "trick" here is the %WM_GETDLGCODE message return value. We need to do this because controls in FireFly are subclassed.