Howdy, Stranger!

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

Categories

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

  • Haroldo_o_pHaroldo_o_p Member Posts: 79
    : 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

  • etlusketlusk Member Posts: 168
    : : 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.

  • Haroldo_o_pHaroldo_o_p Member Posts: 79
    : : : 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.

  • dartsmandartsman Member Posts: 88
    Do you still need any help with the key pressing thing?
    I might know of a way that might help you.
  • ca_priestca_priest Member Posts: 17
    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

  • etlusketlusk Member Posts: 168
    : 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
  • dartsmandartsman Member Posts: 88
    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.
  • InterGeekInterGeek Member Posts: 4
    : 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

  • Shawn CarterShawn Carter Member Posts: 0

    // http://forcoder.org // free ebooks and video tutorials about | Ruby, Go, Delphi, PL/SQL, R, Perl, MATLAB, C, PHP, Assembly, Objective-C, C++, Visual Basic, C#, Python, Visual Basic .NET, JavaScript, Scratch, Swift, Java Transact-SQL, COBOL, Ada, Clojure, Apex, Bash, FoxPro, Alice, Kotlin, Fortran, ABAP, ML, Dart, Hack, F#, Crystal, Rust, Julia, Logo, Scheme, Lua, VBScript, Erlang, SAS, Lisp, LabVIEW, Awk, D, Scala, Prolog | _________

Sign In or Register to comment.