Recent

Author Topic: [SOLVED] Reading the mouse wheel delta in a side thread  (Read 1982 times)

furious programming

  • Hero Member
  • *****
  • Posts: 858
[SOLVED] Reading the mouse wheel delta in a side thread
« on: February 19, 2021, 01:51:45 pm »
I have a side thread that gets the state of the gamepad, keyboard and mouse buttons 60 times per second (just like a regular game). To read the state of controller buttons I use the joyGetPosEx function, for keyboard and mouse buttons I use the GetKeyState function. The problem occurs with the wheel of the mouse, because the status of the wheel cannot be read with GetKeyState.

After the wheel is turned, the system sends a message to the window, this is processed in a loop, and the OnMouseWheel event is dispatched. The problem, however, is that the state of the mouse buttons is to be retrieved in a side thread (the form should have nothing to do with it), and the thread will not get this message about the rotation of the scroll.

I can just use the OnMouseWheel event to write/increment the delta in the custom form field, and the thread will read that value and reset the variable. But this is a fallback option because I don't like it — sounds dirty.

Anyone know of any way to read the mouse wheel delta without using a form message loop? Is it even possible?
« Last Edit: February 21, 2021, 12:54:20 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

440bx

  • Hero Member
  • *****
  • Posts: 4023
Re: Reading the mouse wheel delta in a side thread
« Reply #1 on: February 19, 2021, 02:36:32 pm »
Anyone know of any way to read the mouse wheel delta without using a form message loop? Is it even possible?
I don't believe it is possible because scroll wheel actions are not a state (like a key being pressed).  The scrolling of the wheel is _always_ in a message.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Reading the mouse wheel delta in a side thread
« Reply #2 on: February 19, 2021, 02:59:02 pm »
Ok, thank you for the explanetion. What do you think about mouse hook?
« Last Edit: February 19, 2021, 03:02:30 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

440bx

  • Hero Member
  • *****
  • Posts: 4023
Re: Reading the mouse wheel delta in a side thread
« Reply #3 on: February 19, 2021, 03:57:10 pm »
Ok, thank you for the explanetion. What do you think about mouse hook?
You're welcome.  As far as a mouse hook, it seems to me that, whatever the hook routine does could just as well be done when processing the WM_MOUSEWHEEL message.  IOW, I don't see what advantage, if any, hooking the mouse would provide.

At this time, I think the "dirty" solution (or a variation thereof) you mentioned originally may very well be the simplest.  One mechanism that comes to mind would be to have the thread that pumps window messages forward the message to the thread that keeps track of the other various events.  That would require that thread to have a message queue and use either GetInputState or PeekMessage to find out if it got one of those mouse wheel messages without blocking for it.  That's the closest (and simplest at this time) thing that comes to mind to implement a "mouse wheel state" for the "state" thread.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: [SOLVED] Reading the mouse wheel delta in a side thread
« Reply #4 on: February 24, 2021, 12:58:44 am »
Ok, everything is clear. Probably I will do it in a ”dirty” way—declare a global variable for scroll state (forward, middle and backward) in the from of enum and read it's value directly from the side event, without synchronization (it will be not needed). There is no sense to do a complicated code.

Thank you again, problem is solved.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018