Thread: Reading console input (character mode applications)

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Reading console input (character mode applications)

    Hi there, I'm trying to basically create my own CMD.exe just for fun, and I'm trying to get input from the user (what command they want to execute etc...). I'm trying to use the ReadConsole Win32 method, but the problem with that is I can't get the data that goes into the buffer that the user inputs into. Is there anyway to get user input? I mean, I know I can use getch() and cin.getline() and cin >> but I want a Windows solution. Is there any?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Console and mouse events are covered in part 5 of my console tutorial.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Console.Read(), Console.ReadLine?

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Your tutorial is nice, and I've read through some of it, but, are you telling me that in order to get console input using Windows methods, I can only get one character at at time?! Do I seriously need to create a buffer to hold the characters, then compare the buffer to a target word (like dir, cd, del, rename) etc... to know when a user has inputted a command?! That sounds awfully inefficient and just plain stupid.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by maxhavoc
    Do I seriously need to create a buffer to hold the characters, then compare the buffer to a target word (like dir, cd, del, rename) etc... to know when a user has inputted a command?! That sounds awfully inefficient and just plain stupid.
    How exacly dod you think it would work?
    try typing
    Code:
    exit
    and then
    Code:
    exit aaaaaa
    The second exit dosen't produce an error even though those 'a's mean nothing. The first word on the most recent line entered is compared to the console's commands and the appropriate command is executed. If necercary the rest of the line will be taken as arguments.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I was hoping it would work in a more intelligent manner, where I could input text into a buffer, then compare the buffer to a list of commands using a prebuilt function that I called StringsEqual, which returns true if it's equal and false if it's not. This way I first have to FORM the buffer, why in hell can't the ReadConsole method just STORE the damned buffer instead of deleting it! That would make this SO much easier...

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you want to create a cmd.exe that does things while still being responsive to the command prompt you will need to use the events mechanism, or run your command interpreter in a background thread and your user interface in a foreground thread. If you want the multi threaded version, you might be interested in my basic multithreading tutorial starting there.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I don't think that I need multithreading at this point, I just want to be able to get user input without having to do it character by character, but it seems that I have no choice.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by maxhavoc
    I don't think that I need multithreading at this point, I just want to be able to get user input without having to do it character by character, but it seems that I have no choice.
    So you hate cin.getline, getch and cin>> so much?

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by maxorator
    So you hate cin.getline, getch and cin>> so much?
    It's not that I hate them, it's that I only want to use Windows methods.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Alright, so, here is my solution for only using Windows methods to get user input, this function is designed to get all the input until the enter button is pressed, save it all to a string, and return it (as a reference). My problem is that I cannot figure out how to trap the enter or return button as an event, anyone know how to do this? Also, will this code work? (assuming my previous question is answered).

    Code:
    void CommandUtil::ReadWindow(char* pBuffer)
    {
    	int nCounter = 0;
    	m_pHandle = GetStdHandle(STD_INPUT_HANDLE);
    	INPUT_RECORD ih;
    	
    	while (true)
    	{
    		ReadConsoleInput(m_pHandle, &ih, 1, NULL);
    		if ("ENTER IS PRESSED") { break; }
    		pBuffer[nCounter] = ih.Event.KeyEvent.uChar.AsciiChar;
    		nCounter++;
    	}
    	
    }

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    off the top of my mind, not sure if itll work:

    Code:
    void CommandUtil::ReadWindow(char* pBuffer)
    {
    	int nCounter = 0;
    	m_pHandle = GetStdHandle(STD_INPUT_HANDLE);
    	INPUT_RECORD ih;
    	
    	while (true)
    	{
    		ReadConsoleInput(m_pHandle, &ih, 1, NULL);
    		
                    if ( ih.Event.KeyEvent.uChar.AsciiChar == VK_RETURN) // vk_return? or 0x10 or 0x13
                               break;
    
    		pBuffer[nCounter] = ih.Event.KeyEvent.uChar.AsciiChar;
    		nCounter++;
    	}
    	
    //        do sumin with pBuffer
    }

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> ReadConsoleInput(m_pHandle, &ih, 1, NULL);

    MSDN:

    lpNumberOfEventsRead
    [out] Pointer to a variable that receives the number of input records read.

    I suspect setting that to NULL will screw up.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Input From stdin
    By EvilGuru in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 01:48 AM
  2. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM
  5. Reading a character from the screen
    By neandrake in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 05:00 PM