Unless you use a third-party library (like SDL, which you've stated a desire to avoid...), the sane way to do this is via Objective-C, through your NSOpenGLView
or some custom subclass of NSView
(if you want to do more advanced management of your OpenGL context manually).
Both of those classes subclass NSResponder
, which allows you to respond to mouse and key events by implementing the appropriate selector (such as keyDown:
or keyUp:
) in your view subclass. Make sure you also implement acceptsFirstResponder
to allow your view to become a responder and get the key events in the first place:
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)keyDown:(NSEvent *)event{
unichar character = [[event charactersIgnoringModifiers]:characterAtIndex:0];
// ...process 'character' here...
}
If you are using a third-party library to do you window management and view creation, that library should provide you a mechanism to deal with key events. If it doesn't, look and see if it offers a way to provide a custom NSView
, and if it doesn't do either of those things, you should find a better library. SFML is a popular alternative.