PDA

View Full Version : Keyboard Input


imported_Nickolei
2002.07.11, 12:29 PM
I know this is one of those timeless questions that is never really resolved... but from all my reading of various sources I've come to the following conclusion...

Regardless of whether you use GetKeys() or Carbon Events, you are only guaranteed to get 2 keys presses simultaneously, plus any number of the 5 modifier keys.

You may get more, but this depends on the keys that are pressed as well as the specifics of the keyboard.

Except...does the number pad count? (Even though it's a pain for laptop users)

Keyboards are evil indeed. I see why there are practically no 2 player/1 keyboard games.

Nickolei

kainsin
2002.07.11, 02:50 PM
You can get more if you only keep track of key-downs and key-ups. Use Carbon Events and keep a structure of the keys you want to keep track of ( i.e. a structure of booleans to tell whether the key is pressed or not ). Then when you receive a key-down event then update the correct boolean to reflect when the key is down. When you receive a key-up event then do the same thing.

Though...I don't know if pressing more than two keys will result in the correct number of events being called. I'll have to test that out.

OneSadCookie
2002.07.11, 05:00 PM
My experience is that on all modern keyboards (including the ADB one on my old 7300), you can get up to about 10 "real" keys pressed simultaneously. After that, key down events stop arriving.

I'm pretty sure modifier keys are different, and are always reported regardless of what other keys are down, but I have to admit I haven't tested it :)

Anyway, 10 keys is plenty for 3 players on one keyboard, and if modifier keys are really separate, you should be able to have 4 at once no sweat:

A, W, S, D, shift
I, J, K, L, command
arrows, control
numpad, zero or enter

That way, even if all players have two direction keys and action down at once, you should still be noticing all the events...

aarku
2002.07.12, 02:20 PM
Reminds me of the good ole days playing a cutthroat game of Asterax against my brother on our IIci. Whenever we really got ticked off... we'd just slam our hand down across half the keyboard; putting both of our ships out of control for a quick slam into the asteroids. Good times...

Anyways my game uses either keyup/keydown events or InputSprocket (depending on which is selected) and I have had no problems with amount of keys pressed. I'd suggest something like that. (Anyone tried that InputSprocket for OS X thing?)

-Jon

Codemattic
2002.07.13, 11:22 AM
Its a pickle!

The number of keys you can sense is a hardware problem - using getkeys or keydown/up events wont change that. Originally you could only count on two plus the modifiers - dont know what the limit is now. You can pull up the key caps utility and play around - but its going to be different for different brand keyboards.

There are a few keyboards that GetKeys wont work properly on. GetKeys returns the keymap of the last used device - but some keyboards (like ergonomic ones that are broken in two - and some laptop keyboards I think) are actually more than one device.

I think keydown/up events are the way to go personally.

And it gets worse.

What are you going to test those events against. Keycodes? That is different on different models and in different countries. Char value? Again - different countries will return different values - also the char value might be changed by any modifiers that are also being held down, so you might have to do multiple checks for each key.

I think the safest and most inclusive is to use keydown/up events - and provide a settings dialog which asks what the controls should be and records what the keycode and char value of the key the user wants to use for each control is. At least until hid reads keyboards/mice.

hth,
Codemattic

Codemattic
2002.07.13, 11:31 AM
Originally posted by aarku
Reminds me of the good ole days playing a cutthroat game of Asterax against my brother on our IIci. Whenever we really got ticked off... we'd just slam our hand down across half the keyboard; putting both of our ships out of control for a quick slam into the asteroids.

possibly the only good code to come out of Microsoft was MicroSoft Olympics! In their track and field events you ran by hitting two keys for left leg/right leg. So to run I had to hit asasasasasasasasasa as fast as I could - while my brother would have to hit klklklklklklklkl. Sure there was some elbowing - but thats to be expected. We really beat the hell out of that keyboard. What a stupid - fun game

Originally posted by aarku
Good times...
indeed!

-Codemattic

jeckil
2002.08.15, 08:58 PM
i have started to use the event handler for autoKey: and keyDown and i dont know how to trap when the user presses shift or control or the apple key, option or even the caps lock, can someone help on this? Also, when i try to get the function keys they display the same code for all of them i dont know what is that about yet...

P.S. im using carbon right now for my game

THanks!


Jeckil

:cool:

OneSadCookie
2002.08.15, 09:27 PM
If you're using carbon events you can intercept the modifiers changed event... otherwise, I don't know. You can always get the state of the modifier keys, but that doesn't tell you whether they've changed...

ededed
2002.08.17, 03:55 PM
I think its the same on X as in 9 but usually you can be really evil in two player games if you hold down five keys then the other player cant move and gets shot or runover. A really great thing is if you plug two mice into a USB keyboard and have a contest to pull the scrollbar to the top or bottom of a really long file and someone else has to pull the scrollbar to the top. Also a good system for keyevents if to have:

# define leftKey = ....
# define rightKey = ....
....

BOOL leftKey, rightKey, upkey.......;

- (void)animate
{
[sprite move:leftKey :rightKey................];
}

- (void)keyDown:(NSEvent*........
if(..... == myLeftKey) leftKey = YES;
else if(..... == myRightKey) rightKey = YES;
...
}

- (void)Up:(NSEvent*........
if(..... == myLeftKey) leftKey = NO;
else if(..... == myRightKey) rightKey = NO;
...
}

// and the sprite could go:

if(left && !right) [self left]
else if(right && !left) [self right]

Josh
2002.08.17, 08:20 PM
You may want to disable smilies...

Originally posted by ededed
A really great thing is if you plug two mice into a USB keyboard and have a contest to pull the scrollbar to the top or bottom of a really long file and someone else has to pull the scrollbar to the top.I think ededed has WAY too much free time on his hands... :p

ededed
2002.08.19, 12:24 PM
No just two many mice on my hands.

By the way did you get that thing working?