Custom control which can hold focus and receive key-events?

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
Shodan77
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Apr 06, 2005 4:57 pm

Custom control which can hold focus and receive key-events?

Post by Shodan77 » Wed Apr 06, 2005 5:02 pm

I want to create a total custom control which can hold the focus and receive key-events (for example for arrow-keys) like any text-control or list-box can.
But no matter from which class I derive my custom control (wxWindow, wxControl, wxPanel, ...), it is painted correctly but does not receive key-events. Only char-events are received.

I absolutely need to process the arrow-key-events, so how can I enable that for my custom control?

Every hint is appreciated. Thank you in advance.

Robert.

JonnyL
Earned a small fee
Earned a small fee
Posts: 13
Joined: Mon Apr 04, 2005 7:36 pm
Location: Germany, Trier

Post by JonnyL » Wed Apr 06, 2005 7:02 pm

I had exactly the same problem: I wrote a custom TextViewer in 2.4.2 and everything worked fine, but when I switched to 2.5.4 (wx-devcpp 6.7beta) only Mouse clicks were recieved in my control (I found it strange that event.ButtonDown() worked, but GetWheelRotation didn't).

I solved this Problem by just catching EVT_MOUSE_EVENTS and in the associated function calling SetFocus().

Maybe this helps you for the first time, but there must be a better solution.

If someone knows, this would be nice.

Shodan77
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Apr 06, 2005 4:57 pm

Post by Shodan77 » Thu Apr 07, 2005 8:34 am

JonnyL wrote:I had exactly the same problem: I wrote a custom TextViewer in 2.4.2 and everything worked fine, but when I switched to 2.5.4 (wx-devcpp 6.7beta) only Mouse clicks were recieved in my control (I found it strange that event.ButtonDown() worked, but GetWheelRotation didn't).
I am using 2.4.2! It would be sufficient if it worked with this version of wx. So I am probably deriving my control from the wrong base-class. On which base-class did you build your TextViewer? I am currently using wxControl, also tried wxWindow and wxPanel.

koderpat
Experienced Solver
Experienced Solver
Posts: 62
Joined: Tue Apr 05, 2005 5:04 am
Contact:

Post by koderpat » Thu Apr 07, 2005 9:23 am

Find the event type that gets arrowkey events to the frame.

Either use EvtHandler::connect()
or PostEvent() to go from the evthandler of your frame to the evthandler of your control

Shodan77
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Apr 06, 2005 4:57 pm

Post by Shodan77 » Thu Apr 07, 2005 9:38 am

koderpat wrote:Find the event type that gets arrowkey events to the frame.

Either use EvtHandler::connect()
or PostEvent() to go from the evthandler of your frame to the evthandler of your control
But that would result in a permanent "rerouting" of arrowkey events to my control, no? But I want my control to behave like any other text-control, list-box, etc.: If you navigate to it with TAB or click on it, it receives *all* key-events. If you leave it with TAB or click into another control it stops receiving the events.
This must be possible without the kind of workaround you describe :-/

koderpat
Experienced Solver
Experienced Solver
Posts: 62
Joined: Tue Apr 05, 2005 5:04 am
Contact:

Post by koderpat » Thu Apr 07, 2005 10:46 am

Hrm.. Could you mess with the pushevthandler() ? and skip() events that don't pertain to your control? when its not in focus?


theres probably a better way...


what base class are you derieving from? you probably want someting that already knows how to handle key events.

Shodan77
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Apr 06, 2005 4:57 pm

Post by Shodan77 » Fri Apr 08, 2005 9:14 am

koderpat wrote:what base class are you derieving from? you probably want someting that already knows how to handle key events.
My first try was to derive it from wxPanel, but then I found out that wxPanel cannot get the focus at all. Currently I derive my custom control from wxControl.

Vadim says that I should use the style wxWANTS_CHARS for my control:
VZ wrote: Use wxWANTS_CHARS style. This is a hack but it's currently needed to get
events normally used for navigation between controls.

Regards,
VZ
I am currently trying that but it doesn't seem to have any effect :( Maybe I need to switch to wx-version 2.5?

Post Reply