Hi there,
How do I prevent the user from holding down more than one key simultaneously?
Cheers,
Some kind of electrocuting element in his keyboard?
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.
, happy Monday ...
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?
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...
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?
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.
I've tried Rest(), may be set keyboard instead?
No.
Ok, thank you very much, that helps! I appreciate it ...
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.
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 | |
3 | int 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 | } |
20 | END_OF_MAIN() |
Yep, it was so obvious, it must be Monday . Thx again, FMC, Miran, CGamesPlay.