Handling keyboard inputs with C++ in Xcode

659 views
Skip to first unread message

ComputerGuy

unread,
Sep 15, 2009, 3:15:13 PM9/15/09
to iPhone SDK Development
I understand that this group is on the iPhone sdk & this is way off
that topic, But hopefully someone know the answer to my problem.

How would you handle a key press in C++ like the mac method:
- (void)keyDown:(NSEvent *)event:

I haven't found much online on this topic. Also I very unfamiliar with
c++. Thanks in advance!

Lucas Neiva

unread,
Sep 16, 2009, 3:50:44 AM9/16/09
to iphonesdkd...@googlegroups.com
Why not just use the Obj. C Method. I think you can even do a hybrid of C++ and Objective C. I know you can easily mix C and Objective C. Perhaps it also works with C++ and Objective C.

David Phillip Oster

unread,
Sep 16, 2009, 2:28:20 PM9/16/09
to iPhone SDK Development
The AppKit (Mac) and UIKit (iPhone) frameworks on OS X only call
objective C methods. But you can create a tiny Objective-C class in a
file with a .mm suffix (instead of the normal .m file suffix, and use
that to call a C++ method.

- (void)keyDown:(NSEvent *)event {
NSTimeInterval when = [event timestamp];
GetTimeLogVector()->push_back(when);
}

Just remember that you have to catch C++ exceptions, and re-@throw
them as Obj-C NSExceptions when you cross the boundary from one
language to the other (and versa crossing the border the other way.)
Also, don't forget that if you mix C++ or Obj-C++ with C or Obj-C that
your header files should be using:
#ifdef __cplusplus
extern "C" {
#endif

#import <myheader.h> // C header file.

#ifdef __cplusplus
}
#endif

So that the C++ compiler knows not to apply C++ name mangling to
symbols defined in C.

Remember that a C struct is the same as a C++ class, except that the
visibility of all members defaults to public. This is useful for
forward declaring pointers to classes, so you can pass them through C
and Obj-C.

ComputerGuy

unread,
Sep 16, 2009, 11:35:43 PM9/16/09
to iPhone SDK Development
Thanks for all of your suggestions! I was having trouble with my
internet connection, so I wasn't able to update my question. I
actually used SDL for my keyboard handling. Thanks though!

On Sep 16, 11:28 am, David Phillip Oster <davidphillipos...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages