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
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?
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.
Just listen for KEY_CHAR events and process the keycode:
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.
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:
Forget about the keyboard state if you are using events.
Use ALLEGRO_EVENT_KEY_CHAR. The KEY_DOWN event doesn't do what you think.
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 }
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:
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)"
Try adding this before your while loop:
ALLEGRO_DISPLAY *display = al_create_display(640, 480); if (!display) return -1;
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
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).
Ok, so I finally put together something that works ok for me:
I have one last question though. When I replace:
by
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 ?
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.
Ok I see, thank you.