I'm guessing that you use the numbers because it's awkward to convert
to and from a string representation for saving in/loading from your
config file.
Converting the wx enums into some kind of Enumeration object might
really help. I'm using a wrapper for a C++ library that provides this
and it's really convenient.
Here's a sample of what the class could look like, based in part on
what this other wrapper provides:
class KeyCodes(object):
_values = {
"WXK_PAGEUP":100,
"WXK_PAGEDOWN":100
}
def __init__(self, name):
assert (name in KeyCodes._values)
self.value = name
def __str__(self):
return self.value
__repr__ = __str__
def __int__(self):
return KeyCodes._values[self.value]
def __hash__(self):
return KeyCodes._values[self.value]
def __cmp__(self, other):
try:
return cmp(int(self), int(other))
except ValueError:
return cmp(str(self), str(other))
@staticmethod
def load(name):
#Returns a KeyCodes object based on the
#symbolic name provided, for loading
#from a string.
if name in KeyCodes._values:
return getattr(KeyCodes, name)
else:
raise KeyError, "Not a KeyCode"
#these values and the dictionary auto-generated by swig
for value, key in KeyCodes._values.iteritems():
setattr(KeyCodes, value, KeyCodes(value))
#testing
assert(100 == KeyCodes.WXK_PAGEUP)
assert(KeyCodes.WXK_PAGEUP == 100)
assert(101 != KeyCodes.WXK_PAGEUP)
assert(KeyCodes.WXK_PAGEUP != 101)
assert(KeyCodes.WXK_PAGEUP == KeyCodes.WXK_PAGEUP)
assert(KeyCodes.WXK_PAGEUP == "WXK_PAGEUP")
assert("WXK_PAGEUP" == KeyCodes.WXK_PAGEUP)
assert("WXK_PAGEDOWN" != KeyCodes.WXK_PAGEUP)
assert(KeyCodes.WXK_PAGEUP != "WXK_PAGEDOWN")
assert(KeyCodes.WXK_PAGEUP is KeyCodes.load("WXK_PAGEUP"))
One way that this is different than the wrapper I'm using is that it's
the string value (the symbolic name) that is stored in each instance,
rather than the numeric value. This means that, for example, you can
pickle these instances and they will have the correct numeric value
when loaded, even if wx changes underneath them. The current wx.WXK_
constants could be (I think?) transparently replaced with the class
members.
The only thing this needs is a way for SWIG to iterate over members of
an enum. I'm looking at the docs now to see if it's possible.
···
On 12/29/06, Franz Steinhaeusler <franz.steinhaeusler@gmx.at> wrote:
On Thu, 28 Dec 2006 11:51:42 -0800, Robin Dunn <robin@alldunn.com> wrote:
>Franz Steinhaeusler wrote:
>> Hello NG,
>>
>> my DrPython keyboard shortcuts are not working anymore.
>>
>> There are changes between 2.6 and 2.8 in numerous keycodes.
>> For example left cursor it was 316 and now it is 314.
>
>Don't use the numbers. Use the wx.WXK_* constants that already exist.
>
I know, this is not good, but I expected no change.
The numbers are stored externally in a preferences file.
>>
>> Is there a table anywhere to see, what keycodes have changed?
>
>WXK_NEXT and WXK_PRIOR were made equivalent to WXK_PAGEDOWN and
>WXK_PAGEUP, so everything after that spot in the enum had their values
>changed.
Thank you for your information!