PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: keyboard enter event  (Read 245 times)

jermy

  • Little Newbie
  • *
  • Posts: 44
keyboard enter event
« on: April 30, 2020, 11:43:41 AM »

Hello people

I don't get a keyboard enter event, this is programmed on purpose?

Code: [Select]
Function frmMain_Button1_AllEvents( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT
      Select Case e.Message
         case WM_LBUTTONUP
            '  frmMain.Close
     
      end select

            print e.Message 'pressing enter with the keyboard does not raise any event ' No BN_CLICKED Event
   Function = 0
End Function

maybe i should use frmMain_Button1_Click as usual?,  and catch the event there
Logged

Josť Roca

  • Guru Member
  • *****
  • Posts: 3270
Re: keyboard enter event
« Reply #1 on: April 30, 2020, 12:45:38 PM »

But the button has the keyboard focus or not?

jermy

  • Little Newbie
  • *
  • Posts: 44
Re: keyboard enter event
« Reply #2 on: April 30, 2020, 01:07:24 PM »

yes, I see that the button is pressed with enter, but no event is raised
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9287
  • Windows 10
    • PlanetSquires Software
Re: keyboard enter event
« Reply #3 on: April 30, 2020, 06:14:32 PM »

There are several ways to accomplish what you are trying to do. You should also be aware that BN_CLICKED is a notification message that is sent to the parent Form window - NOT the control itself. So trying to catch BN_CLICKED in the Button won't work.

I created a Form and added some buttons. My code below works on a button called Button2

Code: [Select]
''
''
Function frmMain_Button2_AllEvents( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT
      Select Case e.Message
         case WM_LBUTTONUP
            ? "WM_LBUTTONUP - AllEvents"
            '  frmMain.Close
     
      end select

            print e.Message 'pressing enter with the keyboard does not raise any event ' No BN_CLICKED Event

' YOU WILL NEVER GET BN_CLICKED HERE because you need to handle AllEvents for the FORM rather than the BUTTON.

   Function = 0
End Function

''
''
Function frmMain_Button2_MouseUp( ByRef sender As wfxButton, ByRef e As EventArgs ) As LRESULT
   if e.LButton then
      ? "WM_LBUTTONUP - MouseUp handler"
   end if

   Function = 0
End Function

''
''
Function frmMain_AllEvents( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   
   select case e.Message
      case WM_COMMAND
         if hiword(e.wParam) = BN_CLICKED then
            if e.lParam = frmMain.Button2.hWindow then
               AfxMsg( "Button #2 clicked: BN_CLICKED from AllEvents" )
            end if
         end if
   end select
   
   Function = 0
End Function


The general idea behind WinFBE's visual designer is to shield you from having to deal (at least most of the time) directly with the raw Windows messages. That's why the various message event handlers exist. From the above code you can see that you can trap the WM_LBUTTONUP in the Button's AllEvents handler or the dedicated MouseUp event. You can also catch the BN_CLICKED from the Form AllEvents handler.

Or, you could simply deal with the Button's Click event handler which is the best place because you can deal with both the operations of the user clicking on the button or pressing the Enter or Space key to click on the button.
« Last Edit: April 30, 2020, 06:16:59 PM by Paul Squires »
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 9287
  • Windows 10
    • PlanetSquires Software
Re: keyboard enter event
« Reply #4 on: April 30, 2020, 06:21:19 PM »

You can also look at other options like the "AcceptButton" property for the Form.

Another approach is to use the "KeyPreview" for the Form and deal with the Key* events for the Form.

You could even use the "MessagePumpHook" message handler for the Form itself to catch every message as it arrives to the form and before it even gets dispatched to the intended control.
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

jermy

  • Little Newbie
  • *
  • Posts: 44
Re: keyboard enter event
« Reply #5 on: May 01, 2020, 10:29:16 AM »

That makes a lot clear,

the selectable AllEvents in the toolbox returns all events except the events that you can also select in the toolbox.

MessagePumpHook, great no more complex subclassing

thanks for the explanation and examples
Logged