devxlogo

A Simple Right Mouse Click

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;

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist