The MobileKey class is something that I’ve whipped up to solve a couple of nagging problems I keep running into, and I’m posting it here in case others might find it useful. The specific problems are these:
- Trying (and failing) to remember the values returned by Key.getCode() and Key.getAscii() for the various d-pad and keypad keys on a mobile phone;
- Accounting for the fact that key.getCode() returns the same value for ‘8’ and ‘*’ as well as ‘3’ and ‘#’, but that key.getAscii() returns ‘0’ for left, right, up, and down on the d-pad.
MobileKey is a simple class intended to solve these problems. It has one method and a number of properties (I said it was simple!). The properties in MobileKey are as follows:
1 2 3 4 5 6 7 8 9 10 11 12
The class extends the Key class, so you can access all of the Key constants through MobileKey (Key.UP, etc.). I’ve also included the ExtendedKey constants (I have some doubts about the value of including the soft key constants; if you also have your doubts, feel free to comment them out of the MobileKey.as file and continue to use the ExtendedKey class.)
For me, the beauty of this is that I no longer have to remember the Ascii equivalent for the ‘7’ key. I just have to remember MobileKey.SEVEN, which strikes me as being much easier. Also, all of the constants for all keys are now under a single class, which I just find a little simpler.
The one method in the file is meant to replace both Key.getCode() and Key.getAscii(). Essentially, it returns the getAscii() value for the key pressed; however, if one of the d-pad buttons is pressed, it returns the getCode() value. There’s a sample file inside the zip archive showing a very simple keyListener using the MobileKey class.
If you find this useful, please let me know. While this may not be the most elaborate AS2 class ever created, it is the first one I’ve written that I’ve put out for public consumption, so let me know what you think!
Update: Made some minor changes to the class because it turns out that some FL2.0 devices such as the Nokia 5300 actually do return a non-zero value for d-pad presses when you use getAscii(). But Verizon devices appear not do so. So, the new code should take care of that. If you do encounter a device on which this is buggy, please tell me so I can update things. If you downloaded the zip file already, download it again to get the fix.