Porting Help: Input Devices

Post: #1
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.

Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
The game sprockets are all but dead on Mac OS X.

You have 3 choices:

* Carbon -- probably feels most like Windows programming, though that may not be a good thing Wink. 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.

What library are you porting?
Quote this message in a reply
Post: #3
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:

EventRef theEvent;
EventTargetRef theTarget;

theTarget = GetEventDispatcherTarget();

    while  (ReceiveNextEvent(0, NULL,kEventDurationForever,true,
            &theEvent)== noErr)

            SendEventToEventTarget (theEvent, theTarget);

The only problem is that this doesn't give me the facility to only get keyboard events, that and I'm very skeptical of this kEventDurationForever parameter.
Quote this message in a reply
Posts: 79
Joined: 2002.08
Post: #4
I'd say Getkeys() and Carbon is what you are looking for then.
The function below is what I'm using. You pass in the raw key code you want to check for. You would use it like this:

    KeyMap    currentKeyboardState;

    ::GetKeys( currentKeyboardState );

    if ( KeyWasPressed( keyCode, currentKeyboardState ))
     do something

//        KeyWasPressed
//    Test if keyCode key was pressed

    SInt16                        keyCode,
    KeyMap                        theKeyboard )
    SInt16    keymapIndex;
    SInt16    bitInIndex;
    SInt16    keymapEntrySize = 8;

    // The keymap consists of 16 8-bit numbers.
    // Find which of the 16 numbers to check.
    keymapIndex = keyCode / keymapEntrySize;

    // Find which bit to check in the keymap index.
    bitInIndex = keyCode % keymapEntrySize;

    // 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;

    // Get the value of the keymap entry
    // we want to test
    char* startOfKeymap =  (char *) &theKeyboard[0];
    char keymapEntryValue = * (startOfKeymap + keymapIndex);

    // Compare the keymap entry with the value
    // of the key we want to test to determine
    // if the player pressed the key.
    return ((keymapEntryValue & keyTestValue) != 0);
You can of course modify it to suit your needs.

Quote this message in a reply
Post: #5
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?
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #6
Apple doesn't seem to keep a list, but one was posted on this forum just a couple of days ago. You should be able to find it by searching...
Quote this message in a reply
Posts: 365
Joined: 2002.04
Post: #7
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  List of connected devices? thaeez 11 7,087 Jan 26, 2007 01:11 PM
Last Post: Shunter