How to manually get TMouseButton in C++Builder
How to manually get TMouseButton in C++Builder
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 = ???
TShiftState Shift
Shift
Form1->sh
if (Form1->sh.Contains(ssLeft)) ...
ssLeft,ssRight,ssMiddle
ssShift,ssAlt,ssCtrl
x,y
OnMouseMove
2 Answers
2
As others have mentioned, you can use the OnMouseDown event to remember the current mouse button state for use in OnClick, eg.
OnMouseDown
OnClick
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:
GetKeyState()
GetAsyncKeyState()
VK_LBUTTON
VK_RBUTTON
void __fastcall TForm::ListBox1Click(TObject *Sender)
if (GetKeyState(VK_LBUTTON)) ...
if (GetKeyState(VK_RBUTTON)) ...
The correct event to use for details of mouse click events is OnMouseDown (also OnMouseUp and OnMouseMove).
OnMouseDown
OnMouseUp
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.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
use evens that has
TShiftState Shift(OnMouseUp/Down/Move) and remember theShiftinto your variableForm1->sh. Then if you need just useif (Form1->sh.Contains(ssLeft)) ...the buttons aressLeft,ssRight,ssMiddlealso some keys are handyssShift,ssAlt,ssCtrlI usually also rememberx,yposition fromOnMouseMoveas I tend to use it a lot for stuff like selection focus switching/freeing etc. see drag & drop C++/VCL example– Spektre
Sep 10 '18 at 7:04