Cocoa only input/event mechanism?

Mr. Yuk
Post: #1
Ok, well, this is my first posting to iDevGames so greetingz to everybody...

I'm a professional windoze developer, and spend a lot of time mucking about in databases and boring goo like that. In an effort to recapture the 'fun side' of programming, I've been screwing around with learning Cocoa and OpenGL on my G4 tower...

(which I will mention is almost as much fun as programming on my old Amiga back in the day...)

At this point, I've got a fairly decent grasp on Objective C, and know enough about Project Builder to consider myself dangerous.

I've worked through writing a C++ class that is using the CGDisplay stuff to setup my display. Further, I created a simple window in Interface Builder with some buttons and such to give me a way of excercising my C++ class. This works fine (especially with dual monitors) for experimentation because I can control whats going on from my window, and see the results on my secondary display. Neat.

I've started wondering about how to nuke the window and controller class in favor of using the keyboard to get user input directly. For example, catch the function keys, arrow keys, etc.

I've seen some examples of this using Carbon, but I am curious what the 'appropriate' method is to build an input/event handler loop in Cocoa?

I would venture that I should be able to craft something like the following in 'pure' Cocoa?:

main() {

   //do some setup

  while( wating_for_someting_to_happen) {
      //process whatever happened

  //do some cleanup


I've seen vague references to NSRunLoop, or the HID Manager, but haven't found anthing really usefull to someone (like me) that hasn't got a clear grasp of the OSX event mechanism or input model.

Of course, I'm angling toward joystick control next. But for now I would be happy with just smaking keys...

Input is greatly appreciated (no pun there)...

Huh Mr. Yuk...
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
You can look at the Omni Group's GDC "Porting to Mac OS X" PDF. That should tell you everything you need for mouse and keyboard input.

Apple has sample code for using the HID manager but <rant>the HID-specific documentation seems to have gone missing</rant>. The generic IOKit stuff is still available, though, and that gives you most of what you'll need.
Quote this message in a reply
Mr. Yuk
Post: #3
Thank you OneSadCookie,

I had to surf around OMNI's ftp site for a while, and eventually found the "PortingToOSX.pdf" and DMG you were referencing. I couldn't seem to find a direct link on their web site, perhaps it was hidden in plain view?

At any rate, if anyone else is interested, the exact subdirectory is:


What I discovered was simply to create my controller class (with or without an associated window) in interface builder and set the "File's Owner' delegate outlet to my controller instance. After that, you can apparently set up the event loop inside applicationDidFinishLaunching (in the controller class).

After some terminating input (like smacking the escape key) you just call [NSApp terminate] to shut everything down.

It's simple after you actually see it implemented.

Thanks again...

:-) Mr. Yuk
Quote this message in a reply
Post: #4
If you are wondering how to get a multiple-keys down keyboard interaction coded in Cocoa you can see an article I posted called GameKeyboardHandling on the Cocoa Dev wiki:

It basically allows you to respond properly to when the user has, for example, both the up and left arrows pressed. Enjoy.
Quote this message in a reply
Post: #5

If you understand C++ and Windows Event loops this is probably the best example to map that knowledge to Cocoa.
Quote this message in a reply
Posts: 208
Joined: 2005.04
Post: #6
OneSadCookie Wrote:Apple has sample code for using the HID manager but <rant>the HID-specific documentation seems to have gone missing</rant>.

Here are the docs:

It was updated on July 7, 2005, so I bet this is a new URL.
Quote this message in a reply
Posts: 608
Joined: 2002.04
Post: #7
Please do not post in threads that are this old. The question has either been answered or the poster has moved on.
Quote this message in a reply
Posts: 320
Joined: 2003.06
Post: #8
haha, it all seemed reasonable until I saw that 3 year gap. I did wonder why OSC was so polite though Rasp

Chopper, iSight Screensavers, DuckDuckDuck:
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocoa Event Loop/NSTimer revisited Fenris 6 8,297 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  Game input in Cocoa aegidian 8 9,366 Nov 4, 2003 03:47 PM
Last Post: aegidian
  When to run event loop... SethWillits 3 5,847 Aug 11, 2002 07:48 AM
Last Post: ededed