From: Kevin A. <al...@se...> - 2004-04-15 17:04:31
|
> From: Kevin Altis > > On Apr 15, 2004, at 8:06 AM, Schollnick, Benjamin wrote: > > > > >> Since you have control over what goes in the field within > >> your own code, > >> keyPress is probably the easiest since it would just deal > >> with what a user > >> types. > >> > >> Given a TextField named 'fld' the following event handler > >> should do what you > >> want for plain ascii as well as unicode strings > >> > >> def on_fld_keyPress(self, event): > >> upper = chr(event.keyCode).upper() > >> if chr(event.keyCode) != upper: > >> event.target.replaceSelection(upper) > >> else: > >> event.skip() Well I have egg on my face now. So much for writing and testing code before I have my morning coffee. I thought I had tested arrow keys, but I guess not since chr() will throw a value error for keyCodes outside the range 0-255. At a minimum you need to catch that exception. def on_field1_keyPress(self, event): try: upper = chr(event.keyCode).upper() if chr(event.keyCode) != upper: event.target.replaceSelection(upper) else: event.skip() except ValueError: event.skip() However, now that I think about it more I don't see how this is going to deal with Unicode characters outside the ASCII range. It might be simpler to check whether the keyCode is in the wxWidgets defined key codes to see if it is a special character. Sadly, I don't know enough about Unicode to take this much further. Maybe someone else can chime in with the solution to deal with non-ASCII. ka |