Mouse hook misses first event after a key press

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Mouse hook misses first event after a key press

Post by firace »

Not sure what is going on here... This mouse hook detects left clicks just fine, except if a click occurs right after a keypress.
Can anyone confirm? (Win 10 x64, PB 5.73 LTS)

Code: Select all

Procedure MouseHook(nCode, wParam, lParam)
  Select wParam
      
    Case #WM_LBUTTONDOWN
      
      AddGadgetItem(1, -1, "Left Mouse Clicked " + Date())
      
  EndSelect
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0, 0, 0, 240, 320, "Mouse Hook", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

EditorGadget(1, 10, 10, 180, 200)
StringGadget(0, 10, 230, 180, 20, "") : SetActiveGadget(0)

hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseHook(), GetModuleHandle_(0), 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by firace on Thu Jun 10, 2021 9:07 am, edited 1 time in total.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Re: Mouse hook misses first event after a key press

Post by Crusiatus Black »

I can't seem to reproduce this in x64 PureBasic 5.72,
where are you clicking when this seems to happen?

Maybe another application is not calling the next hook in the chain in certain situations.
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Mouse hook misses first event after a key press

Post by firace »

Crusiatus Black wrote: Thu Jun 10, 2021 9:01 am I can't seem to reproduce this in x64 PureBasic 5.72,
where are you clicking when this seems to happen?

Maybe another application is not calling the next hook in the chain in certain situations.
Strange. I'm clicking inside the test window itself. (I have just updated the code in the top post for easier testing.)
I have no other visible applications open - I'm almost suspecting a hardware or driver issue.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Mouse hook misses first event after a key press

Post by RASHAD »

Hi
Did the test before your changes :)
Works fine no problem

Code: Select all

Procedure MouseHook(nCode, wParam, lParam)
  Select wParam
      
    Case #WM_LBUTTONDOWN
           SetGadgetText(0,"Left Mouse Clicked "+ Str(Date()))
      
  EndSelect
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0, 0, 0, 300, 140, "Mouse Hook", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, 280, 20, "")

hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE_LL, @MouseHook(), GetModuleHandle_(0), 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Edit : Works fine even with the renew post

I suggest to use Local Hook better

Code: Select all

Procedure MouseHook(nCode, wParam, lParam)
  Select wParam
      
    Case #WM_LBUTTONDOWN
           SetGadgetText(0,"Left Mouse Clicked "+ Str(Date()))
      
  EndSelect
  ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure

OpenWindow(0, 0, 0, 300, 140, "Mouse Hook", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
TextGadget(0, 10, 10, 280, 20, "")

lpdwProcessId = GetWindowThreadProcessId_(WindowID(0), 0)
hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE, @MouseHook(), GetModuleHandle_(0), lpdwProcessId)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
UnhookWindowsHookEx_(hhkLLMouse)

Egypt my love
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Mouse hook misses first event after a key press

Post by firace »

Just found out something: it works fine if i wait for a second (after my last keypress) before clicking.

So the hook only misses the first mouse event if it occurs too quickly after a keypress.

Also, I just observed the same behavior on a different laptop! So, this seems to rule out a hardware or driver issue.

Anyway, not a big deal, just strange...
Post Reply