Recent

Author Topic: MouseUp event handler does not detect the pressed mouse button  (Read 645 times)

simsee

  • Full Member
  • ***
  • Posts: 181
MouseUp event handler does not detect the pressed mouse button
« on: November 20, 2023, 03:30:50 pm »
As can be deduced from the following code, while the MouseDown event handler detects the pressed mouse button and places it in the Shift set, the same thing does not happen with MouseUp. Is this correct and expected behavior?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  16.     procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  17.   private
  18.     procedure WriteShift(Shift: TShiftState);
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.WriteShift(Shift: TShiftState);
  33. var
  34.   ShiftStateEnum : TShiftStateEnum;
  35. begin
  36.   for ShiftStateEnum in TShiftStateEnum do
  37.     if ShiftStateEnum in Shift then
  38.       writeln(ShiftStateEnum);
  39.   writeln;
  40. end;
  41.  
  42. procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  43. begin
  44.   writeln('FormMouseDown');
  45.   WriteShift(Shift);
  46. end;
  47.  
  48. procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  49. begin
  50.   writeln('FormMouseUp');
  51.   WriteShift(Shift);
  52. end;
  53.  
  54. end.

wp

  • Hero Member
  • *****
  • Posts: 11760
Re: MouseUp event handler does not detect the pressed mouse button
« Reply #1 on: November 20, 2023, 03:45:11 pm »
This is the same as in Delphi. The logics behind it probably is: the ShiftState represents the set of the pressed buttons. When the left button is pressed it is included in the Shift set because it is down. When it is released it is no longer held down and therefore removed from Shift. But note: You can always get the button which is pressed or released from the Button argument of the event handler.

simsee

  • Full Member
  • ***
  • Posts: 181
Re: MouseUp event handler does not detect the pressed mouse button
« Reply #2 on: November 20, 2023, 03:49:28 pm »
Thanks WP. So the State parameter in MouseUP carries no information and is therefore useless.

wp

  • Hero Member
  • *****
  • Posts: 11760
Re: MouseUp event handler does not detect the pressed mouse button
« Reply #3 on: November 20, 2023, 03:57:44 pm »
No. You still can have the CTRL, SHIFT, ALT keys down... But yes, it is useless regarding the mouse button.

Josh

  • Hero Member
  • *****
  • Posts: 1254
Re: MouseUp event handler does not detect the pressed mouse button
« Reply #4 on: November 20, 2023, 03:59:46 pm »
placing 2 labels on form, then using code below, the shift is detected in mouse up and mousedown...


Code: [Select]
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if ssShift in Shift then label1.caption:='MD: Shift' else label1.caption:='MD:';
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  If ssShift in Shift then label2.caption:='MU: Shift' else label2.caption:='MU';
end;         
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

jamie

  • Hero Member
  • *****
  • Posts: 6022
Re: MouseUp event handler does not detect the pressed mouse button
« Reply #5 on: November 21, 2023, 01:04:32 am »
Works for me.

Hold the LEFT mouse button down and click the right mouse button.
The ssLEFT is reported, because that is the shift state.

Same the other way, hold the Right mouse button down and click the left mouse button.

That reports ssRight mouse button.

So this means the buttons on the mouse that are being held down while another mouse button is clicked reports the mouse button being held down.

 If you want to know the actual clicked mouse button, use the BUTTON parameter and from there, use a case statement to check the shift states.

 
Code: Pascal  [Select][+][-]
  1. Case Butotn of
  2.   mbMiddle:If ssLeft in state Then... else if ssright in State then ...
  3.  
  4.  

same for the ither mouse buttons.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018