Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Prevent user from pressing more than one key

This thread is locked; no one can reply to it. rss feed Print
Prevent user from pressing more than one key
Gia Nguyen
Member #6,912
February 2006

Hi there,

How do I prevent the user from holding down more than one key simultaneously?

Cheers,

X-G
Member #856
December 2000
avatar

Some kind of electrocuting element in his keyboard?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

miran
Member #2,407
June 2002

You can't. Unless you go over to each user and cut off nine of their fingers. And even then they might be able to press two adjacent keys simultaneously with the one remaining finger.

--
sig used to be here

Gia Nguyen
Member #6,912
February 2006

8-), happy Monday ... 8-)

extern int (*keyboard_callback)(int key);

If set, this function is called by the keyboard handler in response to every keypress. It is passed a copy of the value that is about to be added into the input buffer, and can either return this value unchanged, return zero to cause the key to be ignored, or return a modified value to change what readkey() will later return.

Allow me to try again, I'm asking about this function above. Is there an example of how this can be used to ignore certain keys, or process each key sequentially if more than one are pressed at the same time?

FMC
Member #4,431
March 2004
avatar

I don't know if it is the case but usually you can do:

if(key[KEY_SPACE])do_stuff();
else if(key[KEY_DOWN])do_stuff();
else if(key[KEY_UP])do_stuff();
else if(key[KEY_RIGHT])do_stuff();
else if(key[KEY_LEFT])do_stuff();

in this way you only react to ONE keypress.

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

miran
Member #2,407
June 2002

Quote:

Allow me to try again, I'm asking about this function above. Is there an example of how this can be used to ignore certain keys, or process each key sequentially if more than one are pressed at the same time?

What exactly are you trying to do? If you're trying to fix your football game, you're going about it the wrong way. Just use the key[] array as FMC said and organize your code better...

--
sig used to be here

Gia Nguyen
Member #6,912
February 2006

if(key[KEY_SPACE])do_stuff();
else if(key[KEY_DOWN])do_stuff();
else if(key[KEY_UP])do_stuff();
else if(key[KEY_RIGHT])do_stuff();
else if(key[KEY_LEFT])do_stuff();

Yes, but if the user holds the Up key and the Right key at the same time, you get diagonal movement instead of Up, then Right, or Right and Up. I've tried Rest(), may be set keyboard instead?

miran
Member #2,407
June 2002

Quote:

Yes, but if the user holds the Up key and the Right key at the same time, you get diagonal movement instead of Up, then Right, or Right and Up.

No. Notice the elses.

Quote:

I've tried Rest(), may be set keyboard instead?

No.

--
sig used to be here

Gia Nguyen
Member #6,912
February 2006

Ok, thank you very much, that helps! I appreciate it ... ::)

CGamesPlay
Member #2,559
July 2002
avatar

does help. I'll say the same thing with different words:

The "else" means that only ONE of those actions will happen, the rest will be skipped.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

FMC
Member #4,431
March 2004
avatar

Quote:

Yes, but if the user holds the Up key and the Right key at the same time, you get diagonal movement instead of Up, then Right, or Right and Up. I've tried

Try out this code:

1#include <allegro.h>
2 
3int main(){
4 allegro_init();
5 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0,0);
6 install_keyboard();
7 
8 int player_x = 200, player_y = 300;
9 while(!key[KEY_ESC]){
10 vsync();
11 clear_bitmap(screen);
12 circlefill(screen, player_x, player_y, 20, makecol(255,0,0));
13
14 if(key[KEY_DOWN])player_y++;
15 else if(key[KEY_UP])player_y--;
16 else if(key[KEY_RIGHT])player_x++;
17 else if(key[KEY_LEFT])player_x--;
18 }
19 }
20END_OF_MAIN()

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Gia Nguyen
Member #6,912
February 2006

Yep, it was so obvious, it must be Monday :P :D. Thx again, FMC, Miran, CGamesPlay.

Go to: