PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: _WM_KEYDOWN handler questions  (Read 2450 times)

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
_WM_KEYDOWN handler questions
« on: October 20, 2013, 06:11:45 PM »

This one has me puzzled...  ???

If I put only the following code into a _WM_KEYDOWN handler for a RichEdit (for instance):
Code: [Select]
    Beep
    Function=%True
I get a beep for each keystroke and non-printable characters are blocked (a couple excepts mentioned below). So far, so good.  But then I do get all printable  characters (Ok, I didn't verify all, but all alpha, numeric, and many others including, "/.,?><":';" etc.

Shouldn't the "Function = %True" stop processing for all keystrokes in this scenario?

I actually tested two controls, and EditBox and a RichEdit.  Tab took me to the next control for both of them. Enter was blocked for the RichEdit, but made it to the EditBox. 

Any thoughts?

David
Logged

Eddy Van Esch

  • Senior Member
  • ***
  • Posts: 246
  • FF3 User
    • DevOTechS
Re: _WM_KEYDOWN handler questions
« Reply #1 on: October 20, 2013, 06:31:16 PM »

David,

In order to intercept (and 'swallow' ) some keystrokes entered in a control, I think you need to subclass that control.
If you do not subclass the control, keystrokes are passed to the controls form WndProc routine no matter what.
Someone please correct me if I'm wrong ...

Kind regards
Logged
Eddy

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #2 on: October 20, 2013, 07:43:02 PM »

Thanks for the reply Eddy,

It's my understanding that FF is subclassing the controls for us.  That is why we have the option to use "Function = %True", to let FF know we don't want further processing on that message to occur (actually FF passes the flag along to let Windows know we don't want further processing done).  I also believe there are many messages that wouldn't be available to us unless FF was subclassing the controls.  Of course, I could be wrong on any one of these beliefs.  :-\
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #3 on: October 20, 2013, 08:36:37 PM »

Hi guys, I haven't tested any of this (not at development computer) but whenever I want to have full control over incoming messages to a Form or Control, I intercept them in the FF_PUMPHOOK handler. There you get the raw, unprocessed, incoming message and you can do anything with it. It has always worked great for me.
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #4 on: October 21, 2013, 01:38:58 AM »

Will give that a try... I've seen you give that advice before (not to me thankfully), so I should have thought of it.  I'll let you know how it turns out.
Logged

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #5 on: October 21, 2013, 02:39:40 AM »

Works perfectly!  Thanks Paul!

That does seem to indicate that there is some bug in the FF message handlers though.

Best regards,

David
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #6 on: October 25, 2013, 03:00:23 PM »

Hi David,

I had occassion to do a lot of work today with key handling in one of my programs so I decided to use the message handlers rather than the PumpHook. In order to catch all of the keystrokes you need to handle WM_KEYDOWN, WM_CHAR and also %WM_GETDLGCODE. Works perfectly for me.

Here is a template that you can use.
Code: [Select]

'--------------------------------------------------------------------------------
Function FORM1_TEXT1_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_CHAR
         Function = -1:  Exit Function
         
         ' Prevent the message beeps when Enter or Escape are pressed
         'Select Case wParam
         '   Case %VK_RETURN, %VK_ESCAPE
         '      Function = -1
         '      Exit Function
         'End Select
         
      Case %WM_KEYDOWN                     
         Function = -1:  Exit Function

         'Select Case wParam
         '   Case %VK_RETURN
         '      ' do something here in response to the ENTER key
         '      Function = -1
         '   Case %VK_ESCAPE
         '      ' do something here in response to the ESCAPE key
         '      Function = -1
         '      Exit Function   
         'End Select

      Case %WM_GETDLGCODE
         ' MS system responds irregular to Enter key - better handle it ourselves
          Local pMsg As TAGMSG Ptr
          Local iMsg As Long

          pMsg = lParam
          If pMsg > 0 Then
             iMsg = @pMsg.Message
             If (iMsg = %WM_KEYDOWN) Or (iMsg = %WM_CHAR) Then 
                Function = %DLGC_WANTALLKEYS
                Exit Function
             End If
          End If

   End Select
   
End Function

Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #7 on: October 26, 2013, 05:55:47 AM »

Well, I must admit that I didn't try the custom message handler.  ???  But that's only because there is a dedicated handler for WM_CHAR and WM_KEYDOWN. ::)  Are those supposed to be used some other way? It's strange because I could see the FF routine hat called that handler pass the return code along to whatever called it.  I didn't try to follow it further up.

I didn't know about WM_GETDLGCODE.  Thanks for the tip and the template!
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #8 on: October 26, 2013, 08:40:55 AM »

I guess I could have broke the code out into the separate WM_CHAR and WM_KEYDOWN handlers but I would still have to use WM_GETDLGCODE in the Custom handler.... so, I decided to put it all in one place for the sake of togetherness. :)
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #9 on: October 26, 2013, 02:08:09 PM »

I see what's going on now!  I reread my first post.  It was written poorly and my message was not so clear. :-[

Look at the attached project.

Compile and run the project and observe the control behavior when typing.  Remove the comment on the line in the pump hook and retest as before.  Notice the different behavior.

Best regards,

David


Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #10 on: October 26, 2013, 06:12:04 PM »

It is not as easy/clear as you might expect. You should read up on how Windows handles key processing. It can be confusing. In FireFly, if you intercept WM_KEYDOWN in the PumpHook then the message never makes it to the TanslateMessage function that is in FireFly's message loop.

If the message makes it to TranslateMessage then it will get dispatched to the Form/Control and will also generate a WM_CHAR message. At that point you need to deal with WM_CHAR (regular alphanumeric kind of characters) and WM_KEYDOWN (for arrow keys, function keys, Jome/End, PgUp, PgDn, etc)

These links may help to explain things better than me:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646268(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646276(v=vs.85).aspx
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #11 on: October 26, 2013, 06:25:30 PM »

...and I shouldn't be using the %VK_ codes in WM_CHAR. I should be testing for character codes (eg. ENTER = 13, ESCAPE = 27, etc).
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #12 on: October 26, 2013, 10:50:29 PM »

But why would FF translate it before calling the Control's WM_KEYDOWN message handler.  Shouldn't it be calling it after that message handler returns so that it can skip the TranslateMessage call if the handler returns a %True?

It's OK if the answer is that it couldn't be done any other way.  :)

Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8781
  • Windows 10
    • PlanetSquires Software
Re: _WM_KEYDOWN handler questions
« Reply #13 on: October 27, 2013, 09:52:36 AM »

But why would FF translate it before calling the Control's WM_KEYDOWN message handler.  Shouldn't it be calling it after that message handler returns so that it can skip the TranslateMessage call if the handler returns a %True?
I guess that it could have been done that way but it sure would have resulted in one hell of a large message pump. I would have to add code to preprocess every message handler for every form and every control in the application. Whereas, now the design of FireFly allows the messages for the form/control to be processed in the individual specific forms/controls Window Procedures. It makes for better modular code with no loss of functionality.

I guess that I implied that it is TranslateMessage that sends the keystroke messages to the form/control but it is actually DispatchMessage that does that work. TranslateMessage and DispatchMessage are usually paired together in the message pump.
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

David Kenny

  • Senior Member
  • ***
  • Posts: 454
  • Windows 7
Re: _WM_KEYDOWN handler questions
« Reply #14 on: October 27, 2013, 01:13:37 PM »

Quote
I guess that it could have been done that way but it sure would have resulted in one hell of a large message pump.
I can accept that.  I had always been operating under the assumption that code could be plucked from a case structure in a regular PB program and inserted into the appropriate FF message handler (and you can some 99% of the time ;D) and it would work the same.  But it's good to know the conditions that dictate when it needs to be handled differently.

I withdraw my assertion that there must be some bug in FF's message pump as it's more a limitation that I was unaware of.

Thanks Paul!
Logged