Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Buggy shift key behavior with numpad

This thread is locked; no one can reply to it. rss feed Print
Buggy shift key behavior with numpad
Edgar Reynaldo
Member #8,592
May 2007
avatar

Hello mi compadres!

I have discovered somewhat buggy behavior in the keyboard handling in Allegro 5.1.13. I don't know if it persists in 5.2.0 or not, so I would appreciate someone who already has 5.2.0 installed to test the following code :

Edit - I am on Windows 10 using MinGW 4.8.1

#SelectExpand
1#include "allegro5/allegro.h" 2 3#include <cstdio> 4 5int main(int argc , char** argv) { 6 7 if (!al_init()) { 8 return 1; 9 } 10 11 if (!al_install_keyboard()) { 12 return 2; 13 } 14 15 16 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED); 17 18 int width = 800; 19 int height = 600; 20 21 ALLEGRO_DISPLAY* display = al_create_display(width , height); 22 23 al_clear_to_color(al_map_rgb(0,0,255)); 24 al_flip_display(); 25 26 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 27 28 ALLEGRO_TIMER* timer = al_create_timer(1.0/10.0); 29 30 al_register_event_source(queue , al_get_keyboard_event_source()); 31 al_register_event_source(queue , al_get_timer_event_source(timer)); 32 al_register_event_source(queue , al_get_display_event_source(display)); 33 34 35 bool keys_down[ALLEGRO_KEY_MAX]; 36 memset(&keys_down , 0 , sizeof(keys_down)); 37 38 39 int npkeycode[10] = { 40 ALLEGRO_KEY_PAD_0, 41 ALLEGRO_KEY_PAD_1, 42 ALLEGRO_KEY_PAD_2, 43 ALLEGRO_KEY_PAD_3, 44 ALLEGRO_KEY_PAD_4, 45 ALLEGRO_KEY_PAD_5, 46 ALLEGRO_KEY_PAD_6, 47 ALLEGRO_KEY_PAD_7, 48 ALLEGRO_KEY_PAD_8, 49 ALLEGRO_KEY_PAD_9 50 }; 51 52 const char* npkeycodestr[10] = { 53 "ALLEGRO_KEY_PAD_0", 54 "ALLEGRO_KEY_PAD_1", 55 "ALLEGRO_KEY_PAD_2", 56 "ALLEGRO_KEY_PAD_3", 57 "ALLEGRO_KEY_PAD_4", 58 "ALLEGRO_KEY_PAD_5", 59 "ALLEGRO_KEY_PAD_6", 60 "ALLEGRO_KEY_PAD_7", 61 "ALLEGRO_KEY_PAD_8", 62 "ALLEGRO_KEY_PAD_9" 63 }; 64 65 al_start_timer(timer); 66 67 bool quit = false; 68 while (!quit) { 69 70 71 72 do { 73 ALLEGRO_EVENT ev; 74 al_wait_for_event(queue , &ev); 75 76 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 77 quit = true; 78 } 79 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { 80 keys_down[ev.keyboard.keycode] = true; 81 printf("%s key pressed\n" , al_keycode_to_name(ev.keyboard.keycode)); 82 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 83 quit = true; 84 } 85 } 86 if (ev.type == ALLEGRO_EVENT_KEY_UP) { 87 printf("%s key released\n" , al_keycode_to_name(ev.keyboard.keycode)); 88 keys_down[ev.keyboard.keycode] = false; 89 } 90 91 if (ev.type == ALLEGRO_EVENT_TIMER) { 92 93 if (keys_down[ALLEGRO_KEY_LSHIFT] || keys_down[ALLEGRO_KEY_RSHIFT]) { 94 printf("Key SHIFT down "); 95 96 bool npkeydown = false; 97 for (int i = 0 ; i < 10 ; ++i) { 98 if (keys_down[npkeycode[i]]) { 99 npkeydown = true; 100 } 101 } 102 if (npkeydown) { 103 printf("Numpad key down : "); 104 for (int i = 0 ; i < 10 ; ++i) { 105 if (keys_down[npkeycode[i]]) { 106 printf("%s down " , npkeycodestr[i]); 107 } 108 } 109 } 110 else { 111 printf("Numpad keys all up "); 112 } 113 } 114 else { 115 printf("Key SHIFT up "); 116 bool npkeydown = false; 117 for (int i = 0 ; i < 10 ; ++i) { 118 if (keys_down[npkeycode[i]]) { 119 npkeydown = true; 120 } 121 } 122 if (npkeydown) { 123 printf("Numpad key down : "); 124 for (int i = 0 ; i < 10 ; ++i) { 125 if (keys_down[npkeycode[i]]) { 126 printf("%s down " , npkeycodestr[i]); 127 } 128 } 129 } 130 else { 131 printf("Numpad keys all up "); 132 } 133 } 134 printf("\n"); 135 } 136 } while (!al_is_event_queue_empty(queue)); 137 138 } 139 return 0; 140}

What it does is track key up and down events, and the state of the left and right shift keys and the numpad keys. I first noticed problems when I was trying to get my program to work and I couldn't figure out why it wasn't.

Here are the problems I have discovered so far. Please test them on your machines with 5.2.0. I will get around to compiling 5.2.0 here in the next few days when I can.

Problems :
1. If you press one SHIFT key and then the other you get both key down events, but then you no longer receive release or press events for either SHIFT key until you release both of them. This happens regardless of the state of the num lock key. This bug does not affect the CTRL or ALT keys.

2. This only happens with num lock on for me, but if you press shift and then a num pad key and are still holding shift, you get an erroneous key up event on the SHIFT key you were holding. If you then release the num pad key while still holding SHIFT, then you get a key down event on the SHIFT key.

I don't know if there are reasons for this behavior, but it seems incorrect to me, as the state of the shift keys are not correctly reported and the key up and key down events are incorrect as well.

torhu
Member #2,727
September 2002
avatar

Well, the first issue is because Allegro uses the regular Windows API for keyboard input. I made a patch to switch to Raw Input a few years ago, but by using that you would bypass keyboard remappings. IIRC, you can get the best of both worlds by using the DirectInput keyboard API, but that's ridiculously complicated...

Try the executables in the Release folder of the attachement to see the differences between the regular WM_KEYDOWN and WM_KEYUP system, and WM_INPUT.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Go to: