Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] user keyboard input in int array ?

This thread is locked; no one can reply to it. rss feed Print
[A5] user keyboard input in int array ?
Inquisiteur
Member #12,428
December 2010

Hi,

I took a glance at the keyboard tutorial. Unfortunatly I was not able to derive from it some way to gather user keyboard input in an array of int.

My goal is:

1. Display a textbox with two fields - The fields are coordinate X/Y (I know how to do that)
2. The user can input numbers in both fields (arrays)
3. While he is pushing numbers on his keyboard the X coordinate array is filled. (then the second)

Right now, and thanks to the tutorial, I know how to make the software recognize which key is pushed, however I would have to enumerate all allegro keycodes for that.

What I want is to store every number push, whatever the number is.

Can anybody englighten me ?

Thanks

Matthew Leverton
Supreme Loser
January 1999
avatar

Do you want an array like key[256] that is true if that key code is pressed, or an array that indicates a stream of characters that the user has entered?

Inquisiteur
Member #12,428
December 2010

Thanks for your reply,

I need an array that catches a stream of numbers, not their code.

So basically:

"Please input X coordinates"

--> user input 72

Software allocate the number 72 to variable CoordX.

In fact I probably don't need an array at all...I just need to stock the user keyboard input, whatever it is (for now numbers) in some kind of int variable, I dont know what A5 function would allow me to do that.

Matthew Leverton
Supreme Loser
January 1999
avatar

Just listen for KEY_CHAR events and process the keycode:

#SelectExpand
1int number = 0; 2 3while (some_loop) 4{ 5 // assume you just received a KEY_CHAR event 6 7 if (keycode >= '0' && keycode <= '9') 8 { 9 int digit = keycode - '0'; // convert from ASCII code to integer 0-9 10 number = number * 10 + digit; // shift the current number left and add the new integer 11 } 12 13 else if (keycode == ALLEGRO_KEY_BACKSPACE) 14 { 15 number = number / 10; // remove the right most digit 16 } 17 18 // print number to screen 19}

The result is that number holds an integer as entered by the keyboard.

Edit: actually I think you need to use the unichar property for the digits. i.e.:

if (unichar >= '0' && unichar <= '9')
{
  int digit = unichar - '0';
}

But the same principle applies.

Inquisiteur
Member #12,428
December 2010

Thank you very much, I'm gonna try that today.

Update-------

For some reason I can't even have the software recognize that I am pushing a keyboard key...

I have seen two other posts in the forum about that, however they did not help me solve my issue. I have seen that this has been solved by implementing a timer, assuming I don't want a timer, is there a way to fix the code below ?

Sry to ask again for your help...

Here's the code:

#SelectExpand
1 2#include <allegro5/allegro.h> 3#include <stdio.h> 4 5int main(int argc, char *argv[]) { 6 al_init(); 7 al_install_keyboard(); 8 9 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 10 ALLEGRO_KEYBOARD_STATE keys; 11 event_queue = al_create_event_queue(); 12 al_register_event_source(event_queue, al_get_keyboard_event_source()); 13 14 15 while(1){ 16 ALLEGRO_EVENT ev; 17 al_wait_for_event(event_queue, &ev); 18 al_get_keyboard_state(&keys); 19 20 if(ev.type == ALLEGRO_EVENT_KEY_DOWN){ 21 if(al_key_down(&keys,ALLEGRO_KEY_TAB)){ 22 printf("keyboard key pressed down"); 23 } 24 } 25 } 26 return 0; 27}

Matthew Leverton
Supreme Loser
January 1999
avatar

  1. Forget about the keyboard state if you are using events.

  2. Use ALLEGRO_EVENT_KEY_CHAR. The KEY_DOWN event doesn't do what you think.

  3. Use ev.keyboard.unichar for printable characters; use keycode for special keys

if (ev.type == ALLEGRO_EVENT_KEY_CHAR && ev.keyboard.keycode == ALLEGRO_KEY_TAB)
{
  // tab was pressed
}

Inquisiteur
Member #12,428
December 2010

Thanks again for your reply.

I am starting to wonder if there would be some kind of incompatibility coming from my keyboard ? I have been trying a lot of variation with absolutely no other results than a black console...

Here's the code:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3 4int main(int argc, char *argv[]) { 5 6 al_init(); 7 8 al_install_keyboard(); 9 10 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 11 12 event_queue = al_create_event_queue(); 13 14 al_register_event_source(event_queue, al_get_keyboard_event_source()); 15 16 while(1){ 17 18 ALLEGRO_EVENT ev; 19 20 al_wait_for_event(event_queue, &ev); 21 22 if (ev.type == ALLEGRO_EVENT_KEY_CHAR && ev.keyboard.keycode == ALLEGRO_KEY_TAB) 23 // if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 24 { 25 printf("keyboard key pressed down"); 26 } 27 } 28}

Can anyone compile this and tell me if it works ... ?

The // if (ev.type == ALLEGRO_EVENT_KEY_DOWN) didn't work either.

The console is simply not registering any event when I am pushing the TAB, or any other button when using "if (ev.type == ALLEGRO_EVENT_KEY_DOWN)"

LennyLen
Member #5,313
December 2004
avatar

Try adding this before your while loop:

  ALLEGRO_DISPLAY *display = al_create_display(640, 480);
  if (!display) 
      return -1;

Inquisiteur
Member #12,428
December 2010

I can't believe this was the issue. It worked !

So basically if there are no displays to be selected, I won't be able to interact with the "allegro based" software.

Logical and weird at the same time...

Thanks to both of you guys for your time.

Regards,

Inquisiteur

Matthew Leverton
Supreme Loser
January 1999
avatar

Allegro doesn't support console mode like you might expect. It receives input from events sent to the graphical display window by the OS. So without such a window, you won't ever get any keyboard input (among other things).

Inquisiteur
Member #12,428
December 2010

Ok, so I finally put together something that works ok for me:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3 4 void command_console(ALLEGRO_EVENT_QUEUE *event_queue){ 5 6 printf("Please input X coordinates - press SPACE to confirm coordinates\n"); 7 8 int number = 0; 9 10 while(1){ 11 12 ALLEGRO_EVENT c_ev; 13 al_wait_for_event(event_queue, &c_ev); 14 15 if (c_ev.type == ALLEGRO_EVENT_KEY_CHAR) 16 { 17 if (c_ev.keyboard.unichar >= '0' && c_ev.keyboard.unichar <= '9') 18 { 19 int digit = (c_ev.keyboard.unichar - '0'); 20 number = number * 10 + digit; 21 printf("Number is %d\n", number); 22 } 23 else if (c_ev.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 24 { 25 number = number / 10; 26 printf("Number is %d\n", number); 27 } 28 else if (c_ev.keyboard.keycode == ALLEGRO_KEY_SPACE) 29 { 30 printf("coordinates are %d\n", number); 31 } 32 } 33 } 34 } 35///////////////////////////////////////----------------MAIN LOOP 36int main(int argc, char *argv[]) { 37 38 al_init(); 39 40 al_install_keyboard(); 41 42 if(al_is_keyboard_installed()==true) 43 { 44 printf("keyboard installed successfully\n"); 45 } 46 47 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 48 49 event_queue = al_create_event_queue(); 50 51 al_register_event_source(event_queue, al_get_keyboard_event_source()); 52 53 ALLEGRO_DISPLAY *display = al_create_display(640, 480); 54 if (!display) 55 return -1; 56 57 ALLEGRO_EVENT ev; 58 al_wait_for_event(event_queue, &ev); 59 60 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_TAB) 61 { 62 command_console(event_queue); 63 } 64}

I have one last question though. When I replace:

#SelectExpand
1 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_TAB) 2 { 3 command_console(event_queue); 4 }

by

#SelectExpand
1 if (ev.type == ALLEGRO_EVENT_KEY_CHAR) 2 { 3 command_console(event_queue); 4 }

The console closes down as soon as I press a character on the keyboard. So basically the function "command_console" is not launched. Why is ALLEGRO_EVENT_KEY_DOWN working and not ALLEGRO_EVENT_KEY_CHAR in this case ?

Matthew Leverton
Supreme Loser
January 1999
avatar

The key events typically look like:

  • KEY_DOWN

  • KEY_CHAR

  • KEY_UP

The first two happen simultaneously. You are only checking for the first one in your program. If you were to loop through all available events, you'd find a CHAR in the queue.

Inquisiteur
Member #12,428
December 2010

Ok I see, thank you.

Go to: