0

I am using C++Builder from Embarcadero Technology. The built in OnClick event handler does not identify if the mouse click is the left or right button. Is there a function I can call to manually fill the values for TMouseButton. Below is the OnClick event handler?

void __fastcall TForm::ListBox1Click(TObject *Sender)
{
  TMouseButton Button;

  Button = ???
}
homebase
  • 713
  • 1
  • 6
  • 20
  • 1
    use evens that has `TShiftState Shift` (OnMouseUp/Down/Move) and remember the `Shift` into your variable `Form1->sh` . Then if you need just use `if (Form1->sh.Contains(ssLeft)) ...` the buttons are `ssLeft,ssRight,ssMiddle` also some keys are handy `ssShift,ssAlt,ssCtrl` I usually also remember `x,y` position from `OnMouseMove` as I tend to use it a lot for stuff like selection focus switching/freeing etc. see [drag & drop C++/VCL example](https://stackoverflow.com/questions/16450882/does-anyone-know-of-a-low-level-no-frameworks-example-of-a-drag-drop-re-ord/20924609?s=1|45.1559#20924609) – Spektre Sep 10 '18 at 07:04

2 Answers2

1

The correct event to use for details of mouse click events is OnMouseDown (also OnMouseUp and OnMouseMove).

Override the event and then implement MouseDown event like this

void __fastcall TMyListView::MouseDown(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y)
{
     if (Button == mbLeft){

     }
     if (Button == mbRight){

     }
}

See also Vcl.Controls.TControl.OnMouseDown in Embarcadero's documentation.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
F.Igor
  • 4,119
  • 1
  • 18
  • 26
1

As others have mentioned, you can use the OnMouseDown event to remember the current mouse button state for use in OnClick, eg.

private:
    bool LButtonDown;
    bool RButtonDown;

...

void __fastcall TForm1::ListBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    switch (Button) {
        case mbLeft:
            LButtonDown = true;
            break;

        case mbRight:
            RButtonDown = true;
            break;
    }
}

void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    switch (Button) {
        case mbLeft:
            LButtonDown = false;
            break;

        case mbRight:
            RButtonDown = false;
            break;
    }
}

void __fastcall TForm::ListBox1Click(TObject *Sender)
{
    if (LButtonDown) ...
    if (RButtonDown) ...
}

If you don't want to do that way, you can use the Win32 API GetKeyState() or GetAsyncKeyState() function to query the current state of the mouse's left and right buttons, using the VK_LBUTTON and VK_RBUTTON virtual key codes, eg:

void __fastcall TForm::ListBox1Click(TObject *Sender)
{
    if (GetKeyState(VK_LBUTTON)) ...
    if (GetKeyState(VK_RBUTTON)) ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770