getting keyboard input without pressing enter in C

Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.

Comments

  • : Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.
    :

    If all you want to do is to read a single keypress, you can use the getch() function, that's in

  • : : Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.
    : :
    :
    : If all you want to do is to read a single keypress, you can use the getch() function, that's in
    :
    :
    What you need is a keyboard function lib. It replaces the old key handler and reads keystrokes without waiting for a return key. Most game libs have one. The problem with getch() is that it waits for a key instead of just checking and continuing.

  • : : : Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.
    : : :
    : :
    : : If all you want to do is to read a single keypress, you can use the getch() function, that's in
    : :
    : :
    : What you need is a keyboard function lib. It replaces the old key handler and reads keystrokes without waiting for a return key. Most game libs have one. The problem with getch() is that it waits for a key instead of just checking and continuing.
    :
    :

    I know that. I just wanted to give him a simpler answer, after all, he's doing an RPG, not an action game.

  • Do you still need any help with the key pressing thing?
    I might know of a way that might help you.
  • yes....the getch works...but im still tryin to put together a function utilizing the kbhit method. Any more help is definatly appreciated, on this or any other methods you know that can help. Thanks

  • : yes....the getch works...but im still tryin to put together a function utilizing the kbhit method. Any more help is definatly appreciated, on this or any other methods you know that can help. Thanks
    :
    :
    Have you checked out the keyboard libraries that use direct scanning yet? It's the only way to doit!
    if you can't find one I'll send you one.
    mail me: etlusk@woodstock.com
  • If you go to http://www.gametutorials.com and then look at there C tutorials (Maze 1), they use the bit of code below to get users input.

    BOOL MovePlayer(CHAR_INFO screenBuffer[], COORD *playerPos)
    {
    INPUT_RECORD InputRecord;
    COORD oldPosition = *playerPos;
    DWORD Events=0;
    HANDLE hInput;
    BOOL bPlayerMoved = FALSE;

    hInput = GetStdHandle(STD_INPUT_HANDLE);

    ReadConsoleInput(hInput, &InputRecord, 1, &Events);

    if(InputRecord.EventType == KEY_EVENT)
    {

    if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
    {
    if(InputRecord.Event.KeyEvent.bKeyDown)
    {
    if(playerPos->X < (SCREEN_WIDTH - 1))
    {
    playerPos->X++;
    bPlayerMoved = TRUE;
    }
    }
    }

    else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
    {
    if(InputRecord.Event.KeyEvent.bKeyDown)
    {
    if(playerPos->X > 0)
    {
    playerPos->X--;
    bPlayerMoved = TRUE;
    }
    }
    }

    else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
    {
    if(InputRecord.Event.KeyEvent.bKeyDown)
    {
    if(playerPos->Y > 0)
    {
    playerPos->Y--;
    bPlayerMoved = TRUE;
    }
    }
    }

    else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
    {
    if(InputRecord.Event.KeyEvent.bKeyDown)
    {
    if(playerPos->Y < (SCREEN_HEIGHT - 1))
    {
    playerPos->Y++;
    bPlayerMoved = TRUE;
    }
    }
    }
    else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
    {
    printf("The end!");
    sleep(2000);
    exit(0);
    }
    }

    FlushConsoleInputBuffer(hInput);

    if(bPlayerMoved)
    {
    /* Collision Detection goes here */
    If(screenBuffer[playerPos->X + playerPos->Y * SCREEN_WIDTH].Char.AsciiChar == WALL)
    {
    *playerPos = oldPosition;
    }
    }
    return TRUE;
    }

    Check it out, although it uses console functions most of the Maze tutorial.
  • : Hi im currently writing my own rpg style game in C. For movement im using the 'w' key as up, 's' key as down, 'd' as right, and 'a' as left. My problem is that if the player wants to move right they have to move tile by tile and hit enter to move each time. I basically need a way to accept a character from the keyboard without having to type enter. Any help or suggestions is greatly appreciated.
    :
    Just as somewhat of a more mundane and theoretical suggestion, wouldn't it be a little more convenient to use the keypad for these movements? Of course, the numLock should be turned off, but I think that this would be a better utilization of the entire keyboard, especially if the characters in the game have the opportunity to speak. Just a thought.

    - InterGeek

    We are the music makers, and we are the dreamers of dreams. Willy Wonka

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories