Prevent user from pressing more than one key
Gia Nguyen

Hi there,

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

Cheers,

X-G

Some kind of electrocuting element in his keyboard?

miran

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.

Gia Nguyen

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

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.

miran
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...

Gia Nguyen

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
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.

Gia Nguyen

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

CGamesPlay

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.

FMC
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()

Gia Nguyen

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

Thread #572661. Printed from Allegro.cc