Is there some sort of function that pushes eg. space instead of checking if it's pushed?
What do you need an automatically pressed space-bar for? I can't imagine why that'd be necessary...
Not only space. I couldn't find a decent key binding program, so I decided to create one myself.
Example:
if(key[KEY_A])
{
press space;
}
I still...don't really understand why you don't just check for space-bar being pressed... I assume after that you'd have something checking to see if space-bar was 'pressed', to accomplish something?
As in:
if(key[KEY_A]) space_pressed = true; if(space_pressed == true) runMyFunction();
What's wrong with if(key[KEY_SPACE])? Is there some reason you're checking for an artificial space-bar press, rather than just using a bool or something similar?
I never press space myself, but I want it to be pressed when I press A.
In your example, space is just a name, it has nothing to do with the spacebar.
...why? Is there some part of your code that demands that the space-bar be pressed? If so, just change it to A. Or you could have it check to see if A or space-bar were pressed.
[edit] Exactly. The keyboard is a human interface device. If you want it to change without human input, you might as well just use a bool instead.
[edit2]Wait wait wait, are you going for a 'user defines their own buttons to press for certain things' kinda thing?
I just want to be able to bind the keys.
Alright, yeah... That makes more sense. What you really want to do is to have a variable for each thing you'd like your buttons to do. (probably not the best way, I've never tried this before...)
But...something like:
Actually, now that I think about it, key[] might not like ints...
But you get the idea, anyways.
[edit] Just found this in TFM, though, if that's what you really want to do.
Something like that I'm looking for yes, but I don't seem to get it working:
if(key[KEY_A])
simulate_keypress(KEY_SPACE);
if(key[KEY_SPACE])
floodfill(buffer, 1, 1, makecol(128,128,128));
I want it to use the floodfill function when you press A.
Do you have a main loop, checking for input, blitting the buffer to the screen and all that fun stuff?
Yeah, and when I press space myself it works like it should.
I dunno why it wouldn't work with key[]...but try using the if (readkey() == (KEY_SPACE); method from the example on that page.
[edit] And...yeah... That way is definitely much more useful if you ever plan to do anything more complicated than what you're doing now. Up to you though.
What does the "int key_that_you_press_to_make_the_buffer_gray = KEY_A;" do?
It does this. Very important.
somewhat important to explicitly note, simulate_keypress only places a new key in allegro's readkey buffer, and does nothing else.
I dunno why it wouldn't work with key[]...but try using the if (readkey() == (KEY_SPACE); method from the example on that page.
Important to note it that simulate_keypress only works on the readkey buffer as Thomas just said, and the snippet you just suggested is not what the page said to do and won't work. To learn how to use readkey, see its manual page.
readkey
What does the "int key_that_you_press_to_make_the_buffer_gray = KEY_A;" do?
It makes a variable. You can name it whatever you want, but it stores the name of the key that you want to read. When you go to check the key, instead of using a fixed name, you use the name that you stored in the variable.
But you are going to have to learn how to use variables to really understand what's going on.
Here's a primitive mapping system I used in projects a while back.
allegro_input.h
| 1 | /************************************************** |
| 2 | * FILE: allegro_input.h * |
| 3 | * AUTHOR: Steven Silvey * |
| 4 | * DATE: 5/07/2006 * |
| 5 | * * |
| 6 | * Configures a dynamic mapping of all input, * |
| 7 | * either keyboard or joystick, to main input. * |
| 8 | **************************************************/ |
| 9 | |
| 10 | #ifndef _ALLEGRO_INPUT_H_ |
| 11 | #define _ALLEGRO_INPUT_H_ |
| 12 | |
| 13 | #define MAX_MAPPING 50 |
| 14 | typedef class INPUT input; |
| 15 | |
| 16 | #include "game.h" |
| 17 | #include "allegro_keyboard.h" |
| 18 | #include "allegro_joystick.h" |
| 19 | |
| 20 | /* Quick key associations */ |
| 21 | #define ACCEPT 0 |
| 22 | #define ACTION_1 1 |
| 23 | #define ACTION_2 2 |
| 24 | #define CANCEL 3 |
| 25 | #define LEFT 4 |
| 26 | #define RIGHT 5 |
| 27 | #define UP 6 |
| 28 | #define DOWN 7 |
| 29 | |
| 30 | extern int input_timer; |
| 31 | |
| 32 | class INPUT |
| 33 | { |
| 34 | public: |
| 35 | JOY *JOYSTICK; |
| 36 | myKEY *KEYBOARD; |
| 37 | INPUT(); |
| 38 | ~INPUT(); |
| 39 | |
| 40 | |
| 41 | void map(int iIndex, int iType, int iValue, int jType); |
| 42 | |
| 43 | /* Checks if the key or joystick mapped to iValue has been accepted. */ |
| 44 | bool MAP(int iValue); |
| 45 | |
| 46 | /* Checks if the key or joystick mapped to iValue has been accepted with a |
| 47 | cool down timer set to iTimer_Delay */ |
| 48 | bool MAP(int iValue, int iTimer_Delay); |
| 49 | |
| 50 | /* Checks if the key or joystick[mPID] mapped to iValue has been accepted with a |
| 51 | cool down timer set to iTimer_Delay */ |
| 52 | bool MAP(int mPID, int iValue, int iTimer_Delay); |
| 53 | }; |
| 54 | |
| 55 | #endif /* End of _ALLEGRO_INPUT_H_*/ |
allegro_input.cpp
| 1 | #include "allegro_input.h" |
| 2 | |
| 3 | int input_timer; |
| 4 | |
| 5 | INPUT::INPUT() |
| 6 | { |
| 7 | KEYBOARD = new myKEY; |
| 8 | JOYSTICK = new JOY; |
| 9 | } |
| 10 | |
| 11 | INPUT::~INPUT() |
| 12 | { |
| 13 | delete KEYBOARD; |
| 14 | delete JOYSTICK; |
| 15 | } |
| 16 | |
| 17 | void INPUT::map(int iIndex, int iType, int iValue, int jType) |
| 18 | { |
| 19 | if(iIndex < 0 || iIndex >= MAX_MAPPING) return; |
| 20 | if(iType == 0) |
| 21 | KEYBOARD->map(iIndex, iValue); |
| 22 | else |
| 23 | JOYSTICK->map(iIndex, jType, iValue); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | bool INPUT::MAP(int mPID, int iValue, int iTimer_Delay) |
| 28 | { |
| 29 | if((KEYBOARD->MAP(iValue) || JOYSTICK->MAP(mPID, iValue)) && abs(ticks - input_timer) >= iTimer_Delay) |
| 30 | { |
| 31 | input_timer = ticks; |
| 32 | return true; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | bool INPUT::MAP(int iValue) |
| 38 | { |
| 39 | return MAP(0, iValue, 0); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | bool INPUT::MAP(int iValue, int iTimer_Delay) |
| 44 | { |
| 45 | return MAP(0, iValue, iTimer_Delay); |
| 46 | } |
And then, I'd have a place where I'd make the connections:
game.cpp
| 1 | |
| 2 | ... |
| 3 | |
| 4 | // Map the keyboard and joystick |
| 5 | //gInput.map(CANCEL, 0, KEY_ESC, 0); |
| 6 | gInput.map(LEFT, 0, KEY_LEFT, 0); |
| 7 | gInput.map(RIGHT, 0, KEY_RIGHT, 0); |
| 8 | gInput.map(DOWN, 0, KEY_DOWN, 0); |
| 9 | gInput.map(UP, 0, KEY_UP, 0); |
| 10 | gInput.map(ACCEPT, 0, KEY_SPACE, 0); |
| 11 | gInput.map(ACTION_1, 0, KEY_LCONTROL, 0); |
| 12 | |
| 13 | ... |
| 14 | |
| 15 | while(!gInput.MAP(CANCEL,5) && !GAME.quit) |
| 16 | |
| 17 | ... |
I stopped using it because it needs some cleaning up, but the functionality of it seemed to work fine if I remember correctly.
Aqua Regia : It may not be clear so far, but all the KEY_ symbols correspond a number: KEY_A is 1, KEY_SPACE is 75.
You write if (key[KEY_SPACE]) because it's more readable than if (key[75]), but the end result is the same.
Since these are simply numbers, you can store them in integer variables, as used in the above explanations.
key[KEY_SPACE] = 1
This'll do exactly what you say you want, but probably not what you really want.
Also it will randomly not work but only sometimes. Doing that is just a bad idea, because Allegro automatically updates that variable from a different thread.