• Updated 2023-07-12: Hello, Guest! Welcome back, and be sure to check out this follow-up post about our outage a week or so ago.

Reading more than two keys?

onlyonemac

Well-known member
Hi!

As I'm sure you are aware, the Macintosh system will only send keyDown events for up to two keys. If one presses more than two keys, they will not receive any more events.

Is there a way to somehow read more keys at a time, even an unlimited number?

Thanks,

onlyonemac

 

bbraun

Well-known member
Inside Macintosh I said:
If you wish to periodically inspect the state of the keyboard or keypad—say, while the mouse button is being held down—use the procedure GetKeys; this procedure is also the only way to tell whether a modifier key is being pressed alone.
Inside Macintosh Toolbox Essentials said:
In addition to getting keyboard events when the user presses or releases a key, you can directly read the keyboard (and keypad) using the GetKeys procedure.
 

onlyonemac

Well-known member
What about the special characters that require option-shift-letter? That'd be 3 keys, no?
Those aren't keyDown events; they're modifiers. They're passed differently.
Inside Macintosh I said:
If you wish to periodically inspect the state of the keyboard or keypad—say, while the mouse button is being held down—use the procedure GetKeys; this procedure is also the only way to tell whether a modifier key is being pressed alone.
Inside Macintosh Toolbox Essentials said:
In addition to getting keyboard events when the user presses or releases a key, you can directly read the keyboard (and keypad) using the GetKeys procedure.
I'd thought of that, but all I can find out is that it needs some sort of a "mask", and I don't know how the heck one is supposed to know what that means.
 

bbraun

Well-known member
It is all documented in the two books I mentioned, and probably numerous places elsewhere. There is no mask involved.

 

onlyonemac

Well-known member
You can use the GetKeys procedure to obtain the current state of the keyboard.
PROCEDURE GetKeys (VAR theKeys: KeyMap);

theKeys Returns the current state of the keyboard, including the keypad, if any.

The GetKeys procedure returns this information using the KeyMap data

type.

TYPE KeyMap = PACKED ARRAY[0..127] OF Boolean;

Each key on the keyboard or keypad corresponds to an element in the

KeyMap array. The index for a particular key is the same as the key’s

virtual key code minus 1. For example, the key with virtual key code 38

(the “J” key on the Apple Keyboard II) can be accessed as KeyMap[37] in

the returned array. A KeyMap element is TRUE if the corresponding key is

down and FALSE if it isn’t. The maximum number of keys that can be

down simultaneously is two character keys plus any combination of the

five modifier keys.
So it looks like even that won't work.
Oh well, I suppose it's not possible then.

 

olePigeon

Well-known member
How'd they handle it with games, then? I'm pretty sure in games like Wolfenstein 3D or Marathon, you were pressing more than 2 keys.

 

MacJunky

Well-known member
I remember playing Carmageddon and having problems trying to use too many keys. Same for STVEF on a mac IIRC.

 

onlyonemac

Well-known member
I think they allow two keys because that's enough for key rollover (what happens when fast typists type too fast :lol: ).

Other than that there's no real need for more keys from an office point of view, which was, after all, what Macs were designed for...

 

bbraun

Well-known member
Have you tried GetKeys() or are you going off your reading of the documentation?

Using this code, at least with my ps2/adb adapter, is showing at least 5 non-modifier keys down simultaneously:

Code:
void main(void) {
   KeyMap keys;
   while(1) {
       GetKeys(keys);
       printf("%lx %lx %lx %lx\n", keys[0], keys[1], keys[2], keys[3]);
   }
}
It is my opinion that the 2 non-modifier keys down simultaneously is a bit of a misrepresentation. The ADB protocol is only capable of conveying 2 key events in a single packet. A key event is either a key up or a key down. However, the system keeps track of the events and updates the keymap. So if you do 3 key down events, it will be distributed over two ADB packets, so there's no way they can be received "simultaneously", but the key down events are (eventually) received and the key map is updated. Modifier keys are called out separately because their state can be retrieved through a different ADB packet where all modifier keys can be retrieved in one transaction.

There's all kinds of caveats to this, since the Extended keyboard and later are capable of distinguishing between right and left modifier keys, but the ADB packet where all modifier keys are retrieved isn't capable of representing that distinction. The power key is also special since it can be the only key represented in a single ADB key event packet (it counts as 2 keys). And then there's the limitations of the keyboard hardware, which may have limitations on how many simultaneous key presses it is capable of detecting, or buffering for subsequent ADB transactions.

But, if you actually try the GetKeys() call, it does report more than 2 non-modifier keys being held down simultaneously.

 
Top