How to use GetKeys()

xDexx
Unregistered
 
Post: #1
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
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #2
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
xDexx
Unregistered
 
Post: #3
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
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #4
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.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
xDexx
Unregistered
 
Post: #5
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
Quote this message in a reply
Tobi
Unregistered
 
Post: #6
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();
}
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #7
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.

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
  Beginning Game Programmer, Question about GetKeys() Why37 2 3,400 Jul 21, 2002 02:47 PM
Last Post: codemattic