TForm.OnMouseLeave not working lazarus / 31-LCL ---------------------------------------------------------------------- Handoko · 2017-05-29 18:17 As the title said, TForm.OnMouseLeave is not working correctly. I'm using Ubuntu Mate 16.10 64-bit Lazarus 1.6.4 FPC 3.0.2 GTK2. You can try putting this code in OnMouseEnter and OnMouseLeave events: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormMouseEnter(Sender: TObject);begin  Caption := 'Mouse Entered';end; procedure TForm1.FormMouseLeave(Sender: TObject);begin  Caption := 'Mouse Left';end; Does it work on your test? Let me know, so I will report it to the bug tracker forum. ---------------------------------------------------------------------- GAN · 2017-05-29 18:38 Yes, you're right, when I pass the mouse over a button or a grid of the form, then display "mouse left". Linux Mint 17.2 64 bit Mate GTK2 - Lazarus 1.6 FPC 3.0.0 ---------------------------------------------------------------------- Handoko · 2017-05-29 18:57 Maybe I don't understand how the Mouse Leave works. But I think it has 2 issues: - If we move the mouse pointer to other controls (for example a button), that is still inside the form the OnMouseLeave will be triggered. I think it is wrong because that control is inside the form. Correct me if I'm wrong. - If we move the mouse pointer from inside to the outside of the form, the OnMouseLeave will not be triggered. It is definitely wrong. (Actually, it will be triggered but at very low chances, perhaps 1% only) Any test from others, maybe Windows? ---------------------------------------------------------------------- balazsszekely · 2017-05-29 20:40 Quote@Handoko Does it work on your test? Let me know, so I will report it to the bug tracker forum. It works both under windows and linux, but only if the form is empty. If you place another control, for example a memo with align-->alClient, the events are not triggered and AFAIK this is the intended behavior, because now the OnMouseEnter of the Memo is triggered instead. As a workaround you can do something like this: 1. Drop a timer to your form, set interval to 50 ms 2. Declare a private variable FMouseOverForm Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses LCLIntf; procedure TForm1.tmTimer(Sender: TObject);var  P: TPoint;begin  GetCursorPos(P);  P := ScreenToClient(P);  if (PtInRect(ClientRect, P)) and (not FMouseOverForm) then  begin    Caption := 'Mouse enter';    FMouseOverForm := True;  end else if (not PtInRect(ClientRect, P)) and (FMouseOverForm) then  begin    Caption := 'Mouse leave';    FMouseOverForm := False;  end;end; PS: Alternatively you can redirect all OnMouseEnter/OnMouseLeave to a single event. ---------------------------------------------------------------------- Handoko · 2017-05-30 01:57 You solution using TTimer worked. And I understand your explanation about that intended behavior. But you should test it yourself. On an empty form and move your the mouse pointer from inside to outside. It really a bug. 99% it won't be triggered, and only in some random chances it will be triggered correctly. It could be something wrong with my Lazarus installation. But I won't think so because this trivial issue already bugged me from some previous versions. ---------------------------------------------------------------------- sky_khan · 2017-05-30 03:11 On windows (this is what i know), your window(form) wont get any messages when mouse not in your form. Only the window under mouse get windows messages. So when mouse leaves your form you're at dark. As far as I remember it is same in Delphi too. ---------------------------------------------------------------------- Handoko · 2017-05-30 04:03 Sounds reasonable. Because if the mouse pointer is no longer hovering on the window, it won't be monitored. But it doesn't explain why on my test it still able to trigger OnMouseLeave event but only at very low rate. Okay, let me explain what I actually want to do. I want to customize TDrawGrid appearance using OnCellDraw and OnPrepareCanvas. It worked, but I got mouse hovering problem. I want the cells will be automatically highlighted when the mouse hovering on it and the highlight is removed when the mouse pointer if not on the cell. Below is the code, to make it simple I used TStringGrid and removed all unnecessary things: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Forms, Graphics, Grids; type   TForm1 = class(TForm)    StringGrid1: TStringGrid;    procedure FormCreate(Sender: TObject);    procedure StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;      aState: TGridDrawState);  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;  aState: TGridDrawState);begin  if gdHot in aState then    (sender as TStringGrid).Canvas.Brush.Color := clRed;end; procedure TForm1.FormCreate(Sender: TObject);begin  StringGrid1.Options := StringGrid1.Options + [goHeaderHotTracking];end; end. Try to move your mouse pointer to highlight the cells and slowly move it outside. It should work correctly, but try it again with moving your mouse very quickly. The highlight won't be removed. What I thought is perhaps because the mouse pointer movement is too fast, the grid doesn't properly detected the mouse leaving (from grid to the form). So, I think to solve it by adding code on TForm.OnMouseLeave to remove the grid highlight. I also provided the downloadable code (test.zip) for you to test. Does it happen on Linux GTK2 only or also on other systems? ---------------------------------------------------------------------- balazsszekely · 2017-05-30 04:18 @Handoko I tested your demo application. Under windows the cell highlight is always removed no matter how fast I move my mouse. Under Linux(Gtk2) I was able to reproduce the issue, but even there only 1 times out of 10. ---------------------------------------------------------------------- Handoko · 2017-05-30 04:27 Thank you for testing it. So, so far the conclusion is: - On Windows, (visual components') mouse leaving is working as intended - On Linux Gtk2, the mouse leaving is not 100% working Any volunteer help me testing the code, please? ---------------------------------------------------------------------- sky_khan · 2017-05-30 04:30 On Windows 10 I could make it. I tried to move mouse too fast from grid to out of form, one in 8-10 times it stucks. I guess I can find a solution by using winapi but I dont know much about gtk or qt widgetsets. ---------------------------------------------------------------------- Handoko · 2017-05-30 04:38 So, the issue is reproducible. On my tests it almost always happens. Maybe because I use Compiz to add effects on my windows manager. Using winapi to solve the issue on Windows is okay. But we know writing code to cover a bug isn't a best thing to do. The bug is still there. ---------------------------------------------------------------------- sky_khan · 2017-05-30 05:04 Yeah, my solution would be a workaround, not a real fix. You may have to resort to use workarounds if you are under a pressure to ship a software product in real world though ---------------------------------------------------------------------- Handoko · 2017-05-30 05:17 I don't write program for money. I do programming for fun. It is my personal trayicon Memopad. Very useful for programmers, if you ever used SideKick (for DOS) you know what I mean. It's almost finished just some improvements, I will put it on open source after I fix some remaining issues. If you have time, please write the workaround. I use Linux but I believe someone will find it useful. ---------------------------------------------------------------------- Ondrej Pokorny · 2017-05-30 05:55 IIRC, I fixed this in 1.7. Please retest in 1.8 RC1. ---------------------------------------------------------------------- balazsszekely · 2017-05-30 06:06 @Ondrej It's ok in windows with Lazarus trunk. Under linux(Gtk2) the issue is still present. I can reproduce it, if I try hard enough. ---------------------------------------------------------------------- Handoko · 2017-05-30 07:03 Just installed Lazarus 1.8.0RC1 2017-05-15 FPC 3.0.3 x86_64-linux-gtk2 and tested the code. The result is same. If I move fast enough, the issue is always reproducible. ---------------------------------------------------------------------- Handoko · 2017-06-03 08:00 I checked the Lazarus Bugtracker and I found this issue may be related with these: - 0031511: MouseLeave not called ... gtk2, 2017-03-09 https://bugs.freepascal.org/view.php?id=31511 - 0031510: MouseLeave isn't executed ..., 2017-03-08 https://bugs.freepascal.org/view.php?id=31510 - 0022098: MouseLeave not always fired, 2012-05-22 https://bugs.freepascal.org/view.php?id=22098 ---------------------------------------------------------------------- Handoko · 2020-02-02 09:38 Quote from: Ondrej Pokorny on May 30, 2017, 07:55:59 amIIRC, I fixed this in 1.7. Please retest in 1.8 RC1. I retested the issue again today on Lazarus 2.0.6 GTK2 Ubuntu 19.10, it's still not fixed yet. ---------------------------------------------------------------------- jamie · 2020-02-05 01:19 Mouse movements of that nature in windows is a low priority.. a message is sent via the OS but it can get delayed and it can also get trashed. on my system it seems to work ok but on slower systems the mouse may skip outside the form and if the system is busy it won't get sent.  maybe the onActivate and OnDeActivate form is what you can use ---------------------------------------------------------------------- Handoko · 2020-02-05 01:47 Yes, just like you said if I move the move away slowly there will be no problem. Unfortunately users do not always move the mouse slowly.  On activate/deactive may work but not suitable for the case I want to do. ---------------------------------------------------------------------- jamie · 2020-02-05 21:40 I am not exactly sure what it is  you are doing but I can only assume you are jumping between windows and need to know this information ?  What I do when using mouse as a free hand drawing or dragging tool I always record the last mouse position I processed and there for I use a Lineto call for free hand drawing instead of a point draw.  This will guarantee that I do not have any broken lines sections ..  In your case what you should be doing is recording the MouseMove Plots in a global location so that each time a mouse move is received you can first test to see what the last one was. If the last one was in a different form then you can execute the OnMouseEnter yourself in code.  When doing such a practice you always record the current location for the next time you receive the message and thus can test for this..  So basically you need to use the Mouse Move event and do your own Enter and Exit where by you can check the last control it was over. ---------------------------------------------------------------------- winni · 2020-02-05 22:25 Quote from: Handoko on May 29, 2017, 08:57:51 pmMaybe I don't understand how the Mouse Leave works. But I think it has 2 issues: - If we move the mouse pointer to other controls (for example a button), that is still inside the form the OnMouseLeave will be triggered. I think it is wrong because that control is inside the form. Correct me if I'm wrong. - If we move the mouse pointer from inside to the outside of the form, the OnMouseLeave will not be triggered. It is definitely wrong. (Actually, it will be triggered but at very low chances, perhaps 1% only) Any test from others, maybe Windows? Hi! In your first szenario the behaviour is correct. The enter/leave event depends on the z-axis : The highest component is triggerd and get the focus - if possible. OneMouseLeave for a form is not usable. You have to check if it is the Form is active or take the screen coordinates via application.OnMouseMove. If the MouseLeave/MouseEnter inside an app gets lost you can  use the internal logic: If the MouseMove of a component is triggered there must definitly  an Enter happend. If  a MouseMove of anonother neighbour or parent component is triggered, then there must be a mouseleave for the first component. This problem exists since the last millenium. And I never had those problems with Delphi Winni ---------------------------------------------------------------------- Handoko · 2020-11-09 16:19 Hello, I'm back again. Now with more detailed information. Quote from: jamie on February 05, 2020, 10:40:48 pmI am not exactly sure what it is  you are doing ... Look at the picture below. I'm designing a form that lets users to select an item from a list. On the right bottom corner, there is an image 'F1', which means users can press F1 or click on the image. To let user know that image is clickable, I want it to change color (or perhaps change the image to 'click me') when the mouse pointer is hovering on it. So I need to use OnMouseEnter and OnMouseLeave. Unfortunately OnMouseLeave is not 100% triggered. I tested on Lazarus 2.0.10 GTK2 Ubuntu Mate 20.04. Quote from: winni on February 05, 2020, 11:25:29 pmYou have to check if it is the Form is active or take the screen coordinates via application.OnMouseMove. I can't find TApplication.OnMouseMove. Where to find it? Changing image is not simple so I wrote a demo that use a TButton instead. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Graphics, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Shape1: TShape;    procedure Button1MouseEnter(Sender: TObject);    procedure Button1MouseLeave(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1MouseEnter(Sender: TObject);begin  Shape1.Brush.Color := clBlue;end; procedure TForm1.Button1MouseLeave(Sender: TObject);begin  Shape1.Brush.Color := clWhite;end; end. Any solution? Workaround? Or maybe suggestion to use other component? ---------------------------------------------------------------------- winni · 2020-11-09 17:00 Hi Handoko! To call the mouse from the application you have to use the hook called Application.AddOnUserInputHandler. Why ever they hide the word "mouse". A little example is here: https://forum.lazarus.freepascal.org/index.php/topic,22815.msg135328.h tml#msg135328 Winni ---------------------------------------------------------------------- Handoko · 2020-11-09 18:39 I tried you suggestion and wrote a demo using it. Unfortunately the result is the same as using OnMouseLeave. I'm not sure on Windows, but on Linux GTK2 if I move the mouse quickly, it cannot 100% detect mouse outside button positions. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, sysutils, Forms, Controls, Graphics, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    RadioGroup1: TRadioGroup;    Shape1: TShape;    procedure MouseInput(Sender: TObject; Msg: Cardinal);    procedure Button1MouseEnter(Sender: TObject);    procedure Button1MouseLeave(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.MouseInput(Sender: TObject; Msg: Cardinal);var  LeftTop, RightBottom: TPoint;  inRange: Boolean;begin  if not(RadioGroup1.ItemIndex = 0) then Exit;   // Button Position  LeftTop     := Point(Button1.Left, Button1.Top);  RightBottom := Point(Button1.Left+Button1.Width, Button1.Top+Button1.Height);  // Convert to screen coordinate  LeftTop     := ClientToScreen(LeftTop);  RightBottom := ClientToScreen(RightBottom);  // Is mouse position in the range of button?  with Mouse.CursorPos do    inRange := (X > LeftTop.X) and (X < RightBottom.X) and      (Y > LeftTop.Y) and (Y < RightBottom.Y);  // Change color  case inRange of    True:  Shape1.Brush.Color := clGreen;    False: Shape1.Brush.Color := clWhite;  end;end; procedure TForm1.Button1MouseEnter(Sender: TObject);begin  if not(RadioGroup1.ItemIndex = 1) then Exit;  Shape1.Brush.Color := clBlue;end; procedure TForm1.Button1MouseLeave(Sender: TObject);begin  if not(RadioGroup1.ItemIndex = 1) then Exit;  Shape1.Brush.Color := clWhite;end; procedure TForm1.FormCreate(Sender: TObject);begin  Application.AddOnUserInputHandler(@MouseInput, True);  RadioGroup1.ItemIndex := 0;end; end. ---------------------------------------------------------------------- jamie · 2020-11-09 22:39 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. ---------------------------------------------------------------------- winni · 2020-11-09 23:44 Hi Handoko! I tested your code: Everything works fine. With a quick mouse. With a very slow mouse. Everything is ok. Perhaps you moan about the 1 Pixel in X and one Pixel in Y: Always the same  with the TRect: TopLeft is INSIDE the button. BottomRight is OUTSIDE the button. So your code has to be: with Mouse.CursorPos do     inRange := (X >= LeftTop.X) and (X < RightBottom.X) and       (Y >= LeftTop.Y) and (Y < RightBottom.Y); Now the last pixels are found: System: Lin64, fpc 3.04 Lazarus 2.0.10 gtk2 Winni ---------------------------------------------------------------------- jamie · 2020-11-10 00:50 Reason to use the  PtInRect function ---------------------------------------------------------------------- winni · 2020-11-10 01:44 Hi Jamie! Which PtInRect  do you use? Windows?  LCLIntf?  jedi?  types? I swear the are some more. But a simple const likeCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  HexChars=['0'..'9','a'..'f','A'..'F']; is missing. Winni ---------------------------------------------------------------------- winni · 2020-11-10 01:52 Another one: PointInRect  in leakview Winni ---------------------------------------------------------------------- jamie · 2020-11-10 02:00 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. ---------------------------------------------------------------------- Handoko · 2020-11-10 02:06 Quote from: winni on November 10, 2020, 12:44:44 amPerhaps 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. Quote from: jamie on November 09, 2020, 11:39:26 pmuse 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. Quote from: jamie on November 10, 2020, 01:50:56 amReason to use the  PtInRect function  I knew PtInRect. But using PtInRect or not should not cause the issue. Quote from: winni on November 10, 2020, 12:44:44 amI tested your code: Everything works fine. With a quick mouse. Could it be a platform specific issue? I use Ubuntu Mate 20.04. ---------------------------------------------------------------------- winni · 2020-11-10 02:23 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 · 2020-11-10 02:43 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 · 2020-11-10 13:07 Quote from: Handoko on November 10, 2020, 03:43:27 am 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Timer1Timer(Sender: TObject);begin  Caption:= IntToStr(Mouse.CursorPos.x)+' / '+IntToStr(Mouse.CursorPos.y);end;  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 ---------------------------------------------------------------------- Handoko · 2020-11-10 13:59 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.  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. ---------------------------------------------------------------------- Handoko · 2020-12-23 13:11 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Graphics, ExtCtrls, StdCtrls, Messages, LMessages; type   { TForm1 }   TForm1 = class(TForm)    CheckBox1: TCheckBox;    Shape1: TShape;    procedure FormCreate(Sender: TObject);    procedure Shape1MouseEnter(Sender: TObject);    procedure Shape1MouseLeave(Sender: TObject);  private    procedure SysMessage(var Msg: TMessage); message LM_MOUSELEAVE;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Constraints.MinHeight := 240;  Constraints.MinWidth  := 320;  Shape1.Height         := Height - 72;  Shape1.Width          := Width  - 48;  Shape1.Anchors        := [akTop, akBottom, akLeft, akRight];end; procedure TForm1.Shape1MouseEnter(Sender: TObject);begin  Caption := 'OnMouseEnter triggered';  Shape1.Brush.Color := clRed;end; procedure TForm1.Shape1MouseLeave(Sender: TObject);begin  Caption := 'OnMouseLeave triggered';  Shape1.Brush.Color := clWhite;end; procedure TForm1.SysMessage(var Msg: TMessage);begin  if CheckBox1.Checked then    Shape1MouseLeave(Self);end; 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.