Catching Unicode letters

I have this code block in a handler and I don’t know how to catch letters like č š đ ć ž and the like. If I use event.GetUnicodeKey() instead of event.GetKeyCode(), the F1 through F12 keys are regarded as letter keys. Please assist me what should I do (what method should I use). Here is my code block:

try:

keyCode = event.GetKeyCode()

keyCharacter = chr(keyCode)

if keyCharacter.isalpha():

print ‘Letter inputted.’

elif keyCharacter.isdigit():

print ‘Digit inputted.’

except ValueError:

print ‘Not a letter or a digit inputted.’

finally:

event.Skip()

Is this in a EVT_KEY_DOWN or a EVT_CHAR event handler? You'll want to be sure that you understand the difference between the two because they are related but have different behaviors regarding non-ascii character values. Search the archives for messages from be discussing "raw" and "cooked" key values.

In a nutshell you'll only get keycodes in a key-down handler that corespond to the keyboard's scan-codes, which just happen to also correspond to the acii values on a standard US keyboard for the alpha numeric keys. If the key-down handler does not consume the key (by not calling Skip) then the system then checks shift states, passes the event though any alternate keyboard layout and/or IME if they are active. At this point the key event may or may not turn into a EVT_CHAR event, and it is in the char events that you will get things like the accented letters or proper unicode characters.

Play with the KeyEvents sample in the demo so you can see how the events act and what attributes they give you, and perhaps you will see a pattern emerge that will work for your needs. Typically I will catch both the key-down and the char events, and will deal with "command keys" like F-keys there, and will call Skip for the rest so they can go on to the char handler where I can get the proper character values.

···

On 1/28/11 4:46 PM, Boštjan Mejak wrote:

I have this code block in a handler and I don't know how to catch
letters like č š đ ć ž and the like. If I use event.GetUnicodeKey()
instead of event.GetKeyCode(), the F1 through F12 keys are regarded as
letter keys. Please assist me what should I do (what method should I
use). Here is my code block:

         try:
             keyCode = event.GetKeyCode()
             keyCharacter = chr(keyCode)
             if keyCharacter.isalpha():
                 print 'Letter inputted.'
             elif keyCharacter.isdigit():
                 print 'Digit inputted.'
         except ValueError:
             print 'Not a letter or a digit inputted.'
         finally:
             event.Skip()

--
Robin Dunn
Software Craftsman

Is this in a EVT_KEY_DOWN or a EVT_CHAR event handler?
It’s in the EVT_CHAR event handler.