key[KEY_SPACE] backwards
Aqua Regia

Is there some sort of function that pushes eg. space instead of checking if it's pushed?

Anomie

What do you need an automatically pressed space-bar for? I can't imagine why that'd be necessary...

Aqua Regia

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;
}

Anomie

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?

Aqua Regia

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.

Anomie

...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?

Aqua Regia

I just want to be able to bind the keys.

Anomie

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:

int jump, run_left, run_right;

jump = KEY_SPACE;
run_left = KEY_A;
run_right = KEY_D;

//Later...

if(key[jump]) doJump();
if(key[run_left]) doRunLeftStuff();
if(key[run_right]) doRunRightStuff();

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.

Aqua Regia

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.

Anomie

Do you have a main loop, checking for input, blitting the buffer to the screen and all that fun stuff?

Aqua Regia

Yeah, and when I press space myself it works like it should.

CGamesPlay

No, what Anomie is saying is that you should have it work like this:

int key_that_you_press_to_make_the_buffer_gray = KEY_A;

if(key[key_that_you_press_to_make_the_buffer_gray])
    floodfill(buffer, 1, 1, makecol(128,128,128));

Anomie

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.

Aqua Regia

What does the "int key_that_you_press_to_make_the_buffer_gray = KEY_A;" do?

Anomie

It does this. Very important.

Thomas Fjellstrom

somewhat important to explicitly note, simulate_keypress only places a new key in allegro's readkey buffer, and does nothing else.

CGamesPlay
Quote:

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

Quote:

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.

Onewing

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
14typedef 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 
30extern int input_timer;
31 
32class INPUT
33{
34public:
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 
3int input_timer;
4 
5INPUT::INPUT()
6{
7 KEYBOARD = new myKEY;
8 JOYSTICK = new JOY;
9}
10 
11INPUT::~INPUT()
12{
13 delete KEYBOARD;
14 delete JOYSTICK;
15}
16 
17void 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 
27bool 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 
37bool INPUT::MAP(int iValue)
38{
39 return MAP(0, iValue, 0);
40}
41 
42 
43bool 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);
6gInput.map(LEFT, 0, KEY_LEFT, 0);
7gInput.map(RIGHT, 0, KEY_RIGHT, 0);
8gInput.map(DOWN, 0, KEY_DOWN, 0);
9gInput.map(UP, 0, KEY_UP, 0);
10gInput.map(ACCEPT, 0, KEY_SPACE, 0);
11gInput.map(ACTION_1, 0, KEY_LCONTROL, 0);
12 
13...
14 
15while(!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.

Audric

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.

amber

key[KEY_SPACE] = 1

This'll do exactly what you say you want, but probably not what you really want.

CGamesPlay

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.

Thread #596582. Printed from Allegro.cc