Arrow keys and normal input question

This is a discussion on Arrow keys and normal input question within the Game Programming forums, part of the General Programming Boards category; I have a game which is text-based. I'm trying to incorporate the arrow keys. I know what the values are. ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    28

    Arrow keys and normal input question

    I have a game which is text-based. I'm trying to incorporate the arrow keys. I know what the values are. What I'm trying to do is be able to differentiate the difference between the player using the arrow keys and typing input.

    What I've tried so far was just detect for an extended character. This works but if it is a normal key, that character does not show up as part of an input.

    Here's an example of what I want to do:

    player is in a room.
    player wants to move so he presses the arrow key up.
    player moves up from pressing only one key.
    player sees something and now wants to look at it.
    player types "look object"

    If you get what I'm trying to do, it's not that easy for me. I thought about just taking the letter if pressed and then appending the rest of the input a player would put in, but that won't work if the player then decides to backspace through it all and retype something. That first letter will always stay the same.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Which OS/Compiler are you using?

    ((hopes it isn't the ancient fossil TurboC))
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    Visual C++ 2008 Enterpirse

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > but that won't work if the player then decides to backspace through it all and retype something. That first letter will always stay the same.
    So if they type "look" and then backspace 4 times, you're still stuck with the initial 'l'?

    Seems like a straight-forward counting bug to me.

    A bit of code showing your input text handing would be useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    Quote Originally Posted by Salem View Post
    So if they type "look" and then backspace 4 times, you're still stuck with the initial 'l'?
    Yes, by my thinking it through. Here's an idea I had. I would get each individual character and print it as if you were using fgets. But again, the backspace key is a bother. Here's my testing code so far.

    Code:
    char input[81] = {0};
    char cChar[2];
    int c;
    int extended = 0;
    
    c = _getch();
    if (c == 224)				// extended character pressed.  i.e. arrow keys
    	extended = _getch();
    if (extended)				// special keys
    {
                  // arrow key commands will be placed here
                  // then exit the function
    }
    
    while (c != '\r')
    {
    	printf("%c", c);
    	sprintf(cChar, "%c", c);
    	strcat(input, cChar);
    	c = _getch();
    }
    This works for what I want. But again, there is a problem if the backspace character is pressed.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    if ( c == '\b' )

    Then remove the last character from the input you have already.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    Thanks for the advice.

    This is what works now which takes into consideration any backspaces.
    Code:
    char cChar[2];
    int c;
    int extended = 0;
    
    c = _getch();
    if (c == 224)				// extended character pressed on first time.  i.e. arrow keys
    	extended = _getch();
    if (extended)				// special keys
    {
               // exit this function and perform special actions
    }
    
    while (c != '\r')			// until the enter button is pressed
    {
    	if (strlen(input) == 71)// to limit the string length to prevent overflow to output string
    		c = '\b';
    	if (c == 224)			// detects extended character input during regular input
    		c = _getch();		// to ignore the extended key. i.e. arrow keys
    	if (c == '\b')			// backspace pressed
    	{
    		if (strlen(input) != 0)		// string is not empty
    		{		
    			input[strlen(input) - 1] = '\0';	// erase the last character in the string
    			printf("%c %c", c, c);			// erase the character on the screen
    		}
    	}
    	else			 // if the character is viewable.  i.e. not backspaces
    	{
    		printf("%c", c);
    		sprintf(cChar, "%c", c);
    		strcat(input, cChar);
    	}
    	c = _getch();
    }
    Last edited by edomingox; 12-04-2010 at 03:43 PM.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    This is a good idea. A couple of adventure games back in the mid 80's used this concept to good effect. Instead of telling yourself to walk here or there you used the arrow keys.

    I'm glad to see you are allowing real time input without the game having to wait for the player to press enter. This means you can do some background processing and have the AI track and perhaps attack the player while they are or are not typing. Text adventure games should never be paused while they are waiting for input.
    Last edited by VirtualAce; 12-04-2010 at 09:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectInput arrow keys
    By Raison in forum Windows Programming
    Replies: 6
    Last Post: 12-17-2005, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21