Get a real character on wxEVT_CHAR_HOOK event

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
zura
Earned some good credits
Earned some good credits
Posts: 104
Joined: Thu Apr 02, 2009 8:11 pm
Location: Tbilisi, Georgia

Get a real character on wxEVT_CHAR_HOOK event

Post by zura »

Hello,

Is it possible to get a real character in the wxEVT_CHAR_HOOK event handler?

e.g. I want to handle '+', which is shift + '=' key combination, so in the event handler I get '=' key code. Is there some portable way to "calculate" the real character considering the modifiers (which would be '+' in this case)?

Like Qt's QKeyEvent::text():
Returns the Unicode text that this key generated.
Return values when modifier keys such as Shift, Control, Alt, and Meta are pressed differ among platforms and could return an empty string.
User avatar
doublemax
Moderator
Moderator
Posts: 19163
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get a real character on wxEVT_CHAR_HOOK event

Post by doublemax »

Do you really need wxEVT_CHAR_HOOK and can't use wxEVT_CHAR instead?

If yes, there is no equivalent function to do this in wxWidgets. Under Windows, you could use this: https://msdn.microsoft.com/en-us/librar ... 85%29.aspx

I don't know about other platforms.
Use the source, Luke!
zura
Earned some good credits
Earned some good credits
Posts: 104
Joined: Thu Apr 02, 2009 8:11 pm
Location: Tbilisi, Georgia

Re: Get a real character on wxEVT_CHAR_HOOK event

Post by zura »

Thanks for the reply.

Yes, I need wxEVT_CHAR_HOOK (for some reasons). In other cases, I'd need at least EVT_KEY_DOWN - because I need to handle Ctrl- +/- (So it is Ctrl-Shift-= for + on US layout) for zooming in/out and it needs to be done continuously while the keys are pressed - I believe this can NOT be done with wxEVT_CHAR, right?
User avatar
doublemax
Moderator
Moderator
Posts: 19163
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Get a real character on wxEVT_CHAR_HOOK event

Post by doublemax »

I'd need at least EVT_KEY_DOWN - because I need to handle Ctrl- +/- (So it is Ctrl-Shift-= for + on US layout) for zooming in/out and it needs to be done continuously while the keys are pressed - I believe this can NOT be done with wxEVT_CHAR, right?
Yes.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7481
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get a real character on wxEVT_CHAR_HOOK event

Post by ONEEYEMAN »

Hi,
There is also EVT_KEY_DOWN event, which I think comes right after EVT_CHAR_HOOK.

EVT_CHAR_HOOK event is unique in the sense that it comes before any actual keyboard event for extra processing. It is mostly used for ESC key processing.

EVT_KEY_DOWN can be used I think in your case. It is much simpler and more portable.

Thank you.
zura
Earned some good credits
Earned some good credits
Posts: 104
Joined: Thu Apr 02, 2009 8:11 pm
Location: Tbilisi, Georgia

Re: Get a real character on wxEVT_CHAR_HOOK event

Post by zura »

Correct. I've actually dug through my old code and for EVT_KEY_DOWN you correctly get '+' or '-' symbols.

Anyway, for my case I need to use wxEVT_CHAR_HOOK - ToUnicode/ToAscii didn't work (return 0, i.e. no translation).

The solution (on Windows) could be comparing event.GetRawKeyCode() and modifiers to:

Code: Select all

SHORT plusKeyScan = VkKeyScan('+');
BYTE plusKeyCode = LOBYTE(plusKeyScan); // == event.GetRawKeyCode()
BYTE plusModifier = HIBYTE(plusKeyScan);  // compare this to modifiers received from wxWidgets
Post Reply