Re: Detecting the Enter Key
Re: Detecting the Enter Key
- Subject: Re: Detecting the Enter Key
- From: "Michael Ash" <email@hidden>
- Date: Wed, 10 Dec 2008 14:28:24 -0500
On Wed, Dec 10, 2008 at 12:43 PM, Eric Gorr <email@hidden> wrote:
> One way to check to see if the enter key has been pressed is to:
>
> [theEvent keyCode] == 0x04C
>
> where 0x04C is the keyCode corresponding to the enter key.
> (Is there an Apple defined constant for this key code?)
>
>
>
> Another way, found at:
>
> http://developer.apple.com/samplecode/TrackBall/listing9.html
>
> is to do:
>
> NSString *characters = [theEvent characters];
>
> switch ([characters characterAtIndex:0])
> {
> case NSEnterCharacter:
> case NSNewlineCharacter:
> case NSCarriageReturnCharacter:
> }
>
>
>
> My question is which method is the preferred or recommended way to detect
> this key in onKeyDown?
>
> Is there a better way?
It really depends on whether you want to detect the physical key or
the logical key. Input methods may conceivably change the mapping from
one to the other and how you want to check will depend on how you want
your program's behavior to change in that situation. Most of the time
you'll want to use the second technique.
Also note that if you use the second technique, you must check that
[characters length] > 0 before you grab character 0. It is entirely
legal for an NSEvent's characters to be empty, and you don't want to
throw an exception because of that.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden