Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-04

TMouseButton

12 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kjow

Hi all,

I need to create an "advanced" function that manages mouse events, so I need to know when/which a button is pressed and so, when no button is pressed.
I saw that in TMouseButton (row 142 of Controls unit) there isn't mbNone, so I simply edited that row:

TMouseButton = (mbNone, mbLeft, mbRight, mbMiddle, mbExtra1, mbExtra2);

Now I can see when no buttons are pressed; I simply manage it with

MouseButtonPressed:=Button; //OnMouseDown
MouseButtonPressed:=mbNone; //OnMouseUp

Now the question: there is a better way to do this whitout modify the Lazarus source code? It is a bug/missing feature or this is a feature?

Regards,
Kjow

Reply by email

Vincent Snijders
to Kjow
Kjow wrote 19 lines
Hi all, I need to create an "advanced" function that manages mouse events, so I need to know when/which a button is pressed and so, when no button is pressed. I saw that in TMouseButton (row 142 of Controls unit) there isn't mbNone, so I simply edited that row: TMouseButton = (mbNone, mbLeft, mbRight, mbMiddle, mbExtra1, mbExtra2); Now I can see when no buttons are pressed; I simply manage it with MouseButtonPressed:=Button; //OnMouseDown MouseButtonPressed:=mbNone; //OnMouseUp Now the question: there is a better way to do this whitout modify the Lazarus source code? It is a bug/missing feature or this is a feature?

What about:

type
   TMouseButtons = set of TMouseButton; // if it doesn't exist yet
var
   MouseButtonPressed: TMouseButtons;
MouseButtonPressed:=MouseButtonPressed + Button; //OnMouseDown

Your mbNone becomes:

MouseButtonPressed:=[];

Vincent

Reply by email

Martin
to Kjow: I need to create an "advanced" function that manages mouse events, so I need to know…
Kjow wrote 19 lines
Hi all, I need to create an "advanced" function that manages mouse events, so I need to know when/which a button is pressed and so, when no button is pressed. I saw that in TMouseButton (row 142 of Controls unit) there isn't mbNone, so I simply edited that row: TMouseButton = (mbNone, mbLeft, mbRight, mbMiddle, mbExtra1, mbExtra2); Now I can see when no buttons are pressed; I simply manage it with MouseButtonPressed:=Button; //OnMouseDown MouseButtonPressed:=mbNone; //OnMouseUp Now the question: there is a better way to do this whitout modify the Lazarus source code? It is a bug/missing feature or this is a feature?
I wouldn't recommend that change. Some units define
   const  Foo = Array [TMouseButton] of String = (list of names for values);

Those will break, because the count of the enum changed.

Create a set

type
   TMouseButtons = set of  TMouseButton ;
var
   MouseButtonPressed: TMouseButtons;
MouseButtonPressed := [];
MouseButtonPressed := [mbLeft];
MouseButtonPressed := [mbLeft, mbRight];

and so on

Reply by email

Paul Ishenin
to Kjow: I need to create an "advanced" function that manages mouse events, so I need to know…
Kjow wrote 7 lines
I saw that in TMouseButton (row 142 of Controls unit) there isn't mbNone, so I simply edited that row: TMouseButton = (mbNone, mbLeft, mbRight, mbMiddle, mbExtra1, mbExtra2); Now I can see when no buttons are pressed; I simply manage it with

You miss that it is possible to press 2 buttons at the moment. That's why LCL uses a set to define what is pressed and what is not.

As result you don't need mbNone. Just use [] to indicate that no button is pressed.

Best regards,
Paul Ishenin.

Reply by email

Kjow
to Martin: I wouldn't recommend that change. Some units define const Foo = Array [TMouseButton] of…

2010/4/7 Paul Ishenin :

As result you don't need mbNone. Just use [] to indicate that no button is pressed.

2010/4/7 Martin :

Show quoted text 17 lines
I wouldn't recommend that change. Some units define ?const ?Foo = Array [TMouseButton] of String = (list of names for values); Those will break, because the count of the enum changed. Create a set type ?TMouseButtons = set of ?TMouseButton ; var ?MouseButtonPressed: TMouseButtons; MouseButtonPressed := []; MouseButtonPressed := [mbLeft]; MouseButtonPressed := [mbLeft, mbRight]; and so on

Oh! Thank you both very much!

I searched for this, but nothing found!

:-)

Best regards,
Kjow

Reply by email

Kjow
to Kjow

2010/4/7 Kjow :

Show quoted text 5 lines
> As result you don't need mbNone. Just use [] to indicate that no button is > pressed. 2010/4/7 Martin : > Create a set

Other question... how to pass/read [] "vaule" to/in a function?

e.g.

function ButtonPress(Button: TMouseButton): string;
begin
  case Button of
   []: Result:='Nothing Pressed'; //e.g.
   mbLeft: Result:='mbLeft';
   mbRight: Result:='mbRight';
   mbMiddle: Result:='mbMiddle';
  end;
end;

and somewhere:

showmessage(ButtonPress([])); // [] -> e.g.

Thanks,
Kjow

Reply by email

Kjow
to Vincent Snijders: What about: type TMouseButtons = set of TMouseButton; // if it doesn't exist yet var…

2010/4/7 Vincent Snijders :

Show quoted text 15 lines
What about: type ?TMouseButtons = set of TMouseButton; // if it doesn't exist yet var ?MouseButtonPressed: TMouseButtons; > MouseButtonPressed:=MouseButtonPressed + Button; //OnMouseDown Your mbNone becomes: > MouseButtonPressed:=[]; Vincent

Thank you Vincent,

I tried, but

MouseButtonPressed:=[];

returns:

Error: Incompatible types: got "Empty Set" expected "TMouseButton"

and I don't know how to add Button to MouseButtonPressed...

Thanks,
Kjow

Reply by email

Kjow
to Kjow

2010/4/7 Kjow :

Error: Incompatible types: got "Empty Set" expected "TMouseButton"

Whops, I've mistyped, it works!
But how to read the values from a function?

e.g.

function ButtonPress(Button: TMouseButtons): string;
begin
  case Button of
    ...: Result:= 'you pressed' ;
  end;
end;

regards,
Kjow

Reply by email

Kjow
to Kjow

2010/4/7 Kjow :

Show quoted text 14 lines
2010/4/7 Kjow : > Error: Incompatible types: got "Empty Set" expected "TMouseButton" Whops, I've mistyped, it works! But how to read the values from a function? e.g. function ButtonPress(Button: TMouseButtons): string; begin ?case Button of ? ?...: Result:= 'you pressed' ; ?end; end;

Ok, today is not my day :)
I'm sorry for this flooding :(

with

if mbLeft in Button then Result:='left';

I can manage the buttons, but what for nothing?

e.g.

 if [] in Button then Result:='nothing';

Thanks,
Kjow

Reply by email

Martin
to Kjow: 2010/4/7 Kjow : Whops, I've mistyped, it works! But how to read the values from a…
Kjow wrote 16 lines
2010/4/7 Kjow: > Error: Incompatible types: got "Empty Set" expected "TMouseButton" > Whops, I've mistyped, it works! But how to read the values from a function? e.g. function ButtonPress(Button: TMouseButtons): string; begin case Button of ...: Result:= 'you pressed' ; end; end;

Case will not work.

but
mbleft, or mbLeft + any other button

   if mbLeft in Buttons then xxx;
for only mbLeft, no other button:
   if Buttons = [mbLeft] then xxx;
to add / remove ...
Buttons := Buttons + mbLeft;
Include(Buttons, mbLeft);
Buttons := Buttons - mbLeft;
Exclude(Buttons, mbLeft);

Reply by email

Kjow
to Martin

2010/4/7 Martin :

Show quoted text 14 lines
Case will not work. but mbleft, or mbLeft + any other button ?if mbLeft in Buttons then xxx; for only mbLeft, no other button: ?if Buttons = [mbLeft] then xxx; to add / remove ... Buttons := Buttons + mbLeft; Include(Buttons, mbLeft); Buttons := Buttons - mbLeft; Exclude(Buttons, mbLeft);

Thank you!

Now I understood that for 'nothing' I need to do:

if Button=[] then Result:='nothing';

And if I want a combination I need to provide any case

e.g. if Buttons = [mbLeft,mbRight] then xxx

Thank you very much to all,
you are great :)

Best Regards,
Kjow

Reply by email

Mattias Gärtner
to Kjow: 2010/4/7 Kjow : Whops, I've mistyped, it works! But how to read the values from a…

Zitat von Kjow :

Show quoted text 14 lines
2010/4/7 Kjow : > Error: Incompatible types: got "Empty Set" expected "TMouseButton" Whops, I've mistyped, it works! But how to read the values from a function? e.g. function ButtonPress(Button: TMouseButtons): string; begin case Button of ...: Result:= 'you pressed' ; end; end;

The mouse buttons are combined in TShiftState.
For example: OnMouseDown gets the Shift parameter, which contains the pressed mouse buttons and the pressed modifier keys. There is already a function to convert TShiftState into a string in unit LCLProc:

procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
   Shift: TShiftState; X, Y: Integer);
begin
   writeln('TMainForm.FormMouseDown ',dbgs(Shift));
   // when only the right mouse button is pressed
   // it will write: [ssRight]
end;

Mattias

Reply by email