0

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.

Jackie
  • 455
  • 1
  • 7
  • 16
  • 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? – jrsala May 24 '13 at 20:02
  • What do you use to create the window? Whatever it is (GLUT?), it probably comes with input handling. – Appleshell May 25 '13 at 02:21

1 Answers1

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.