How to bind mouse events to a wxbutton? 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.
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

How to bind mouse events to a wxbutton?

Post by drewwent »

I have been trying to make a button mimic push to talk functionality on an open source app that uses wxwidgets. The button is question is just a standard wxButton. Declared in the event table as EVT_BUTTON(XRCID("PTT"), InPanel::OnPTT). The ONPTT method currently functions as a toggle, unmuting the mic when pressed and muting the mic when pressed again. But, I would like it to unmute the mic when it is held, and mute the mic when it is released. I have two working handlers for the mouse events EVT_LEFT_DOWN(downptt) and EVT_LEFT_UP(upptt). Is there a way to trigger these when the ONPTT button is pressed(onptt) and released(upptt)? To clarify both of my mouse event handlers work as expected, just not when the button is pressed. Any help would be appreciated.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to bind mouse events to a wxbutton?

Post by doublemax »

To clarify both of my mouse event handlers work as expected, just not when the button is pressed.
I don't quite understand what the problem is. What doesn't work?
Use the source, Luke!
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

Re: How to bind mouse events to a wxbutton?

Post by drewwent »

doublemax wrote:
To clarify both of my mouse event handlers work as expected, just not when the button is pressed.
I don't quite understand what the problem is. What doesn't work?
I have a wxbutton, and two handlers for mouse up and mouse down. I would like the button(PTT) to have the combined functionality of those two events so it behaves kind of like a push to talk button instead of its current functionality where it just toggles something when the mouse is released. Is this possible?
Last edited by drewwent on Mon Jun 19, 2017 8:09 pm, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bind mouse events to a wxbutton?

Post by ONEEYEMAN »

Hi,
Did you actually tried it? Did you have any issues with it?

Thank you.
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

Re: How to bind mouse events to a wxbutton?

Post by drewwent »

ONEEYEMAN wrote:Hi,
Did you actually tried it? Did you have any issues with it?

Thank you.
Yes the mouse handlers only work when the panel is clicked and the mouse button is held, not the button.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bind mouse events to a wxbutton?

Post by ONEEYEMAN »

Hi,
Can you post the code you used?

Thank you.
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

Re: How to bind mouse events to a wxbutton?

Post by drewwent »

Code: Select all

BEGIN_EVENT_TABLE(InCallPanel,CallPanelBase)
EVT_BUTTON(XRCID("PTT"), InCallPanel::OnPTT)
EVT_LEFT_DOWN(InCallPanel::DownPTT)
EVT_LEFT_UP(InCallPanel::UpPTT)
....
void InCallPanel::OnPTT(wxCommandEvent & /*event*/)
...
	
void InCallPanel::DownPTT(WxMouseEvent& /*event*/)
	unmute mic
	
void InCallPanel::UpPTT(WxMouseEvent & /*event*/)
	mute mic
	
That's basically it with the non important stuff as pseudo code.
Last edited by drewwent on Tue Jun 20, 2017 5:36 pm, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bind mouse events to a wxbutton?

Post by ONEEYEMAN »

Hi,
Did you call an "event.Skip()" at the end of the handlers? If not - can you try with it?
Also, it would be easier if you use Bind() for the button and not the parent class. You only interested in the button clicks, right?

Thank you.
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

Re: How to bind mouse events to a wxbutton?

Post by drewwent »

ONEEYEMAN wrote:Hi,
Did you call an "event.Skip()" at the end of the handlers? If not - can you try with it?
Also, it would be easier if you use Bind() for the button and not the parent class. You only interested in the button clicks, right?

Thank you.
Yes, how would I use bind for the button? I'm confused there.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to bind mouse events to a wxbutton?

Post by doublemax »

Unlike EVT_BUTTON which is a wxCommandEvent, EVT_LEFT_DOWN and EVT_LEFT_UP don't propagate upwards in the windows hierarchy. So you need to catch them at the button, you can't catch them in their parent, the panel.

// assuming "button" is a (wxButton *) pointer to the button in question:

Code: Select all

button->Bind( wxEVT_LEFT_DOWN, &InCallPanel::DownPTT, this );
button->Bind( wxEVT_LEFT_UP, &InCallPanel::UpPTT, this );
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bind mouse events to a wxbutton?

Post by ONEEYEMAN »

doublemax,
It looks like he is using XRC. Unfortunately I don't know how to make the code you posted work with XRC.
Can you fix it?

Thx.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to bind mouse events to a wxbutton?

Post by doublemax »

Unfortunately I don't know how to make the code you posted work with XRC.
Unfortunately i don't know the "official" way to get the pointer to a control with XRC either.

What might work, even if ugly:

Code: Select all

wxWindow *button = wxWindow::FindWindowById( XRCID("PTT") );
// as we only want to call Bind(), a (wxWindow *) is enough.
BTW: You may also want to catch wxEVT_MOUSE_LEAVE event, because if the user moves the mouse outside the button while the mouse button is pressed, you won't receive the wxEVT_LEFT_UP event when he releases the mouse button.
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: How to bind mouse events to a wxbutton?

Post by coderrc »

ONEEYEMAN wrote:Unfortunately I don't know how to make the code you posted work with XRC.
it depends on if you need the button for other things or not.
if you dont need it for anything else, then this will suffice

Code: Select all

FindWindow(XRCID("PTT"))->Bind(wxEVT_LEFT_DOWN, &InCallPanel::DownPTT, this);
if you intend to use the button later then something like this might be what youre after

Code: Select all

wxButton* btnPTT = XRCCTRL(*this, "PTT", wxButton);
btnPTT->Bind(wxEVT_LEFT_DOWN, &InCallPanel::DownPTT, this);
drewwent
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Jun 08, 2017 3:28 pm

Re: How to bind mouse events to a wxbutton?

Post by drewwent »

doublemax wrote:
Unfortunately I don't know how to make the code you posted work with XRC.
Unfortunately i don't know the "official" way to get the pointer to a control with XRC either.

What might work, even if ugly:

Code: Select all

wxWindow *button = wxWindow::FindWindowById( XRCID("PTT") );
// as we only want to call Bind(), a (wxWindow *) is enough.
BTW: You may also want to catch wxEVT_MOUSE_LEAVE event, because if the user moves the mouse outside the button while the mouse button is pressed, you won't receive the wxEVT_LEFT_UP event when he releases the mouse button.
I gave that a shot and now I'm getting "wxWindow" has no member Bind.
Edit: Never mind, the older version of wxwidgets I had to install actually has no Bind whatsoever. So I am going to just use a toggle button instead I think. Thanks for all the help though guys
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to bind mouse events to a wxbutton?

Post by doublemax »

In wx 2.8.12 you can use Connect() instead of Bind(), the syntax is just slightly different.

(Using the code from coderrc):

Code: Select all

wxButton* btnPTT = XRCCTRL(*this, "PTT", wxButton);
btnPTT->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(InCallPanel::DownPTT), NULL, this);
(Untested, typed from memory).
Use the source, Luke!
Post Reply