Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Keyboard key up detection

This thread is locked; no one can reply to it. rss feed Print
Keyboard key up detection
Fredrik Beckman
Member #15,374
November 2013

[edit] Should be moved to programming questions, I guess... Perhaps help with that from a moderator maybe..? (Sorry)

Hi,
I'm having some issues with the keyboard key-up detection. The problem is that it seems like when I release ARROWUP and ARROWLEFT keys at the same time, the program doesn't detect that the ARROWLEFT-key is released. It behaves the same if I use keyboard events as if not.

The thing is that I can't repeat this with any other key combination - just with up/left keys. And it's quite easy to repeat. I'm beginning to think it's a hardware related problem, but does anyone have a solution to the problem?

I'm using allegro 5, and part of the code is here:

#SelectExpand
1 /* Start timer */ 2 al_start_timer(timer); 3 4 while(doexit == false) 5 { 6 ALLEGRO_EVENT ev; 7 al_wait_for_event(event_queue, &ev); 8 9 if(ev.type == ALLEGRO_EVENT_TIMER) 10 { 11 al_get_keyboard_state(&kbdstate); 12 13 if (al_key_down(&kbdstate, ALLEGRO_KEY_ESCAPE)) 14 { 15 /* Quit */ 16 break; 17 } 18 19 if (al_key_down(&kbdstate, ALLEGRO_KEY_UP)) 20 { 21 /* Forward */ 22 playerTank.y -= cos(playerTank.angle); 23 playerTank.x += sin(playerTank.angle); 24 } 25 26 if (al_key_down(&kbdstate, ALLEGRO_KEY_DOWN)) 27 { 28 /* Reverse */ 29 playerTank.y += cos(playerTank.angle); 30 playerTank.x -= sin(playerTank.angle); 31 } 32 33 if (al_key_down(&kbdstate, ALLEGRO_KEY_LEFT)) 34 { 35 /* Turn left */ 36 playerTank.angle -= (pi / 60); 37 if (playerTank.angle <= -(2 * pi)) 38 { 39 playerTank.angle = 0.0; 40 } 41 } 42 43 if (al_key_down(&kbdstate, ALLEGRO_KEY_RIGHT)) 44 { 45 /* Turn right */ 46 playerTank.angle += (pi / 60); 47 if (playerTank.angle >= (2 * pi)) 48 { 49 playerTank.angle = 0.0; 50 } 51 } 52 } 53 54 if (redraw && al_is_event_queue_empty(event_queue)) 55 { 56 redraw = false; 57 58 al_set_target_backbuffer(display); 59 al_clear_to_color(al_map_rgb(255, 255, 255)); 60 61 al_draw_rotated_bitmap(playerTank.bitmap, 12, 12, playerTank.x, playerTank.y, playerTank.angle, 0); 62 if (playerShot.active) 63 { 64 al_draw_rotated_bitmap(playerShot.bitmap, 2, 2, playerShot.x, playerShot.y, playerTank.angle, 0); 65 } 66 67 al_flip_display(); 68 } 69 70 }

Regards
Fredrik

André Silva
Member #11,991
May 2010
avatar

Well, the most common way to detect key presses and releases is through keyboard events, but you said you tried that already... You made sure to also detect ALLEGRO_EVENT_KEY_UP events, right?

Also, is the problem only when up and left are pressed at the same time? Keyboards are pretty picky when you press multiple keys, and several combinations block all keyboard input. It makes sense: if you fall asleep on your keyboard while writing a document, you don't want to wake up to 671 pages of "g", but it does suck for playing games.
Now, I know up and left are amongst the most jarring examples, but... That only seems to happen when I press up, left, and another key. Up and left alone normally work fine without blocking. Try it on another machine, I guess.
I tried your code on my machine, and up+left works fine; the tank goes forward and rotates to the left.

Fredrik Beckman
Member #15,374
November 2013

Hi,
thanks for a quick reply,

Yes, I had ALLEGRO_EVENT_KEY_UP events in the first try. And after I read your reply I plugged in an external keyboard in my laptop. With the external keyboard I can't repeat the problem.

I should have done that immediately, when I first thought it was hw-related.

Anyway - problem solved (or.. maybe not.. ::) )

/Fredrik

Kris Asick
Member #1,424
July 2001

Yeah, the code looks like it should be working, so it probably is hardware related. Many keyboards don't like it when certain combinations of keys are pressed, and even as few as two keys can be enough to mess things up. :-/

To that end, it's always good to allow players to use different combinations and set their own controls. AWDS is common nowadays, but the numeric keypad is always a good standby too for a game that doesn't have any mouse controls.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Go to: