Cannot get mouse state with wxGetMouseState() Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Cannot get mouse state with wxGetMouseState()

Post by softport »

Hello, I am just testing the wxGetMouseState() function, but can't get it to
work. When WxButton1is pressed, and the left mouse button is kept down,
the 'while' loop executes only once.

Thanks for any input.

Code: Select all

void MouseFrm::WxButton1Click(wxCommandEvent& event)
{
	bool mouseButtonDown = TRUE;
	int n = 1;
	wxMouseState  mouse_state;

        while(mouseButtonDown == TRUE){
            WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
            n++;
            mouse_state = wxGetMouseState();
            mouseButtonDown = mouse_state.LeftIsDown();
        }
}
wxGetMouseState()
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cannot get mouse state with wxGetMouseState()

Post by doublemax »

A buttonclick is actually triggered on mouse-up, so the mouse button is never down when it enters the event handler.
Use the source, Luke!
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: Cannot get mouse state with wxGetMouseState()

Post by softport »

Got it. Thanks very much!
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: Cannot get mouse state with wxGetMouseState()

Post by softport »

Here's one more :).

It almost works if I use a spin button. When I release the spin-up, the display
shows the number of times that the loop executed. But why is it acting this way:

1. The spin button does not depress when you click on it. If I comment out the line
"mouse_state = wxGetMouseState();", the spin button goes down as normal.
2. The display is not updated inside the loop. The total number of runs through the loop
is only updated when I release the mouse button.

What I would like to have is an event function that:
1. Stays inside the loop while the mouse key is down on the button (spin button seems to do this)
2. Keeps incrementing and displaying a value as you keep the button pressed
3. The size of the increment gets larger as you keep the button pressed longer

Is this possible at the novice level?

Code: Select all

void MouseFrm::WxSpinButton1Up(wxSpinEvent& event)
{
    bool mouseButtonDown = TRUE;
    int n = 1;
    wxMouseState  mouse_state;

    while(mouseButtonDown == TRUE){

        WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
        n++;

        mouse_state = wxGetMouseState();
        mouseButtonDown = mouse_state.LeftIsDown();
        WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
    }
}
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Cannot get mouse state with wxGetMouseState()

Post by doublemax »

Loops like that where you prevent the normal event process from happening, are usually bad.

If you want to stay with your approach, you'd have to call wxApp::Yield() inside the loop, to that the event loop has a chance to redraw the control (this creates reentrancy problems though). Additionally, call event.Skip() at the end, so that the default behavior for the mouse-up event can take place.

A "clean" solution would look like this:
- in the mouse down handler, call wxWindow::CaptureMouse() and start a timer.
- in the mouse up handler, stop the timer and call wxWindow::ReleaseMouse()
- in the timer event handler, increase variables and redraw controls.
Use the source, Luke!
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: Cannot get mouse state with wxGetMouseState()

Post by softport »

Dude!! (..and I don't use that word often!)

The "clean" solution I will have to dig into and study, but...
The wx help mentioned wxSafeYield to avoid some of the problems you
mentioned about wxYield.

I just added wxSafeYield() at the bottom of the loop, and it works great. Should I
still try to avoid even the wxSafeYield()?

Thanks very much, I was nowhere near finding this function on my own.

Code: Select all

void MouseFrm::WxSpinButton1Up(wxSpinEvent& event)
{
	bool mouseButtonDown = TRUE;
	int n = 1;
	wxMouseState  mouse_state;

    while(mouseButtonDown == TRUE){

        WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
        n++;

        mouse_state = wxGetMouseState();
        mouseButtonDown = mouse_state.LeftIsDown();
        WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
        wxSafeYield();
    }
}
Edit...
It still had one quirk left: it stopped updating the display after about half a second.
I added CaptureMouse() and ReleaseMouse and it fixed this last problem. Also added
a 50ms Sleep function so it doesn't test the mouse continuosly (it was using a lot of
cpu time).

Here is what I have so far:

Code: Select all

void MouseFrm::WxSpinButton1Up(wxSpinEvent& event)
{
    bool mouseButtonDown = TRUE;
    int n = 1;
    wxMouseState  mouse_state;

    CaptureMouse();

    while(mouseButtonDown == TRUE){

        WxMemo1->SetValue(wxString::Format(wxT("%d"),n));
        n++;
        Sleep(50);

        mouse_state = wxGetMouseState();
        mouseButtonDown = mouse_state.LeftIsDown();

        if(mouseButtonDown == TRUE){
            wxSafeYield();
        }
    }
    ReleaseMouse();
}
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW
Post Reply