Configurable keys - describe key that was pressed

Moderator
Posts: 435
Joined: 2002.09
Post: #1
I would like to give players the ability to remap keys in my game. Nothing magical about that, but to save tedium I was hoping someone already had a function that would return a description for most key codes. For example, given the character code for the Option key the function would return "Opt" or "Option". (I don't mind if some really obscure keys are not described.)

I am using Cocoa keyboard events, but I can turn them into ascii or whatever. So if the solution only handles ascii characters typed on English keyboards, fine. I could code that myself though it will take an hour just to get most of the mappings... But I'd rather have a function that is friendly to international keyboards. My game should be localizable, though I haven't tested that yet, and it would be nice to continue with that effort.

Thanks for any hints.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 20
Joined: 2002.12
Post: #2
I have a function which will return a string based on a key code. For character keys it uses a nifty function called KeyTranslate, for the rest, it's just a case statement :-)

Code:
function KeyToStr (key: integer): string;
        var
            KCHRPtr: Ptr;
            someState: UInt32;
            Result: longint;
            ASCII: packed array [0..3] of char;
            
    begin

        case key of
            $31:
                KeyToStr := 'Space';
            $7E:
                KeyToStr := 'Up Arrow';
            $7B:
                KeyToStr := 'Left Arrow';
            $7D:
                KeyToStr := 'Down Arrow';
            $7C:
                KeyToStr := 'Right Arrow';
            $37:
                KeyToStr := 'Command';
            $38:
                KeyToStr := 'Shift';
            $24:
                KeyToStr := 'Return';
            $39:
                KeyToStr := 'Caps Lock';
            $30:
                KeyToStr := 'Tab';
            $33:
                KeyToStr := 'Delete';
            $3A:
                KeyToStr := 'Option';
            $3B:
                KeyToStr := 'Control';
            $4C:
                KeyToStr := 'Enter';
            $47:
                KeyToStr := 'Clear';
            $72:
                KeyToStr := 'Help';
            $73:
                KeyToStr := 'Home';
            $74:
                KeyToStr := 'Page Up';
            $75:
                KeyToStr := 'Del';
            $77:
                KeyToStr := 'End';
            $79:
                KeyToStr := 'Page Down';
            
            otherwise
                begin
                    KCHRPtr := Ptr(GetScriptManagerVariable(smKCHRCache));
                    someState := 0;
                    Result := KeyTranslate(KCHRPtr, key, someState);
                    
                    longint(ASCII) := Result;
                    
                    KeyToStr := ASCII[3]
                end
        end
        
        
    end;

HTH,
-wep

"Programmers are tools for converting caffeine into code."
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #3
You can look at the Hooptie source for one way of doing it. Basically switch the ASCII value for the key and return a pre-made string.

If anybody's got a better way I'd definitely like to hear it. The Hooptie way is hardly optimal.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #4
The characters you get from NSEvents have the following string values:
Code:
#define UnicodeTab        [NSString stringWithFormat:@"%C",0x21E5]
#define UnicodeBackTab        [NSString stringWithFormat:@"%C",0x21E4]
#define UnicodeEnter        [NSString stringWithFormat:@"%C",0x2324]
#define UnicodeReturn        [NSString stringWithFormat:@"%C",0x21A9]
#define UnicodeForwardDelete    [NSString stringWithFormat:@"%C",0x2326]
#define UnicodeDelete        [NSString stringWithFormat:@"%C",0x232B]
#define UnicodeClear        [NSString stringWithFormat:@"%C",0x2327]
#define UnicodeEscape        [NSString stringWithFormat:@"%C",0x238B]
#define UnicodeNBSP        [NSString stringWithFormat:@"%C",0x237D]
#define UnicodeSpace        [NSString stringWithFormat:@"%C",0x2423]
#define UnicodeBackSpace    [NSString stringWithFormat:@"%C",0x21D0]
#define UnicodeCR        [NSString stringWithFormat:@"%C",0x21B5]
#define UnicodeLF        [NSString stringWithFormat:@"%C",0x21B4]
#define UnicodeInsert        [NSString stringWithFormat:@"%C",0x22EF]

#define UnicodeShift        [NSString stringWithFormat:@"%C",0x21E7]
#define UnicodeControl        [NSString stringWithFormat:@"%C",0x2303]
#define UnicodeOption        [NSString stringWithFormat:@"%C",0x2325]
#define UnicodeCommand        [NSString stringWithFormat:@"%C",0x2318]
#define UnicodeCapsLock        [NSString stringWithFormat:@"%C",0x21EA]

#define UnicodeLeft        [NSString stringWithFormat:@"%C",0x2190]
#define UnicodeRight        [NSString stringWithFormat:@"%C",0x2192]
#define UnicodeUp        [NSString stringWithFormat:@"%C",0x2191]
#define UnicodeDown        [NSString stringWithFormat:@"%C",0x2193]
#define UnicodePageUp        [NSString stringWithFormat:@"%C",0x21DE]
#define UnicodePageDown        [NSString stringWithFormat:@"%C",0x21DF]
#define UnicodeHome        [NSString stringWithFormat:@"%C",0x2196]
#define UnicodeEnd        [NSString stringWithFormat:@"%C",0x2198]

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
kberg
Unregistered
 
Post: #5
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #6
Thanks for the replies I've gotten.

I've discovered now that in my zeal to abstract the OS-specific code from the core of my game, I've accidentally broken the ability to use internationalized strings. At one "bottleneck" the NSStrings are turned into C strings, just because I wanted to hide the Cocoa objects from the innards of the code. I may be able to fix this with a couple of days work, but I'm not sure if I'll have time!

If I stick with C strings (and English) then kberg's routine is awesomely complete. If I can get back to unicode, then kelvin's definitions will save me some time. Either way, thanks.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 20
Joined: 2002.12
Post: #7
You don't need to include all the letters, numbers, and other keys which have a once character equivalent. You can retrieve those from a key code using KeyTranslate. It will return a packed array of four characters, the fourth character being the symbol itself.

See the code I posted earlier for an example.

HTH
-wep

"Programmers are tools for converting caffeine into code."
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
If you convert to a C string using UTF-8, you still preserve the unicode so you can get back to it later...

Of course, there are other disadvantages to using UTF-8, such as the fact that one char doesn't correspond to one character...
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #9
Quote:Originally posted by OneSadCookie
If you convert to a C string using UTF-8, you still preserve the unicode so you can get back to it later...

Of course, there are other disadvantages to using UTF-8, such as the fact that one [b]char
doesn't correspond to one character... [/b]


Thanks for the tip, I'll have to look into that. But I think I'm doing a lossy conversion at the moment.

Since this particular conversion is used just so that I can define my APIs using types which are not as OS-specific as Cocoa types, I may just switch to use "unichar."

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #10
I'm apparently misunderstanding something. I thought I'd be getting unicode characters back from the events. Instead, the codes I get back look like ascii codes.

Here is how I am getting the characters from the keyUp event:

NSString *strEv = [pEvent charactersIgnoringModifiers];
unichar unich = [strEv characterAtIndex:0];

For now I will just use ascii codes to decode the values. But I'd like to understand this better. Any thoughts on why I'm not seeing the unicode values like Kelvin lists earlier in this thread?

Thanks...

[Edit: correction: some keys are coming in with unicode, but the values do not match Kelvin's. Others, not. For example, I get f729 for Home. Tab key, I get 0x9.]

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #11
I think that for characters which are in the normal ASCII charset the Unicode standard preserves the values (so the ASCII value for A is equal to the Unicode code for A)

Then again I could be completely wrong.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
Quote:Originally posted by Steven
I think that for characters which are in the normal ASCII charset the Unicode standard preserves the values (so the ASCII value for A is equal to the Unicode code for A)

Then again I could be completely wrong.


You're not, this is precisely the case.

In fact, the bottom 256 characters in Unicode correspond to ISO 8859-1, and the bottom 127 in ISO 8859-1 correspond to ASCII.
Quote this message in a reply
Moderator
Posts: 435
Joined: 2002.09
Post: #13
Steven: I think you are right...

In other news, I found most of the codes (Fn keys, Home, Page Up...) were defined as constants in NSEvent.h. Other codes (Esc, Return, Space...) I found using trial and error. Since those are in the Ascii range I'm hopeful they will be valid on international keyboards. If they aren't, well, it's just an annoyance for the player, nothing fatal.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
Only thing to watch out for is that return/enter might give '\r' or '\n'... probably best to check for both.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Spaces on Snow Leopard and ctrl/arrow keys GolfHacker 19 15,493 Dec 4, 2012 03:18 PM
Last Post: bmantzey
  Multiple Keys down in SDL silver9172 2 4,902 May 30, 2009 06:16 PM
Last Post: scgames
  Left mouse button causes moueup event despite button still being pressed. QuestingCordiial 28 11,674 Jun 21, 2008 03:18 PM
Last Post: AnotherJake
  [NSEvent keyCode] to actual keys? teknein 8 9,516 Sep 5, 2007 07:27 AM
Last Post: teknein
  Keys ......again Coin 7 6,317 Jan 29, 2005 09:38 PM
Last Post: belthaczar