Thread: is there any way to read a key pressed rather then held down in allegro

  1. #1
    bobish
    Guest

    is there any way to read a key pressed rather then held down in allegro

    any help would be appreciated

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    If I understand you're meaning, you might try:

    PHP Code:
    if(keypressed())
      
    keyVar=readkey(); 
    Where keyVar is a variable you made earlier to store the key that was pressed. Basically this is implementing a keyboard buffer for you so you don't have to handle the keyboard at such a low level.

    -Justin
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  3. #3
    bobish
    Guest
    puting "if (keyvar=KEY_SPACE)" in side that if statment slowed down the rate of key reading but it still reconized the key being held down. I want it so it will only recognize the key as pressed once and then you have to let go of the key and press it again to recognize it as pressed again.
    void set_keyboard_rate(int delay, int repeat);
    sounded like it might do the right thing but the documentation was pretty vauge on how to use it.

  4. #4
    bobish
    Guest
    the other way i could see doing this is that once a key has been pressed it is ignored untill the key is released but can you read if the key is up in allergo. If not would it work to use GetAsyncKeyState()

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    if (keyvar=KEY_SPACE)
    Don't replace the if statement in the code I gave, add your if statment after it. And it should be:
    if (keyvar == KEY_SPACE)
    otherwise you are assigning keyvar the value of the space bar being pressed, not testing if it is.

    Modified snippet:

    PHP Code:
    int keyvar;
    ...
    if(
    keypressed())
      
    keyvar=readkey();
    else 
    keyvar=0;

    if(
    keyvar == KEY_SPACE)
    {...} 
    Hope that works. If not, (haven't tried this one yet..) then sounds like you may have to impliment it yourself. There are lots of ways to do this, one may be to create an array that will be indexed by the significant key.

    PHP Code:
    bool kbhandle[256]; // Handle all ASCII keys.

    // if key pressed:
    kbhandle[keyvar]=1;

    // if key released:
    kbhandle[keyvar]=0;

    if(
    keyvar == KEY_SPACE && kbhandle[KEY_SPACE] == 0)
       ... 
    There are obviously some optimizations to be done to that... but just trying to give the idea.

    -Justin
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  6. #6
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    I dont use allegro, but i think i know what your saying, to check if it has been hit once, but no being held down?

    You can do something like:

    Code:
    if(key_is_down==FALSE) keypressed=FALSE;
    if (key_is_down==TRUE && keypressed==FALSE)
    {
        keypresses=TRUE;
        do whatever
    }

  7. #7
    this is good for loops

    make sure you installed the allegro keyboard handler

    Code:
    if (key[KEY_LEFT])
    {
        ...
    }
    it is kinda like kbhit(), where it doesn't block when you use it. That makes it ideal for gameloops and stuff like that. It is fast too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  2. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. Plz Tell me how to Read Characters like Key UP, Key Down
    By arvindkr in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 06:22 PM
  4. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM