How to bind mouse events to a wxbutton? Topic is solved
How to bind mouse events to a wxbutton?
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.
Re: How to bind mouse events to a wxbutton?
I don't quite understand what the problem is. What doesn't work?To clarify both of my mouse event handlers work as expected, just not when the button is pressed.
Use the source, Luke!
Re: How to bind mouse events to a wxbutton?
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?doublemax wrote:I don't quite understand what the problem is. What doesn't work?To clarify both of my mouse event handlers work as expected, just not when the button is pressed.
Last edited by drewwent on Mon Jun 19, 2017 8:09 pm, edited 1 time in total.
Re: How to bind mouse events to a wxbutton?
Hi,
Did you actually tried it? Did you have any issues with it?
Thank you.
Did you actually tried it? Did you have any issues with it?
Thank you.
Re: How to bind mouse events to a wxbutton?
Yes the mouse handlers only work when the panel is clicked and the mouse button is held, not the button.ONEEYEMAN wrote:Hi,
Did you actually tried it? Did you have any issues with it?
Thank you.
Re: How to bind mouse events to a wxbutton?
Hi,
Can you post the code you used?
Thank you.
Can you post the code you used?
Thank you.
Re: How to bind mouse events to a wxbutton?
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
Last edited by drewwent on Tue Jun 20, 2017 5:36 pm, edited 1 time in total.
Re: How to bind mouse events to a wxbutton?
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.
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.
Re: How to bind mouse events to a wxbutton?
Yes, how would I use bind for the button? I'm confused there.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.
Re: How to bind mouse events to a wxbutton?
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:
// 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!
Re: How to bind mouse events to a wxbutton?
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.
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.
Re: How to bind mouse events to a wxbutton?
Unfortunately i don't know the "official" way to get the pointer to a control with XRC either.Unfortunately I don't know how to make the code you posted work with XRC.
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.
Use the source, Luke!
Re: How to bind mouse events to a wxbutton?
it depends on if you need the button for other things or not.ONEEYEMAN wrote:Unfortunately I don't know how to make the code you posted work with XRC.
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);
Code: Select all
wxButton* btnPTT = XRCCTRL(*this, "PTT", wxButton);
btnPTT->Bind(wxEVT_LEFT_DOWN, &InCallPanel::DownPTT, this);
Re: How to bind mouse events to a wxbutton?
I gave that a shot and now I'm getting "wxWindow" has no member Bind.doublemax wrote:Unfortunately i don't know the "official" way to get the pointer to a control with XRC either.Unfortunately I don't know how to make the code you posted work with XRC.
What might work, even if ugly: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.Code: Select all
wxWindow *button = wxWindow::FindWindowById( XRCID("PTT") ); // as we only want to call Bind(), a (wxWindow *) is enough.
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
Re: How to bind mouse events to a wxbutton?
In wx 2.8.12 you can use Connect() instead of Bind(), the syntax is just slightly different.
(Using the code from coderrc):(Untested, typed from memory).
(Using the code from coderrc):
Code: Select all
wxButton* btnPTT = XRCCTRL(*this, "PTT", wxButton);
btnPTT->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(InCallPanel::DownPTT), NULL, this);
Use the source, Luke!