Recent

Author Topic: TForm.OnMouseLeave not working  (Read 9842 times)

jamie

  • Hero Member
  • *****
  • Posts: 4214
Re: TForm.OnMouseLeave not working
« Reply #30 on: November 10, 2020, 03:00:23 am »
I aways use PtInRect because its a API call in windows if you reference it from the Windows unit but, its actually faster to use it locally instead.

if you use the LCLintf unit it cantains it so it will work in all worlds.

PointInRect must be someone's nightmare idea cause its just a repeat unless it uses different sub member types, like floats maybe?

also with a TRect it has helpers..
 
aRect.Contains(ThePoint) etc..

 you could make up your rectangle on the fly I guess.

 Rect(x,y,x2,y2).Contains(thePoint)

and I think there is also an overload to check if another Trect fits in there too.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: TForm.OnMouseLeave not working
« Reply #31 on: November 10, 2020, 03:06:04 am »
Perhaps you moan about the 1 Pixel in X and one Pixel in Y:

I don't need high accuracy. But if you think that could be the problem, okay I tried it. Modified the code and run it again. That made no difference. Still got many misses.

use a borderless control and set the cursor shape to a custom shape? at least it will change to a different shape as you pass over it.

Which component will you suggest? The code provided is only a demo, actually I will use a TImage.

Reason to use the  PtInRect function  :D

I knew PtInRect. But using PtInRect or not should not cause the issue.

I tested your code: Everything works fine. With a quick mouse.

Could it be a platform specific issue? I use Ubuntu Mate 20.04.
« Last Edit: November 10, 2020, 03:21:01 am by Handoko »

winni

  • Hero Member
  • *****
  • Posts: 2219
Re: TForm.OnMouseLeave not working
« Reply #32 on: November 10, 2020, 03:23:32 am »
Hi Handoko!

Slow computer? Slow Graphics?

I use a Ryzen 5 with Radeon Vega on the CPU (or whatever they call it now).

But look this forum for OS trouble related to fpc/Laz:

* Unbeaten numer one is the Mac
* But No 2 is Ubuntu with it's problems

To use Debian as base for a desktop PC is a strange decision.
Debian is the worst Linux related to the desktop.
It is designed to be a robust long term server OS.
And that it is.

Take another Linux. I am not the one to convince you to Suse.
There are a lot of other good distros.

Winni

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: TForm.OnMouseLeave not working
« Reply #33 on: November 10, 2020, 03:43:27 am »
The code is simply reading mouse position and do comparison, it should be able to run on very old computers. Yes, I am using Core2Duo with integrated VGA, not a high-end machine. But I still can play many 3D games.

If I just want to do mouse onHover effect, which commonly used on websites. It sounds bad if the program requires high specification machines.

Changing OS may solve the problem. But that does not really fix the issue.

I examine AddOnUserInputHandler you suggested. I found that it won't be fired if the mouse pointer is outside the form.

winni

  • Hero Member
  • *****
  • Posts: 2219
Re: TForm.OnMouseLeave not working
« Reply #34 on: November 10, 2020, 02:07:41 pm »

Changing OS may solve the problem. But that does not really fix the issue.

I examine AddOnUserInputHandler you suggested. I found that it won't be fired if the mouse pointer is outside the form.

Hi!

Tested your code with Win7 on VirtualBox - works fluently.

The AddOnUserInputHandler  fires only inside your Form(s).
That's why it is named Application.AddOnUserInputHandler .

My workaround for your problem is this:

* Put a timer on your form
* Set the tick intervall to 100 ms

Add the TimerEvent:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   Caption:= IntToStr(Mouse.CursorPos.x)+' / '+IntToStr(Mouse.CursorPos.y);
  4. end;
  5.  

Now move your mouse outside the Form.
And inside.
You see: the InputHandler of the App is not touched and works still.

Perhaps there is a more intelligent way to do this.
But I have not found.

Winni
« Last Edit: November 10, 2020, 02:11:44 pm by winni »

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: TForm.OnMouseLeave not working
« Reply #35 on: November 10, 2020, 02:59:09 pm »
Thank you for the suggestion. I already used a TTimer yesterday, before you mentioned it. Yes it works. Not a good solution but indeed is a workaround.

I performed some tests first using a TToolbar, it had OnMouseLeave issue too. So I tested the toolbars of my Pale Moon and Firefox browsers, I was unable to reproduce the issue. Then I tested the toolbar of my Lazarus' compoment tab, the issue was reproducible. So now I believe it is not a problem of my OS.

I am going to dig deeper. It should be a fix or better solution. I don't think Pale Moon and Firefox use a TTimer to make their toolbars to work correctly.  :D


Update:

From the web, I found a Terminal command:
xinput test-xi2 --root

It is for showing the mouse activity. If you run the command without --root, it shows the mouse position only if the pointer is inside the form.

Can anyone find the API? If Lazarus can use the system-wide hook I believe the bug can be fixed.
 
« Last Edit: November 10, 2020, 03:37:06 pm by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: TForm.OnMouseLeave not working
« Reply #36 on: December 23, 2020, 02:11:12 pm »
I found the workaround for this issue. Using LM_MOUSELEAVE on the mainform the mouse leave event will be triggered 100%.

Here is the summary:

  • Visual components' OnMouseLeave event does not 100% triggered if you move the mouse pointer out the mainform fast enough.
  • This issue does not happen on Windows (I tested only on Win7). But it happens on Linux GTK2 (tested on Ubuntu Mate).
  • I found using LM_MOUSELEAVE message can solve the problem. Success rate is 100%.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Graphics, ExtCtrls, StdCtrls, Messages, LMessages;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     CheckBox1: TCheckBox;
  16.     Shape1: TShape;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure Shape1MouseEnter(Sender: TObject);
  19.     procedure Shape1MouseLeave(Sender: TObject);
  20.   private
  21.     procedure SysMessage(var Msg: TMessage); message LM_MOUSELEAVE;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.   Constraints.MinHeight := 240;
  36.   Constraints.MinWidth  := 320;
  37.   Shape1.Height         := Height - 72;
  38.   Shape1.Width          := Width  - 48;
  39.   Shape1.Anchors        := [akTop, akBottom, akLeft, akRight];
  40. end;
  41.  
  42. procedure TForm1.Shape1MouseEnter(Sender: TObject);
  43. begin
  44.   Caption := 'OnMouseEnter triggered';
  45.   Shape1.Brush.Color := clRed;
  46. end;
  47.  
  48. procedure TForm1.Shape1MouseLeave(Sender: TObject);
  49. begin
  50.   Caption := 'OnMouseLeave triggered';
  51.   Shape1.Brush.Color := clWhite;
  52. end;
  53.  
  54. procedure TForm1.SysMessage(var Msg: TMessage);
  55. begin
  56.   if CheckBox1.Checked then
  57.     Shape1MouseLeave(Self);
  58. end;
  59.  
  60. end.

This is only a workaround, not really a fix for the problem. Maybe can be useful for someone that has same problem in the future.

 

TinyPortal © 2005-2018