How to emulate a button click ?

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
SIGEAL
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Mar 29, 2005 5:11 pm

How to emulate a button click ?

Post by SIGEAL »

Hi,

I am looking for a way to emulate a button click when the user press a keyboard key (linked to the button by an accelarator table).

Any clue ?

Thanks.

Christophe Damour
ConnorMacLeod
Knows some wx things
Knows some wx things
Posts: 39
Joined: Thu Nov 25, 2004 10:10 am
Location: Germany

Post by ConnorMacLeod »

Just put a call of the button function (OnButtonClick()) in the event handling method of the key.
[wxMSW 2.6.1/VC7.1 .NET 2003/WinXP]
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

You can emulate all event with create one (a wxMouseEvent for exemple). You must just create an instance of the event, correctly initialize it and process it with wxEvtHandler::ProcessEvent (wxWindow and derivates are wxEvtHandler-based).
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

My solution (which is already given is this);

Code: Select all

wxMyApp::OnSomeButtonEvent(wxCommandEvent &event)
{
    DoWhateverYouWant();
}

wxMyApp::DoWhateverYouWant()
{

}

// simply call DoWhateverYouWant directly, and do not fake the events, it is
// more clean this way, less code overhead also
= Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
SIGEAL
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Mar 29, 2005 5:11 pm

Post by SIGEAL »

Thanks for your answers, I was not precise enough.

What I mean with "emulate button click" : when the user press the keyboard key, I would like the corresponding button in the dialog to behave like the key. That is to say button up when the key is up, button down when the key is down, button released when the key is released.

With the following lines, I handle the events correctly :

wxAcceleratorEntry entries[1];
entries[0].Set(wxACCEL_NORMAL, (int) 'A', ID_WXBTNA);

But I would like to synchronize the button and the key behaviour.

Christophe Damour
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

The only way is to emulate the mouse down / mouse up clicks I think when you are receiving a key up and key down event you want from the key .. but wht go through all this trouble I may ask ?

The problem is that your key is sent to the main frame I believe and not to the button. So you have to personally map all the keys to the buttons ..

I see no reason why you would not create a button with e.g. B&utton where the ALT-U becomes the accelerator.

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
SIGEAL
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Mar 29, 2005 5:11 pm

Post by SIGEAL »

Jorg,

What I want to do is not properly "usefull", it's jus a question of look ! When the user press an accelerator key which is binded to a button I would like to see the button being pressed...

btw, I tried your B&utton suggestion, but it did'nt work.

Christophe
Post Reply