Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have code that renders a triangle using OpenGL. I would like to control it using the arrow keys on the keyboard.

Anyone know how I would do this on OS X? I think I can create an Objective-C file and import NSEvent, however, I was looking to try this without the need for Objective-C. I would also like to avoid SDL.

share|improve this question
    
Whatever OpenGL context-providing API you're using should come with input-handling features already. At least the good ones do, but I don't know much about what's available on OSX (isn't pretty much everything?). So maybe look up its documentation? –  madshogo May 24 '13 at 20:02
    
What do you use to create the window? Whatever it is (GLUT?), it probably comes with input handling. –  Adam S May 25 '13 at 2:21

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.