2006. január 24., kedd

Detecting simultaneous left and right mouse clicks


Problem/Question/Abstract:

How to known if the user has pressed simultaneously the left and right mouse buttons?

Answer:

The OnMouse event is declared as follows:

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

If you find in the Classes unit the declaration of TShiftState is:

TShiftState = set of (ssShift, ssAlt, ssCtrl,
  ssLeft, ssRight, ssMiddle, ssDouble);

So you need to test if sLeft and ssRight are present in the Shift parameter, now your code must be like this:

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (ssRight in Shift) and (ssLeft in Shift) then
    ShowMessage('The user has pressed Left and Right buttons');
end;

Nincsenek megjegyzések:

Megjegyzés küldése