Send/Generate keyboard events (tab, shift+tab and return)

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.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Send/Generate keyboard events (tab, shift+tab and return)

Post by grf »

Hello,

I'm looking for a solution to navigate/control in a window which has radiobuttons and buttons by using simulated "tab", "shift+tab" and "return" keyboard events.
From "somewhere outside" the user sends some commands (for example via network) and these commands should handled like "tab", "shift+tab" and "return". Therefore I need a function which can generate and send keyboard events to the main frame or actual notebook page.

I tried this:

Code: Select all

wxKeyEvent key_down_event(wxEVT_CHAR);
key_down_event.m_keyCode = WXK_TAB;
wxEvtHandler *handler=this->GetEventHandler();
handler->ProcessEvent(key_down_event);
but nothing happens :(

Also

Code: Select all

wxPostEvent(handler, key_down_event);
doesn't work.

Furthermore I have absolutely no idea how to send "shift+tab" events...

Can anyone give me some hints to do this?

Regards
Michael
Sowas kommt von sowas.
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
under Windows you can use SendInput() SDK function:
http://msdn.microsoft.com/library/defau ... dinput.asp
or keybd_event():
http://msdn.microsoft.com/library/defau ... _event.asp
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

Hi,

Thank you for this hint!
However is there no wxWidgets functionality which can do the same? The program is actual for windows, but I would like to make it as portable as possible...

Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf,
I guess you didn't use a panel as a parent to your control?
Try it, it will do what you want...

Thank you.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

The architecture is as follows:

main panel -> notebook -> notebook panel(s) -> GridBagSizer -> buttons/radiobuttons

Normally I can move the focus from button to button with the Tab key from my keyboard. De-/Activating radiobuttons and buttons can be made with the Return key from my keyboard.
When I try to generate these keyboard events (see my code snipplet) nothing happens. Absolutely nothing... :(

Do you mean, I should use a panel for each button/radiobutton in my GridBag Sizer?

Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf,
How did you create the panels for the notebook?

Tab traversal should be default....


Thank you.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

I created the panels for the notebook with the "AddPanel" function.

EDIT: Ups, I meant with the "AddPage" function:

Code: Select all

main_notebook = new wxNotebook(panel, MAIN_NOTEBOOK_ID);
wxPanel *notebook_panel = new wxPanel(main_notebook, -1);
const wxString page_title = wxT("Title");
main_notebook->AddPage(notebook_panel, page_title);
Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf wrote: The architecture is as follows:

main panel -> notebook -> notebook panel(s) -> GridBagSizer -> buttons/radiobuttons

Normally I can move the focus from button to button with the Tab key from my keyboard. De-/Activating radiobuttons and buttons can be made with the Return key from my keyboard.
First of all what do you mean by (de)activating (radio)buttons? Enabling/Disabling them? Showing/Hiding? (De)Selecting?

Second, you should have a keyboard event handler. Try to step into this function, if it is called, and see what is going on.

Third, where did you put the code you show in the first post? main panel, notebook or book panel?

Thank you.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

1. I mean (de)selecting of radiobuttons. Sorry for my English...

2. Ok, I will try this and see if this event will be generated. However I can try this maybe tomorrow...

3. I put the code in a private function of the frame.

Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf wrote: 1. I mean (de)selecting of radiobuttons. Sorry for my English...
Look at my signature. I'm not native also... :)
grf wrote: 2. Ok, I will try this and see if this event will be generated. However I can try this maybe tomorrow...
Ok, I can wait. However, I'm surprised you want it on the Enter key, and not on the TAB one...
grf wrote: 3. I put the code in a private function of the frame.
But the frame does not know about the events sent to the notebook/book panels. Try to put it there...

Thank you.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

Hi,

I tested it now with an event handler for "EVT_CHAR". When I use

Code: Select all

this->GetEventHandler();
then the corresponding function will be called - it means that the event has been triggered. For all other (panel, notebook_panel and so on) no function call appears. I have no idea, which event handler must be used - it seems, that "this" might be an good option... :?:

However - the event was triggered - but it has absolutely no effect for the GUI controls. With my keyboard Tab I can move the focus from button to button - with Return from my keyboard I can select or deselect radiobuttons.
The same events simulated with

Code: Select all

wxKeyEvent key_down_event(wxEVT_CHAR);
key_down_event.m_keyCode = WXK_TAB;
wxEvtHandler *handler=this->GetEventHandler();
handler->ProcessEvent(key_down_event);
have no effects - no focus is moved and no radiobutton is selected/deselected.

Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf wrote: Hi,

I tested it now with an event handler for "EVT_CHAR". When I use

Code: Select all

this->GetEventHandler();
then the corresponding function will be called - it means that the event has been triggered. For all other (panel, notebook_panel and so on) no function call appears. I have no idea, which event handler must be used - it seems, that "this" might be an good option... :?:

However - the event was triggered - but it has absolutely no effect for the GUI controls. With my keyboard Tab I can move the focus from button to button - with Return from my keyboard I can select or deselect radiobuttons.
The same events simulated with

Code: Select all

wxKeyEvent key_down_event(wxEVT_CHAR);
key_down_event.m_keyCode = WXK_TAB;
wxEvtHandler *handler=this->GetEventHandler();
handler->ProcessEvent(key_down_event);
have no effects - no focus is moved and no radiobutton is selected/deselected.

Regards
Michael
Maybe all you need is to call Connect()?

Thank you.
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

Connect()? It connects a function with an event handler (and type), but I have no function to connect. The function which has to be triggered by this event is implemented somewhere in a library. It's the function which gives users the possibility to control applications with their keyboard...

Regards
Michael
Sowas kommt von sowas.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

grf wrote: Connect()? It connects a function with an event handler (and type), but I have no function to connect. The function which has to be triggered by this event is implemented somewhere in a library. It's the function which gives users the possibility to control applications with their keyboard...

Regards
Michael
grf,
Just for testing purposes, can you create such handler locally and connect to it.

Just a wild guess, but maybe there is something wrong with the libary passing arguments correctly.....
grf
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jun 01, 2006 1:53 pm
Location: Germany

Post by grf »

Hi,

I just wrote such a simple handler (member function, static event table) and it was called. It means that the event was passed to it. Ok so far...
However, when I try to send the event without my own handler ProcessEvent() returns "false". That means, it couldn't find a suitable event handler. Somewhere in the whole program (in the wx library) must be such a handler, because events from the real keyboard are handled correctly. :?: :?:
That confuses me, but it seems to be my problem: The event seems to be not propagated to the suitable handler which handles keyboard events for navigating the GUI via keyboard.

Therefore I tried to set the propagation level to "1" via

Code: Select all

key_down_event.ResumePropagation(1);
but - unsurprising - nothing happened.

Regards
Michael
Sowas kommt von sowas.
Post Reply