From: Aaron H. <arc...@gm...> - 2005-02-28 15:23:11
|
On Mon, 2005-02-28 at 14:25 +0000, Alex Tweedly wrote: > > def on_txtText_keyPress(self, event): > > if len(self.components.txtText.text) < 10 or event.keyCode > in=20 > > [8,316,317,318,319,127,314,315]: > > event.Skip() Here's what I do: 1. create a class module to handle keypress tests 2. in it, define some lists and functions like this: __digit_keys__ =3D [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2, wx.WXK_NUMPAD3, wx.WXK_NUMPAD4, wx.WXK_NUMPAD5, wx.WXK_NUMPAD6, wx.WXK_NUMPAD7, wx.WXK_NUMPAD8, wx.WXK_NUMPAD9] __money_keys__ =3D [36, 44, 46] __ctrl_keys__ =3D [wx.WXK_NUMLOCK, wx.WXK_CAPITAL, wx.WXK_SCROLL] __nav_keys__ =3D [wx.WXK_DELETE, wx.WXK_BACK, wx.WXK_TAB, wx.WXK_RETURN, wx.WXK_LEFT, wx.WXK_RIGHT] __date_keys__ =3D [wx.WXK_DIVIDE, wx.WXK_SUBTRACT, 45, 47] __zip_keys__ =3D [wx.WXK_SUBTRACT, 45] __phone_keys__ =3D [32, 40, 41, 45, 120, wx.WXK_SUBTRACT] __valid_date_keycodes__ =3D __digit_keys__ + __date_keys__ + __ctrl_keys__ __valid_money_keycodes__ =3D __digit_keys__ + __money_keys__ + __ctrl_keys__ __valid_zipcode_keycodes__ =3D __digit_keys__ + __zip_keys__ + __ctrl_keys__ __valid_phone_keycodes__ =3D __digit_keys__ + __phone_keys__ + __ctrl_keys__ __valid_integer_keycodes__ =3D __digit_keys__ + __ctrl_keys__ def isNavKey(self, keyCode): """ Tells whether a key pressed was a navigational key (like tab, delete, etc.) """ return keyCode in self.__nav_keys__ def validkey_date(self, keyCode): return self.isNavKey(keyCode) or keyCode in self.__valid_date_keycodes__ def validkey_zip(self, keyCode): return self.isNavKey(keyCode) or keyCode in self.__valid_zipcode_keycodes__ def validkey_money(self, keyCode): return self.isNavKey(keyCode) or keyCode in self.__valid_money_keycodes__ def validkey_phone(self, keyCode): return self.isNavKey(keyCode) or keyCode in self.__valid_phone_keycodes__ def validkey_integer(self, keyCode): return self.isNavKey(keyCode) or keyCode in self.__valid_integer_keycodes__ 3. in my app's init attribute/method: import keyCodeTests kct =3D keyCodeTests.keyCodeTests() 4. in keyPress events, something like this: def on_MyIntegerField_keyPress(self, event): if kct.isNavKey(event.keyCode) or (kct.validkey_integer (event.keyCode) and len(event.target.text) < self.__MyIntegerField_MAXLEN__): event.skip() FWIW, you should probably set things like max acceptable field length, max acceptable values, etc., for your fields in a config file and then write code to read the config first thing (during init). That way you easily change stuff without having to re-write code. Hope this helps. -Aaron |