iDevGames Forums
How to use GetKeys() - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: How to use GetKeys() (/thread-7254.html)



How to use GetKeys() - xDexx - Mar 6, 2003 11:12 AM

i have looked over some of the older threads and i am still confused as to how GetKeys() works. this is what i did

KeyMap keymap;

GetKeys(keymap);

what do i do from here? i read a bit that i had to translate it in order to get the keycodes. thanks in advance!
-brett


How to use GetKeys() - NCarter - Mar 6, 2003 11:45 AM

Take a look at Apple's iGetKeys source code. It does the whole thing for you in a way which works with any keyboard layout.

You can find iGetKeys here.


How to use GetKeys() - xDexx - Mar 17, 2003 10:15 AM

hmmm i cant get iGetkeys to compile. would it be possible for someone to tell me what i need to do after:

KeyMap keymap;

GetKeys(keymap);

im not looking for exact code, as i want to do it myself. thanks!
-brett


How to use GetKeys() - NCarter - Mar 17, 2003 11:48 AM

I recall having some problems getting iGetKeys to compile, but I can't remember what I did to fix it. I may have forced it to compile as C++ or something....

Anyway, if you really want to do it yourself, the documentation for GetKeys() is here. Essentially, the KeyMap struct is a bitfield with 128 bits spread over four long ints, and you need to do some bit shifting and masking to choose the right one for the key you're interested in. The index number of the bit is the keycode for the key that was pressed minus one.

If you want a more complete explanation, you might as well read the code in iGetKeys.c. The TestForKeyDown() function is the one you're after.

Whichever way you choose to do it, don't forget that you still need to deal with keyboards which have a different layout to the one you're using. Also, certain combinations of keys cancel each other out if you hold them down at the same time, and this also varies according to the keyboard design.


How to use GetKeys() - xDexx - Mar 18, 2003 03:12 PM

TestForKeyDown is exactly what i needed. i dont know why i didnt see it. thanks for your help! another question tho. lets say im checking for the z key in my game to make a dot move across the screen. when the game is running in the background and i am typeing a document/chatting...etc. and i press the z key my dot will move. why is this? is there a way to check if my game is in the foreground? thanks for all your help.
-brett


How to use GetKeys() - Tobi - Mar 19, 2003 01:05 AM

Here's how I handle this problem:

You declare a global to store the process serial number of your game app.

Code:
ProcessSerialNumber    myGamePSN;

At initialization time you get the PSN of your app:

Code:
MacGetCurrentProcess(&myGamePSN);

And in your game loop you compare the front app number with the number of your app. If it is the same you do keyboard input.

Code:
ProcessSerialNumber    frontPSN;
Boolean        theSame;

//Get front process and compare with your app
GetFrontProcess(&frontPSN);
SameProcess(&frontPSN, &myGamePSN, &theSame);
if (theSame)
{
HandleKeyboard();
}



How to use GetKeys() - NCarter - Mar 19, 2003 01:37 AM

You're right, GetKeys() detects keys all the time, regardless of whether or not your game is in the foreground. Under Mac OS X, it's easy to switch out of a game which isn't running in full screen mode, and you need to write some event handling code to deal with this situation properly.

The way I handle this is to detect kEventAppDeactivated Carbon events, which are issued whenever the game gets switched out, and when I receive one I stop my main game loop and go back to the preferences dialog. The player can then manually resume the game by pressing a 'continue' button.

If you don't want to actually stop your game, you could simply remember whether or not you've been switched out and not check the keyboard when you're in the background. You could also check for events in the kEventClassKeyboard class instead of using GetKeys(), because you'll only be sent those events when you're in the foreground.

If you're not using Carbon events, I think they're called suspend/resume events under the old Event Manager, but check the documentation on this.... I don't know anything about Cocoa, but I imagine it has some similar feature you can use.