Recent

Author Topic: Force mouse to freeze when other application is active  (Read 1715 times)

blankname

  • New Member
  • *
  • Posts: 35
Force mouse to freeze when other application is active
« on: April 19, 2021, 04:53:54 pm »
Hi,
I wonder, if there is any easy way of forcing mouse to freeze with a keyboard combination of buttons when other application is active. Personally, I need that for a specific game, that has problems with manipulating small knobs - when you try to manipulate them with mouse wheel, sometimes mouse moves and you stop activating the knob or even activate other one.

I tried to find something in google, but all I've seen is like "Mouse freeze BUG" or "Help me unfreeze my mouse". Any help?

blankname

  • New Member
  • *
  • Posts: 35
Re: Force mouse to freeze when other application is active
« Reply #1 on: April 19, 2021, 06:24:08 pm »
For now, I use:

Code: Pascal  [Select][+][-]
  1. function KeybdHookFn(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall;
  2. function MouseHookFn(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall;
  3.  
  4. // This code in init
  5.   kHook := SetWindowsHookEx(wh_keybd_ll, @KeybdHookFn, hInstance, 0);
  6.   mHook := SetWindowsHookEx(wh_mouse_ll, @MouseHookFn, hInstance, 0);
  7.   stopmouse := false;
  8.  
  9. // The rest
  10. function KeybdHookFn(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall;
  11. // possible wParam values: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP
  12. var
  13.   info : ^KeybdLLHookStruct absolute lParam;
  14.   lpChar : word;
  15.   kState : TKeyboardState;
  16. begin              
  17.   result := CallNextHookEx(kHook, nCode, wParam, lParam);
  18.   with info^ do
  19.   case wParam of
  20.     wm_keydown : begin
  21.       if vkCode = VK_RMENU then
  22.       begin
  23.         Host.KeybMemo.Lines.Append(DateTimeToStr(Now()) + ': RMenu down');
  24.         stopMouse := true;
  25.       end;
  26.     end;
  27.     wm_keyup: begin
  28.       if vkCode = VK_RMENU then
  29.       begin
  30.         Host.KeybMemo.Lines.Append(DateTimeToStr(Now()) + ': RMenu up');
  31.         stopMouse := false;
  32.       end;
  33.     end;
  34.   end;                                      
  35.   if Host.KeybMemo.Lines.Count > 8 then
  36.     Host.KeybMemo.Lines.Delete(0);
  37. end;  
  38.  
  39. function MouseHookFn(nCode: LongInt; WPARAM: WPARAM; lParam : LPARAM) : LRESULT; stdcall;
  40. // possible wParam values: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_RBUTTONDOWN, WM_RBUTTONUP
  41. var
  42.   info : ^MouseLLHookStruct absolute lParam;
  43. begin
  44.   if not stopMouse then
  45.     result := CallNextHookEx(mHook, nCode, wParam, lParam);
  46. end;
But the mouse does not freeze - it just slows down muchly...
It's less responsive though

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Force mouse to freeze when other application is active
« Reply #2 on: April 19, 2021, 08:15:11 pm »
Hi!

The mouse is a property of the application (unit Forms).

There is a procedure in TApplication:

Code: Pascal  [Select][+][-]
  1. procedure UpdateMouseControl(NewMouseControl: TControl);  

There you can define your NewMouseControl as "Stopmouse"- just do nothing.

Winni

olly

  • New Member
  • *
  • Posts: 42

balazsszekely

  • Guest
Re: Force mouse to freeze when other application is active
« Reply #4 on: April 20, 2021, 06:34:06 am »
@winni @olly @jamie

According to OP:
Quote
I wonder, if there is any easy way of forcing mouse to freeze with a keyboard combination of buttons when other application is active.
He is playing a game, while the lazarus application is running in the background. When a key combination is pressed, the lazarus program steps in and freeze the mouse inside the game. Actually OP is on the right track with the low level keyboard/mouse hook, but the whole idea smells fishy to me.

blankname

  • New Member
  • *
  • Posts: 35
Re: Force mouse to freeze when other application is active
« Reply #5 on: April 20, 2021, 11:27:58 am »
Yeah, I need to freeze the mouse when I'm in game. If you are interestet - it's for MS Flight Simulator 2020, where people complain about that they cannot use auto-pilot knobs with ease.
If I can freeze the mouse, I can use the knobs at least at start of the flight.

Btw: I found that when I use VK_RMENU and then press R-CTRL again, the cursor stops as I want. But it's like a workaround, because I need to use R-CTRL twice...

balazsszekely

  • Guest
Re: Force mouse to freeze when other application is active
« Reply #6 on: April 20, 2021, 12:07:32 pm »
@blankname
Quote
Yeah, I need to freeze the mouse when I'm in game. If you are interestet - it's for MS Flight Simulator 2020, where people complain about that they cannot use auto-pilot knobs with ease.
If I can freeze the mouse, I can use the knobs at least at start of the flight.
I hope you have a honest purpose for this:
Code: Pascal  [Select][+][-]
  1. if StopMouse then
  2.   Result := -1 //add this line
  3. else
  4.   Result := CallNextHookEx(mHook, nCode, wParam, lParam)
  5.  

blankname

  • New Member
  • *
  • Posts: 35
Re: Force mouse to freeze when other application is active
« Reply #7 on: April 20, 2021, 12:25:40 pm »
Code: Pascal  [Select][+][-]
  1. Result := -1
That's what I needed  :) thanks

 

TinyPortal © 2005-2018