Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » detecting multiple keys pressed

This thread is locked; no one can reply to it. rss feed Print
detecting multiple keys pressed
arkaydo
Member #2,549
July 2002
avatar

hello everyone ::)

i'm new to allegro and wondering how to detect multiple keys pressed at the same time (e.g. cursorUp+cursorRight together)

Justin_W
Member #655
September 2000
avatar

...
install_keyboard();
...
if(key[KEY_UP] && key[KEY_RIGHT])
  ...

See documentation under keyboard routines for more details.

arkaydo
Member #2,549
July 2002
avatar

..oh that simple, shame on me :))
thx a lot

Irrelevant
Member #2,382
May 2002
avatar

On most keyboards, some patterns of keys won't be detected when pressed all at once. On mine, par example, it can't detect Down when I hold Up & Left. Also, it can't detect Y, U, or J when I hold down Z, X, & C.

This isn't usually a problem, though. Only if holding down several keys is vital to gameplay.

<code>//----------------//</code>Here be l33tsp33x0rz.

Thomas Harte
Member #33
April 2000
avatar

To expand on what Irrelevant said - the problem he describes in inherent in the PC hardware, affects different keyboards differently, and is something all PC games must deal with. It is not an Allegro problem.

Irritatingly, this keyboard cannot do forward, left and left alt at the same time, which usually gets me killed in Doom!

Tuomas Korppi
Member #2,474
June 2002
avatar

The problem with not registering multiple key presses is a major problem even if keys need not be held down - when trying to implement a two-player game so that both players share the keyboard.

At least according to my experience, it's possible to find keys that work if keys need not be held down, and both players agree to be polite and not to abuse the keyboard. Anyway, there's always the unfair strategy of helding all your control keys down, and locking the keyboard from the opponent. According to my experience, that kind of behaviour is not always deliberate, but inexperienced players tend to panic when they're losing and hold down keys indisciminately.
How different are keyboards in this respect (i.e. how much does it vary from one keyboard to another, which combinations of keys lock the keyboard)? What are the details of hardware design that cause this problem?

23yrold3yrold
Member #1,134
March 2001
avatar

I've never seen someone mention this problem when dealing with joysticks; can you hold down all the buttons you want on those?

And no one answered me when I asked for opinions on the Wingman Rumble and Wingman Precision that one time :'(

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

the_y_man
Member #1,770
December 2001
avatar

Irrelevant, did you know that there is a bug in your signature....i think.....

Justin_W
Member #655
September 2000
avatar

Keyboard masking and ghosting occurs because keyboard manufacturers want to cut costs by having less circuitry. Instead of each key being tied to an individual line that then gives a unique signal to your port, a matrix is used to cheat. To see how this is done exactly with nice diagrams and such, see this link.

Joysticks/pads do not have this problem because the input is considered more critical so they spend the extra half a penny to directly connect each line to a button (thus each has a distinctly unique ID). The PS2 port on a computer is perfectly capable of receiving all the keys pressed at once, and in fact, there are some keyboards made to allow for just that - just not most of them. :)

Tuomas Korppi
Member #2,474
June 2002
avatar

Thanks Justin_W for your link. Now, the next question is that how is the thingy implemented that takes the results of scanning lines as a parameter and returns the information about key presses? Is it hard-wired on the keyboard or is there a software doing that on the computer? If the latter is true (I suppose not:'( ) you could replace the routine with something optimized for your game. You could use the information that only the game control keys are pressed to guess correct input.

In an old Turbo Pascal program my keyboard beeps and gets locked for half a second if certain combinations of keys are pressed. Is this due to the hardware problem, or some kind of software thing to avoid ghosting?

Is the way of dividing the keyboard into a matrix of rows and colums the same on all keyboards, and what is it exactly? (I mean that are 2ws in the same column or 3wa, in which column is the space bar etc...)

Justin_W
Member #655
September 2000
avatar

Keyboards vary greatly in exactly how this is implemented. Some (actually usually older ones) don't have any ghosting or masking issues at all. Those are nice ones, but rare.

The keyboard sends a signal to the PC once per key event. When a key is pressed, it sends the signal. It does not keep sending a signal, it lets the PC remember that that key is down. (Ever have "stuck" keys on a PC and wonder why? The PC may have missed a crucial sig from the keyboard.) When a key is released, it sends a signal.

A keyboard buffer implemented in the OS is where these key events are stored. Windows (or DOS) grabs 'em up and when your program requests them, the OS spits them out. Unfortunately, the OS does its job really, really well. It is the hardware that gives us the problems.

You can buy keyboard control chips that, with a minor keyboard hack, will eliminate the problem, but let's face it - this is waaay beyond most gamers realm of equipment.

One of the best solutions you can provide is to allow custom key selections. This way, if certain keys don't like each other (and which keys those are are completely dependent upon a particular keyboard), the player may simply pick other keys that do work.

If you ever build an arcade machine and use a keyboard hack, try out several keyboard before hand if you can to figure out which ones work best. Then, you can usually get at least two players (with careful key layout planning) on a keyboard with ultra good responses and one or more on a joypad/stick as well... Like I did, which is why I looked into all this junk. ;)

Go to: