Few problems with console application

This is a discussion on Few problems with console application within the C Programming forums, part of the General Programming Boards category; Hi! First problem: I got a switch loop and in this switch loop I got few conditions like if user ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Few problems with console application

    Hi!

    First problem:

    I got a switch loop and in this switch loop I got few conditions like if user presses 's' or 'a' then do something. This switch loop is in the while loop and while loop will stop executing until the user presses the ESC key. The problem is that sometimes I have to press 2 times the ESC key if I want to exit the applicaion and I have the same problem with the keys 'a' and 's'.
    I'm using the getch() function for checking what key was pressed. What am I missing here?

    Second problem:

    I don't know the ASCII values for arrows (left, right, up, down). Anybody knows ?

    I'm working on a console win32 application in VC++ 6.0 on Win98.

    Please help.
    Last edited by GaPe; 04-01-2002 at 08:17 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Code:

    Code:
    # include <windows.h>
    # include <stdio.h>
    # include <conio.h>
    # include <string.h>
    
    # define RIGHT 77
    # define LEFT  75 
    
    void gotoxy (int x, int y);
    void clrscr (void);
    
    int main()
    {
    	HANDLE hStdout;
    
    	do
    	{
    		hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    		
    		clrscr();
    		SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN);
    		gotoxy (5, 1);
    		printf ("File");
    		gotoxy (18, 1);
    		printf ("Search");
    		switch ( getch() )
    		{
    			case LEFT:
    				SetConsoleTextAttribute(hStdout, BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    				gotoxy (5, 1);
    				printf("File");
    				if (getch() == 13)
    				{
    					gotoxy (5, 3);
    					printf("HI!");
    				}
    				break;
    			case RIGHT:
    				SetConsoleTextAttribute(hStdout, BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    				gotoxy (18, 1);
    				printf ("Search");
    				if (getch() == 13)
    				{
    					gotoxy (18, 3);
    					printf("BLAH!");
    				}
    				break;
    			default:
    				continue;
    		}
    	} while (getch() != 27);
    	SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    	return 0;
    }
    
    void gotoxy(int x, int y)
    {
       COORD coord;
    
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition(GetStdHandle (STD_OUTPUT_HANDLE), coord);
    }
    
    void clrscr ()
    {
       system ("cls");
    }
    Those hex values are not working.
    Last edited by GaPe; 04-02-2002 at 12:47 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Can possibly 1 day go by without this question being asked?

    Look up the API function ReadConsoleInput().....you need to look for KEY_EVENT ....and you need to compare to VK_UP,VK_RIGHT,VK_LEFT,VK_DOWN

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I don't have any help files and I was looking in the MSDN on the net but I found nothing. That MSDN online is a mess.

    Can you write me an example for this function? Please.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ten seconds, but that's only because I went to MSDN via google instead of a direct link.

    http://msdn.microsoft.com/library/de...nchar_4xmc.asp

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    I said already more than once that getch returns ascii codes for characters and scan codes for arrows and funtion keys. Scan codes of the arrow keys:
    hex - left 0x4b, right 0x4d, up 0x48, down 0x50
    decimal - left 75, right 77, up 72, down 80
    characters - left 'K', right 'M', up 'H', down 'P'

    _____________________
    Never teach people - they learn, but they shall never forgive you

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy

    The only thing that works are the keys 'K', 'M', 'H', 'P'. Look for the code up there and look if there is any bug in my code.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Unregistered
    Guest
    Your program works correctly and displays your HI! and BLAH!, but it's a bit strange and that's why you probably cannot use it yourself. Perhaps there is some reason why you expect enter after a key, but usually getch or other keyboard input what waits for the key to be pressed is used only once in a message loop, ie you assign some variable with it and that after you use that variable. In your program I would write character = getch () and clrscr () after you first write "file" and "search". Also system is usually in stdlib.h, cannot figure out in what include file it is in your compiler.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by GaPe
    I don't have any help files and I was looking in the MSDN on the net but I found nothing. That MSDN online is a mess.

    Can you write me an example for this function? Please.
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
    HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);//handle to input
    INPUT_RECORD ir;//keyevent structure
    DWORD dwRead = 0;//dummy....forget this for now
    bool bLoop = true;//continue or quit?
    
    ZeroMemory(&ir,sizeof(INPUT_RECORD));//clear the memory before starting
    cout << "Press arrow keys......escape to quit" << endl;
    while(bLoop){
    if(ReadConsoleInput(hInput,&ir,1,&dwRead) //read input
        && ir.EventType == KEY_EVENT //is it a key?
        && ir.Event.KeyEvent.bKeyDown){//Is the key down or up?
        switch(ir.Event.KeyEvent.wVirtualKeyCode){//What was the key?
        case VK_DOWN:
        cout << "DOWN" << endl;
        break;
        case VK_UP:
        cout << "UP" << endl;
        break;
        case VK_RIGHT:
        cout << "RIGHT" << endl;
        break;
        case VK_LEFT:
        cout << "LEFT" << endl;
        break;
        case VK_ESCAPE:
        bLoop = false;//Quit
        default:
        break;
        }
        }
    }
    return 0;
    }
    This is a quick example using windows consoles.........

    <sorry>
    I wrote this in C++ and then realised it was the C board.....
    Anyway...you get the idea
    </sorry>

  10. #10
    Unregistered
    Guest
    Yes, that's the correct way to do it, but for beginners it lacks that wonderful simplicity...

  11. #11
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Unregistered you're right but I'm trying.

    In the main function I have this:

    Code:
    int main()
    {
    	HANDLE hStdout, hInput = GetStdHandle(STD_INPUT_HANDLE); //handle to input
    	INPUT_RECORD ir; //keyevent structure
    	DWORD dwRead = 0; //dummy....forget this for now
    	BOOL Loop = TRUE;
    	int x = 0;
    
    	ZeroMemory (&ir, sizeof(INPUT_RECORD)); //clear the memory before starting
    	while (Loop)
    	{
    		hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    
    		clrscr ();
    		SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN);
    		gotoxy (5, 1);
    		printf ("File\tSearch\tTools\tHelp");
    		if (ReadConsoleInput(hInput, &ir, 1, &dwRead) //read input
    			&& ir.EventType == KEY_EVENT //is it a key?
    			&& ir.Event.KeyEvent.bKeyDown)
    		{//Is the key down or up?
    			switch (ir.Event.KeyEvent.wVirtualKeyCode)
    			{//What was the key?	
    			case VK_RIGHT:
    				x += 10;
    				SetConsoleTextAttribute (hStdout, FOREGROUND_INTENSITY | BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    				gotoxy (x + 5, 1);
    				printf ("Search");
    				/*if (getch() == 13)
    				{
    					gotoxy (18, 3);
    					printf("BLAH!");
    					getch();
    				}*/
    				if (x == 40) x = 0;
    				break;
    			case VK_LEFT:
    				SetConsoleTextAttribute (hStdout, FOREGROUND_INTENSITY | BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    				gotoxy (5, 1);
    				printf("File");
    				if (getch () == 13)
    				{
    					gotoxy (5, 3);
    					printf("Hi!");
    				}
    				getch ();
    				break;
    			case VK_ESCAPE:
    				Loop = FALSE;
    			default:
    				break;
    			}
    		}
    	}
    	clrscr();
    	SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    	return 0;
    }
    I have a problem with my menu cycle. If I press right or left key then the rectangle of red background color and white font color should cycle through the menu. I can't get it working. Any ideas?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  12. #12
    Unregistered
    Guest
    Yes, simplicity is also necessary. Try to finish your first program first.

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Alright I finished my menu code. It is really simple. But I have a question and I'll post that question in a new thread.

    Thanks guys for helping me.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console application - input handling
    By Something in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2007, 02:19 PM
  2. Console Application
    By llcool_d2006 in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2006, 02:46 PM
  3. Win32 Console app Problems...
    By Junior89 in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2005, 04:17 PM
  4. Console Problems
    By programmer_bod in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2004, 01:50 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

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