About wx key codes and events

The following is an email from the wxPython-users mail list.

Date: Wed, 22 Aug 2007 17:14:07 -0700
From: Robin Dunn <robin@---.com>
To:  wxPython-users@---.org
Subject: Re: [wxPython-users]Questions about keyboard WXK_CODE

甜瓜 wrote:
>> Re-post from wxWidget forum.
> Hi,
> 1. I want to know what does these WXK_Code represent for? (I'm using
> standard 101/102 PS/2 keyboard)
> WXK_COMMAND
> WXK_HELP
> WXK_EXECUTE
> WXK_MENU
> WXK_CANCEL
> WXK_LBUTTON WXK_RBUTTON // I think they only occurred in mouse_event...
> WXK_PRINT // I only got WXK_SNAPSHOT when "PrtSc/SysRq" key is pressed

The world has more in it than just PC Keyboards.  These key codes are 
used on systems that do not have PC keyboards by default.  For example 
the first two are used on a Mac (The command key is the 'apple' key next 
to the space bar, and the help key is where the Insert key would be on a 
PC keyboard.)  I think the others that you listed are used on older 
workstation keyboards that have dedicated keys for those functions.


> 
> 2. What is WXK_CODE of META-Key?
> BOOL wxKeyEvent.MetaDown(); This function indicates whether META-key
> is down. But what is the corresponding WXK_CODE?

It depends on the platform and the keyboard.  For example Windows 
doesn't really have a meta key, although there is a keycode for the 
Windows key which is probably where meta would be on the keyboard if 
Windows supported it.  For software that was designed to use a Meta key 
(like Emacs) then it will usually have an alternate key sequence for 
using it, like using Alt instead, or pressing and releasing ESC.

On Mac it's the wx.WXK_COMMAND key.

On Linux and other X-Windows systems it depends on how your X server is 
set up, or if you've fiddled with the xmodmap settings.  If you've got a 
Meta key on your keyboard try running the KeyEvent sample in the demo 
and see what you get when you press the key.  It might just be mapped to 
Alt.


> 
> 3. (Hard) How does wxWidget deal with foreign keyboard layout? Can I
> assume when press Shift+3, I always get '#' character?

No.

You need to understand the difference between key up/down events and 
char events.  The key events give you values based on the raw scancodes 
on the keyboards.  If you press the key at a certain location on any 
keyboard, most likely you will get the same key codes in the key events 
no matter what the system language is set to.  On the other hand, the 
char event is the result of one or more of the raw key events being 
'cooked' into a value by the OS that is appropriate for the current 
keyboard settings, and it may be very different than the equivalent key 
on a US keyboard, and it is possible to get *no* char events for some 
key events, or *multiple* char events.  It all depends on the language 
and keyboard settings.


> 
> =====
> I have put this a few days but not answered yet... And, I have new
> interesting questions ^_^
> 4. In M$ Win, wxPython window seems having a special style flag to
> indictate whether it can deal with "Input Method" output (toggled by
> Ctrl+Space). Maybe you have noticed that for some windows, when it is
> focused, and press Ctrl+Space, an Input Method floating window will
> appear. But for some other windows, this floating window never appears
> or appears but in "disable" state. I want to know which window style
> flag controls this behavior.

I don't think there is one.  I haven't had much need to use IMEs but my 
understanding that it should be automatically used by the OS.  You just 
need to make sure that your app doesn't interfere with it.  In other 
words, if you are catching the key down events you need to make sure 
that you call event.Skip() so the OS will still see the event, and you 
need to use the char events to get the 'content' to add to your control.

For example, I just added Chinese support to my XP box and added a 
ZhengMa keyboard layout.  I then ran the KeyEvents sample in the demo 
and typed a few random keys.  The IME pops up and starts selecting the 
Chinese glyphs corresponding to what I typed.  I also saw key up/down 
events in the sample's list control for every keystroke I made.  Then 
when I selected the desired glyphs from the IME the sample showed a 
series of 5 char events corresponding to what I selected in the IME, 
each with a specific Unicode value and so on.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!

AboutKeyCodes (last edited 2008-03-11 10:50:25 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.