Register single mouse click (C++) - c++

I have two if statements that both respond to my computer mouse.
if (GetAsyncKeyState(VK_LBUTTON))
{
//Do something
}
//More code in between
if (GetAsyncKeyState(VK_LBUTTON))
{
//Do something else
}
However, when I click the left mouse button once it is registered for both if statements. "GetAsyncKeyState" seems to return true as long as the mouse button is down (which it inevitably will be for the second if statement), so what should I use instead so that I need to actively click the mouse button a second time in order for the second statement to take effect?

Related

How to track mouse dragging?

What event do I need to write in my wxWidgets program so that I can track mouse dragging.
I mean hold down the left mouse button and track the movement while it is pressed.
Perhaps surprisingly, this is not such a simple task. You may look at the implementation of wxMouseEventsManager to see an example of working code doing it, but the main point is that you need to capture the mouse on button press, in order to follow its movement even if it exits the window, and then you need to also react to wxEVT_MOUSE_CAPTURE_LOST events to know when the capture is forcibly broken.
Bind(wxEVT_MOTION, [&](wxMouseEvent& event) {
if (event.Dragging()) {
if (event.LeftIsDown()) {
// code
}
}
});

catch mouse motion in gtkmm

I am trying to catch the mouse motion when I hold the mouse middle button. The goal is to implement a rotation feature in an stl viewer.
I found the event mask BUTTON2_MOTION_MASK. But I have a hard time figuring out which signal catches it.
Here's the two line I use to create and hook the event. These two line are inside a GtkApplicationWindow Constructor.
glWidget.add_events(Gdk::BUTTON2_MOTION_MASK);
glWidget.signal_motion_notify_event().connect(sigc::mem_fun(*this,&mainWindow::rotate));
Here's the function I am trying to connect.
bool mainWindow::rotate(GdkEventMotion* motion_event)
{
cout<<"test"<<endl;
}
Am I using the correct method? The code does not react when I hold the middle mouse button and move mouse.
I managed to get glArea widget to react to scrolling this way.
glWidget.add_events(Gdk::SMOOTH_SCROLL_MASK);
glWidget.signal_scroll_event().connect(sigc::mem_fun(*this,&mainWindow::zoom));
the function I connected:
bool mainWindow::zoom(GdkEventScroll *eventScroll)
{
cout<<"test"<<endl;
return true;
}
I figured it out. You need to both add the Gdk::Button1_MOTION_MASK and the Gdk::BUTTON_PRESS_MASK.
glWidget.add_events(Gdk::Button1_MOTION_MASK | Gdk::BUTTON_PRESS_MASK);
This will catch the signal when the left mouse button is clicked and positioned on the widget.
BUTTON2_MOTION_MASK will require that 2 button are pressed. For some reason, it's only the left mouse button(I want the middle button).

C++ Win32 Trying to create an owner drawn toggle button

I have been trying for a very long time (well a few days) to create a toggle button. A button having a up or down state.
Took over a day to realize it is not possible to create an owner drawn toggle button, a checkbox and pushlike does not work. When using owner drawn there is no difference between a checkbox or regular button (MSDN also notes you cant use owner drawn with any of those styles.)
From reading I found out you have to do it yourself, normally not a problem at all, but I cannot get any real "responsiveness." That is, if I click fast, nothing happens, sometimes I will click and it changes states, other times not and only updates when I click a different button.
I created a global variable for if the button should be shown in up or down state. In the commands I have it set that when the button, IDC_BTN_TOGGLE, it will set the opposite value on the bool.
Then the draw item part:
// button down
if ((pDIS->itemState & ODS_SELECTED) || showButtonDown) {
oldBrush = (HBRUSH)SelectObject(pDIS->hDC, theme.hBrush[BRUSH_BUTTON2]);
}
// button up
else {
oldBrush = (HBRUSH)SelectObject(pDIS->hDC, theme.hBrush[BRUSH_BUTTON]);
}
All my buttons are owner drawn and run through this. showButtonDown is only true when it is drawing IDC_BTN_TOGGLE and the top bool is true as well.
The normal buttons function normally, when I click them, it instantly shows the down state, release, back to normal, the toggle button is barely responsive.

C++ CLI GUI Event Handeling

I am working on a c++ CLI application and am having some difficulty with events. I am wondering if I can get events to fire while the mouse button is clicked. For example, I am wanting to check whether or not the mouse has moved to the next square over only if they have the mouse clicked in. Meaning if they click on square 1 they should be able to hold that click and move the square 2 and my program recognize this.
I have run a number of different events on the mouse, including the "Click" event, but the neither the hover, mouse enter, or mouse down event get triggered while the button is pressed. The "MouseClick" event, which does the same. I tried using just the mouseDown event, but this does not let another mouseDown event, mouse enter, or hover event fire.
Short of checking mouse position I do not know what I can do. I would like to not have to do mouse position checking.
If anyone has any ideas, they would be greatly appreciated.
Clearly you'll want to pay attention to the MouseMove event so you can see the mouse moving into another square. Roughly:
void panel1_MouseMove(Object^ sender, MouseEventArgs^ e) {
if ((e->Button & System::Windows::Forms::MouseButtons::Left) ==
System::Windows::Forms::MouseButtons::Left) {
int square = MapPosToSquare(e->Location);
if (square != currentSquare) {
currentSquare = square;
OnSquareClicked(currentSquare);
}
}
}
If these "squares" are actually controls then you have a different problem. You have to set the control's Capture property to false in the MouseDown event handler so it doesn't capture the mouse.

Raphael - mouse events

How would you determine what mouse buttons was clicked on a mouse event?
I would like to make the distinction between left and right click.
I am not sure about the click event (though it may be worth a try) but if you use the mousedown event which should be good enough, then you can check the "which" property of the event.
The which property is an integer that determine the mouse button, and the values are mapped like...
1 = Left Button
2 = Middle Button
3 = Right Button
Example code...
element.mousedown(ElementMouseDown);
function ElementMouseDown(e) {
switch(e.which){
case 1://left button
break;
case 2://middle button
break;
case 3://right button
break;
}
}