PDA

View Full Version : Cocoa only input/event mechanism?


Mr. Yuk
2002.11.01, 02:40 PM
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)...

:???: Mr. Yuk...

OneSadCookie
2002.11.01, 06:56 PM
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.

Mr. Yuk
2002.11.02, 03:37 PM
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:

pub/software/Archive/Source/MacOSX/Games


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

AlainODea
2005.08.01, 09:17 PM
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:
http://www.cocoadev.com/index.pl?GameKeyboardHandling

It basically allows you to respond properly to when the user has, for example, both the up and left arrows pressed. Enjoy.

nabobnick
2005.08.02, 12:15 AM
GameEngine.zip (http://www.nutzy.net/GameEngine.zip)

If you understand C++ and Windows Event loops this is probably the best example to map that knowledge to Cocoa.

Andrew
2005.08.02, 12:59 AM
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:

http://developer.apple.com/documentation/DeviceDrivers/Conceptual/HID/

It was updated on July 7, 2005, so I bet this is a new URL.

Josh
2005.08.02, 05:14 AM
Please do not post in threads that are this old. The question has either been answered or the poster has moved on.

reubert
2005.08.21, 08:42 AM
haha, it all seemed reasonable until I saw that 3 year gap. I did wonder why OSC was so polite though :p