Thread: FAQ: Directional Keys - Useing in Console

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Theres no way I'm reading all that but try this:

    if (input == 77) ... //Right
    if (input == 75) ... //Left
    if (input == 80) ... //Down
    if (input == 72) ... //Up
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  2. #2
    Yes. I agree with sangdrax. you need to change the way you increment and decrement. i gor teally woird things coming out of my early progs when i would use x = x++. Use x++ by itself, or x += 1. (except with your variables in there.)
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by BMJ

    end_game = end_game--; //Game_Over is reduced by 1
    Change this!!

    end_game--;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Hmm, why do you need to post all your code for something that small. It's just a matter of chnaging some evaluation values, as lightatdawn said.

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    ok, well he still doesn't need to post all his code. And why use the arrow keys? That feels to cramped and is too far away from the other keys.

  6. #6
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    How did you do that code formatting.. I mean that colours etc.. You didn't format them YOURSELF did you???
    what does signature stand for?

  7. #7
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    By the way.. I have the same problem using the direction keys as well... I tried GetKeyState() but virtual keys don't work (excluding VK_RETURN) (Console) .. I also tried that getch() but when you press one of the arrows.. it just says p:
    Code:
    int main()
    {
      char choice[1];
      choice[0] = getch();
      cout << choice[0];
      return 0;
    }
    what does signature stand for?

  8. #8
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Testing PHP...
    PHP Code:
    int main()
    {
    cout << "Hello World!";
    return 
    0;

    what does signature stand for?

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    YEY!! PHP!!.. But the code colours are kinda anti-C++, maybe we should ask an admin to change them...
    what does signature stand for?

  10. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    You need to use Windows' virtual keys

  11. #11
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    *sigh*

    Example of virtual key usage:
    Code:
    #include <windows.h>
    #include <iostream>
    
    bool Keypress(char &Key)
    {
    	INPUT_RECORD Event;
    	DWORD NumberOfEvents, EventsRead, EventCounter;
    
    	GetNumberOfConsoleInputEvents(GetStdHandle(STD_INPUT_HANDLE), &NumberOfEvents);
    
    	if (NumberOfEvents == 0)
    		return false;
    	
    	for (EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++)
    	{
    		PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
    		if ((Event.EventType == KEY_EVENT) && ((Event.Event.KeyEvent.bKeyDown)))
    		{
    			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
    			Key = Event.Event.KeyEvent.wVirtualKeyCode;
    			if (!(FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE))))
    				exit(0);
    			return true;
    		}
    		else
    			ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &Event, 1, &EventsRead);
    	}
    
    	return false;
    }
    
    int main()
    {
    	char key = 0;
    	for (;;)
    	{		
    		if (Keypress(key))
    		{
    			if (key == VK_LEFT)
    				std::cout << "Left\n";
    			if (key == VK_RIGHT)
    				std::cout << "Right\n";
    			if (key == VK_DOWN)
    				std::cout << "Down\n";
    			if (key == VK_UP)
    				std::cout << "Up\n";
    			if (key == VK_ESCAPE)
    				return 0;
    		}
    	}
    	return 0;
    }
    Last edited by BMJ; 10-04-2002 at 04:33 PM.

  12. #12
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    No, it's not; What I did was totally different - as his subject says 'using directional keys'
    Last edited by BMJ; 10-04-2002 at 04:31 PM.

  13. #13
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Well for a game, keypress catching is much nicer than standard inputting; who wants to type "left" and press enter when you can just press the left arrow and have the action take place.

    You don't even have to know how the function KeyPress works, just how to use it, which is simple.


    EDIT: Driveway - whatever.
    Last edited by BMJ; 10-04-2002 at 04:36 PM.

  14. #14
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    LOL! Dude, you copied the code above which got cut off!

    Copy it from this...


    (btw: don't forget to add a way to get out of the infinite for loop, like with escape)

  15. #15
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    hmmm what?

    You can do it!

    There, does that help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. directional keys @ console
    By ipe in forum C Programming
    Replies: 1
    Last Post: 03-13-2003, 06:25 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Arrow keys in console?
    By SyntaxBubble in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 06:12 PM