|
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, 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: 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
|
André Silva
Member #11,991
May 2010
|
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.
|
Fredrik Beckman
Member #15,374
November 2013
|
Hi, 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) |
|