Porting Help: Input Devices

Hi folks I am porting a game library over to the OSX platform, but I'm not exactly the best Mac programmer out there. In fact, this would be the first. I've been developing on Windows for many years, and Java for many as well. The library being ported will be open source through slashdot.org

OSX game development isn't exactly chock full of references and such, so I need some help and this looks to be the best resource to get help (so I'll most likely be back many times).

Here is the first piece of help I need. I need to find the best way to get access to the keyboard and the mouse in such a way that I can get the keys pressed from the keyboard and get the position and buttons pressed from the mouse. I have many ways to do this in Windows, but can't figure out how to do it in OSX. OSX has Carbon, Cocoa, and GameSprockets to accomplish the same job but I can't for the life of me figure out which one I should be using. GameSprockets didn't really seem to be conducive to getting multibutton mice - and I can't really see where its still supported by Apple.

But before I go on much more, please folks. Point me in the right direction.

* Carbon -- probably feels most like Windows programming, though that may not be a good thing . C interfaces to everything. Just use Carbon Events to intercept "raw key down", "raw key up" and the various mouse events. Apple has reasonably simple samples.

* Cocoa -- requires you to learn Objective C (not that that's hard). Perhaps slightly more difficult than Carbon to shoehorn into an existing framework, though. The event mechanism wasn't really designed to be run "by hand". The Omni Group has good sample code.

* HID -- mouse and keyboard control are only available in Jaguar & later. This is as low-level as you can get, so you won't be getting character codes or mouse scaling. The documentation for HID is also worse than the other APIs.

Its a library called Lightweight Gaming Library which sits on to of OpenGL and has a binding to Java.

Now here is my first silly question (yeah, I'm trying to get this port out of the way so I can start coding on the Mac through the engine). I have an interface that needs to be implemented that looks like Keyboard.poll(). Basically this means that at a given moment I want to be able to look at the condition of the keyboard. This means that I cannot use the standard event handler of the system as it is controlling dispatch of the events and the engine will just poll whenever the API calls for that. Currently I'm looking at implementing that with:

// Raise the number 2 by the power of
// the value of bitInIndex to determine
// the number we should test in the keymap
// to see if the key was pressed. The left
// shift operator (<<) performs a multiply by 2.
SInt16 keyTestValue;
keyTestValue = 1 << bitInIndex;

Okay, I'm gathering the keycodes from the operating system using GetKeys() and I'm getting the KeyMap structure back, but I'm having some trouble figuring out how to translate this into actual keys.

What I want to be able to do is take the KeyMap structure and get the keycodes of the keys that have been pressed so I can turn that into return codes for the library. The SDK says this is defined as an array[128] of booleans?

I can't, however, find a keymap listing that tells me what all 128 of these keys are. Can someone point me to the Apple Keycode listing?

By the way, be careful about using keycodes directly. They vary depending on the kind of keyboard you have, and while most recent keyboards have had the same layout, there's no guarantee that this will always be the case. Even if the keycodes are the same, the keymap (the character each key generates) changes for different languages. You need to be able to tell the player what the name of a key is for their specific keyboard, not just what it says on yours!

I'm using Apple's iGetKeys source for this kind of thing. It's a set of simple wrapper functions for GetKeys() that handle things like remapping keycodes onto displayable characters. You still need to provide a configuration dialog or something, of course, but iGetKeys could be used to give the player feedback about the keys they're selecting.