character input v.c++

Discussion in 'OT Technology' started by adam17, Mar 11, 2005.

  1. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    ok im writing a console for my engine in OpenGL and i need to know how to constantly check the keyboard for input. i cant use scan or getch because they require you to hit enter before they return a value. make sense? i want to be able to strike a key and make it appear on the screen. i know direct input is out there but im trying to stay away from it, i want my engine to be portable to linux and maybe mac in the future.

    any ideas? guys? gals?
     
  2. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    hmmm ive done OpenGL in java and i used the actionListeners. worked for me then. see if c++ has anything like that
     
  3. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    i should clarify that i use c++. heh i forgot to mention that:hsugh:
     
  4. R-Type

    R-Type The Bydo Empire must die!

    Joined:
    Aug 2, 2002
    Messages:
    1,049
    Likes Received:
    0
    Location:
    CT
    If memory serves, getche() requires a carriage return, but getch() does not. Try it, if you haven't already.
     
  5. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    ill tell you what here is my code. compile it and tell me what im doing wrong.

    Code:
     #include <stdlib.h>
     #include <stdio.h>
     
     int main()
     {
     	char	cTemp;
     	int		i=0;
     
     	cTemp = getchar();
     	while(cTemp != 0x10)	//check if the character is enter or return
     	{
     		printf("%c", cTemp);//output current character
     		cTemp = getchar();
     	}
     	printf("%c\n", cTemp);
     
     	return 0;
     }
     
    u should only be able to type a string and only the last character should show up. when i run it, u type the word, the entire word is outputted, the program should exit at this point but it doesnt.
     
  6. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    ok changing the value from 0x10 to 0xA exits the loop. still im having the problem with single character input.
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The problem with the last piece of code is that i is initialized to 0, and the loop checks for i != 0, so it isn't executed.

    The behavior I'm seeing with getchar() is that keypresses are buffered until a newline is entered. Keys are echoed when they are pressed, then when you hit enter, the program executes the loop and re-echoes the keypresses so your input appears twice. If you want to avoid keypresses being echoed, you probably want to use getch() from conio.h.
     
  8. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    having the keys echo back immediatly is what i want. this is just a test program for when i write the console for my GL engine. i need a function to return any and every keypress as soon as it is pressed.

    i tried getch from conio and it still echoes the entire string.

    say for example i want to type the word "hello" the program needs to grab the letter, the screen should appear "hheelllloo". a key is pressed and is printed, the loop then prints the same key again.
     
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    What platform and compiler are you using? I compiled the code using Visual C++, and the keyboard input read by getchar() is being buffered until the ENTER key pressed. So the output looks like:

    hello <-- echoed as keys are pressed
    hello <-- characters echoed by printf

    The first call to getchar() is blocked until the ENTER key is pressed. getch() on the other hand returns with each keypress.
     
  10. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    HELL YEAH! it works now, i just replaced getchar with getch (i dont know why i was using getchar) and it works! thanks!

    the only problem im having now is getting enter or escape to exit the loop.
     
  11. R-Type

    R-Type The Bydo Empire must die!

    Joined:
    Aug 2, 2002
    Messages:
    1,049
    Likes Received:
    0
    Location:
    CT
    See! what'd I tell ya :p
     
  12. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    With getch(), check for the carriage return character '\r' instead of newline '\n'
     
  13. adam17

    adam17 now im gonna sing the doom song! doom doom doom do

    Joined:
    Aug 25, 2004
    Messages:
    1,666
    Likes Received:
    0
    Location:
    n ur gf's bed
    w00t thanks alot dude!!! it works perfectly!
     

Share This Page