A Simple Right Mouse Click

Question:
I am learning Delphi 4. I’m trying to create a program that can distinguish between a right and left mouse click and perform an operation based on which button has been pressed. The tutorial I’m reading uses the TMouseButton parameter inside the procedure. It looks like this:

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

But this doesn’t distinguish between left and right buttons. How do I make this happen?

Answer:
Sure it does: if you read the value of TMouseButton within the method, it’ll be one of the following three values: mbLeft, mbRight, or mbMiddle.

Here’s some sample code that’ll do the trick:

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  case Button of    mbLeft : ShowMessage('You pressed the left button');    mbRight: ShowMessage('You pressed the right button');    mbMiddle:ShowMessage('You pressed the middle button');  end;end;

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: