[GD-General] Cross-platform key handling
Brought to you by:
vexxed72
From: Brian H. <bri...@py...> - 2002-02-09 03:24:05
|
This is something that's been bugging me for a while. Right now my multi-platform key handling is a bit of a mess, and I'm probably going to end up rewriting it. The problem is that each platform has a different idea of how much information to export to you about keys being pressed. On Windows it's pretty simple -- you trap WM_KEYDOWN which are system codes that you can translate to ASCII using MapVirtualKey(). On the Mac it's a little uglier because you don't get individual scan code presses that you then translate, instead you get cooked key codes. For example, ctrl-f is, literally, ctrl-f, not a 'f' with the control modifier pressed. Now, on Windows it's possible to get both cooked and uncooked data by trapping WM_CHAR and WM_KEYDOWN (and WM_SYSKEYDOWN). On the Mac you have to deconstruct it into constituent pieces. On top of that, different platforms have different keys. The iMac, for example, doesn't have function keys or a key pad. So there are two issues: how to write a generic event handling mechanism that's transparent to the application, and how to deal with keys that may disappear on different systems. For the former, I'm currently forcing the app to handle key code -> character mapping. This means that on the Mac, I have to deconstruct characters into appropriate key code/modifier key combinations. There's also a bit of ugliness reconciling the fact that different platforms may not distinguish (or export) certain key presses (e.g. keypad arrow vs. inverted-t arrow keys, numpad / vs. keyboard /, etc.). For handling non-existent keys, I'm basically just avoiding those keys. My apps don't use F-keys and don't distinguish between top row numbers and keypad numbers, which is somewhat limiting. Is there a cleaner way to handle all this? Brian |