Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Key Code translation?

Key Code translation?
Thread Tools
Seb G
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
May 19, 2002, 01:10 PM
 
Is there any quick way in Cocoa/ObjC to translate the Key Code, as returned by the keyCode method of an NSEvent, into the corresponding character, subject to the current keyboard setting? I don't have any key event hanging around, as I'm doing something like the Key Caps utility.
     
Ghoser777
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 19, 2002, 05:14 PM
 
Call characters off the NSEvent instance.

HTH,
F-bacher
     
Seb G  (op)
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
May 19, 2002, 05:50 PM
 
Ghoser, unfortunately I don't have any instance of NSEvent handy. I'm trying to draw a virtual keyboard.
     
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Jul 10, 2003, 05:30 PM
 
This seems a curious omission, but... it appears Apple does not have an NSEnterKey or NSReturnKey constant for the NSEvent class.

In fact, calling [theEvent characters] does not return anything for the former and simply returns an "r" for the latter. How can I use that to tell when the user presses enter or return?!
What's more, the keyCode method returns an integer specific to the keyboard connected, thus making it totally useless, right? It'll be different on each keyboard.

How can I tell when enter has been pressed? In my app, I already have my custom NSView set up as the first responder, and it calls its method when a key is pressed. Now I need a way to filter it down to just those two keys.

I have been boning up on the responder chain, NSApplication event handling, and keyboard interface control, but none of these are either helping or hindering me. It's simply undefined. any ideas?
Thanks.
you are not your signature
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 10, 2003, 06:30 PM
 
Perhaps I'm not following what you guys are looking for, but this works fine for me in my view subclass:

- (void)keyDown:(NSEvent *)theEvent
{
char c = [[theEvent characters] characterAtIndex:0];
if ( c == 0x03 )
NSLog( @"Backslash N" );
else if ( c == '\r' )
NSLog( @"Backslash R" );
else
NSLog( @"%c", c );
}

Check out the thread here - it's where I came up with 0x03 instead of '\n':
http://cocoa.mamasam.com/COCOADEV/2001/08/2/10806.php
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:25 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,