![]() |
|
[AL 5] Keyboard Inputs |
Kisscool
Member #12,505
January 2011
|
Hi, I'm just making a litlle Tetris game using Allegro. You'll agree with me : it' easier to play with the keyboard Thats how I tried to use the keyboard with my program ( apparently, it fail ) : Firs of all, I installed the keyboard with : al_install_keyboard() And I control if LEFT is pressed to output a sentence : if (al_key_down(state,ALLEGRO_KEY_LEFT)){ But last one throw me an error message, telling me that I violate something T_T Thanks for your future support ! |
SiegeLord
Member #7,827
October 2006
![]() |
Don't use ALLEGRO_KEYBOARD_STATE, use events. See this tutorial. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
X-G
Member #856
December 2000
![]() |
And completely aside from whether you use _STATE or events, you didn't initialize the state to anything useful, you just left it null... -- |
kenmasters1976
Member #8,794
July 2007
|
When I started using Allegro 5, I used ALLEGRO_KEYBOARD_STATE like this: ALLEGRO_KEYBOARD_STATE key_state; ... if (al_key_down(&key_state, ALLEGRO_KEY_LEFT)){ // Do something } However, once you learn to use the new event system it is quite easy so I agree with SiegeLord, you should use events.
|
Matthew Leverton
Supreme Loser
January 1999
![]() |
You must do this: ALLEGRO_KEYBOARD_STATE state; al_get_keyboard_state(&state); At that point, you can use the al_key_down function. You need to update (get) the state anytime you want to see if a key has changed, so you would need to call it at least once per logic loop. |
23yrold3yrold
Member #1,134
March 2001
![]() |
SiegeLord said: See this tutorial. Ouch. I think I like Fen/Matthew's idea better. I hope that's considered the standard method; trying to cobble together a rudimentary engine off this new API just now ... -- |
Matthew Leverton
Supreme Loser
January 1999
![]() |
Events have the advantage of not losing key strokes if you don't poll enough. Also, it's simple to build key state on top of events, but not the other way around. The key state is more useful when you want to know if some key is currently held down. The events are more useful if you just want to know when a key was pressed. (e.g., Entering a name in a high score.) In A4 terms, the events are more like keypressed with readkey, and the state is more like the key array. |
jmasterx
Member #11,410
October 2009
|
I noticed that using the keystate, on Windows, if I hold shift, then minimize, let go, then restore, another call to a request to the keystate will yeild that shift is still pressed which it is not. Allegro should clear these or have away to clear them so that al_key_down() is reliable, Agui GUI API -> https://github.com/jmasterx/Agui |
Evert
Member #794
November 2000
![]() |
jmasterx said: I noticed that using the keystate, on Windows, if I hold shift, then minimize, let go, then restore, another call to a request to the keystate will yeild that shift is still pressed which it is not. Allegro should clear these or have away to clear them so that al_key_down() is reliable, Do you get a key up event in that case? |
Elias
Member #358
May 2000
|
The question is more - should there be a key up event? For most games it probably would make sense to get a key-up event for any keys which are still pressed when focus is lost and a key-down event for any keys already pressed when focus is regained. On the other hand with the way each keyboard driver does their own state tracking currently it may not be easy to implement and technically it makes sense that you never receive a key-up for a key when it happens while you don't have keyboard focus. -- |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Why can't Allegro 5 just refresh the key states when it regains focus? Allegro 4 did it. And by the way, I immensely prefer reading the key states as opposed to reading the key events. Why should I have to pass a key event to every object that listens for key presses when I could have them just check the global state instead? I mention this because I have an input library for A4 that checks the key states and produces key press/release/held/open states based on them. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Elias
Member #358
May 2000
|
Edgar Reynaldo said: Why should I have to pass a key event to every object that listens for key presses when I could have them just check the global state instead? Because key state is inherently broken. For example if you press the keys left,left,right,left fast and read the key state in each game tick (marked with *): * * * left left right left Then your game will work as if you pressed left 3 times. For some games, this would not matter much. But for some it would mean they completely break (e.g. classic-style fighter where left,left,right,left might be some special attack combo). With events you never can miss anything. Another example is a touchpad which implements touch-to-click as a mouse press and release at the same time when you first touch the pad. With events it's no problem, but with mouse state you will never catch the click as it's simply not long enough in a pressed state to ever catch it. And there's many more examples where state will just mean your game is broken, especially when you consider that in some situation the game may slow down because some background process uses CPU or disk access. With events nothing is lost even if the game would skip for say, half a second. With state whatever happened in that half second is completely lost... and it could mean you die in the game whenever it stutters just for a bit. -- |
Evert
Member #794
November 2000
![]() |
Elias said: The question is more - should there be a key up event?
One could argue either way on that, with the argument that if you don't you will think that keys that were once pressed are never released perhaps deciding in favour of saying that you should. Edgar Reynaldo said: Why can't Allegro 5 just refresh the key states when it regains focus?
Who says it can't? But the way it currently works is that Allegro keeps track of key up/down events for you. The keyboard state is updated incrementally. Quote: I immensely prefer reading the key states as opposed to reading the key events.
You are free to do that. Quote: I mention this because I have an input library for A4 that checks the key states and produces key press/release/held/open states based on them. So did I and it now updates them based on the keyboard event it gets passed. It really doesn't make that much of a difference and has the aesthetic advantage of only updating stuff when it knows that there is stuff to update (or we wouldn't have received a keyboard event in the first place). The rest of the time we can idle/sleep. |
jmasterx
Member #11,410
October 2009
|
I personally do prefer the event system, but, most API's (or atleast WinAPI) provides weather or not CTRL, ALT, and Shift are pressed at the time of a mouse event, and in WinAPI it is always accurate. This is really the only reason I use the key states. It is because when I get mouse events I want to know about those 3 keys. Given this situation however, I will have to keep track of these myself. The only flaw however, is that using this method rather than getting the key state is that if I return (regain Window focus) and shift is still pressed, it will now be unpressed until I get a key down with mouse again. For me that is the biggest problem. There should be a way for me to atleast accurately know the states at a given time so I can do this, and for shift to be set to pressed if it is when I regain focus. I know it is not easy, but I think some kind of solution should be found before AL5 release. Agui GUI API -> https://github.com/jmasterx/Agui |
Evert
Member #794
November 2000
![]() |
jmasterx said: The only flaw however, is that using this method rather than getting the key state is that if I return (regain Window focus) and shift is still pressed, it will now be unpressed until I get a key down with mouse again. Right; that's why the update should be done when keyboard focus is returned to the application, not when keyboard focus is lost. Probably. |
jmasterx
Member #11,410
October 2009
|
Evert said: Right; that's why the update should be done when keyboard focus is returned to the application, not when keyboard focus is lost. Probably. Yes I completely agree, there's no sense in doing it when it is lost, but I do hope it is fixed. Agui GUI API -> https://github.com/jmasterx/Agui |
Kisscool
Member #12,505
January 2011
|
Ok, thanks to all of you PS : OMAGAD IT'S WORKIN' ( Back to a more serious face ) |
|