Recent

Author Topic: [SOLVED] Keep mouse in one window  (Read 536 times)

What I can do

  • Jr. Member
  • **
  • Posts: 67
[SOLVED] Keep mouse in one window
« on: June 21, 2024, 04:08:12 pm »
OS: Windows 10
Project: keep use from leaving the bounds of the window

This was very easy with Delphi
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject) ;
  2.  var r : TRect;
  3.  begin
  4.      //it would be a good idea to move the
  5.      //mouse inside the form before restriction
  6.     r := BoundsRect;
  7.     ClipCursor(@R) ;
  8.  end;
  9.  
  10.  procedure TForm1.FormClick(Sender: TObject) ;
  11.  begin
  12.     //always be sure to release the cursor
  13.     ClipCursor(nil) ;
  14.  end;

but not compatiable with Lazarus

I have a kid who plays simple on line games but I make him earn it by learning some words on my flash card type app
I just want to keep the mouse inside that flash card app until he is done
« Last Edit: June 21, 2024, 06:27:48 pm by What I can do »

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: Keep mouse in one window
« Reply #1 on: June 21, 2024, 04:17:10 pm »
Try OnActivate instead of OnCreate

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10066
  • Debugger - SynEdit - and more
    • wiki
Re: Keep mouse in one window
« Reply #2 on: June 21, 2024, 05:40:34 pm »
Code: Pascal  [Select][+][-]
  1. uses windows;

ClipCursor is in the windows unit. It only works for Windows.

And, well, it has a different call signature
Code: Pascal  [Select][+][-]
  1. ClipCursor(R)
You don't need the "@" => that is handled by the compiler.

Though, given that, I am not sure how you can pass "nil"....

You can always define it yourself.  (still "uses Windows;" so you have the types)
Code: Pascal  [Select][+][-]
  1. function MyClipCursor(lpRect:PRECT):WINBOOL; external 'user32' name 'ClipCursor';


Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: Keep mouse in one window
« Reply #3 on: June 21, 2024, 06:02:48 pm »

What I can do

  • Jr. Member
  • **
  • Posts: 67
[SOLVED] Keep mouse in one window
« Reply #4 on: June 21, 2024, 06:24:54 pm »
Fibonacci
 Yes, it is in the active

 Martin_fr
  Thank you, you were absolutely CORRECT!
  and I tested with the @ and everything still worked fine by just adding Window,
 the @ and nil seamed to work perfectly during the test run

thanks to everybody
hugs, high five, firm handshake

 

TinyPortal © 2005-2018