CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How to get input from c++ without stopping for user to press a key ?

  1. #1
    Join Date
    Dec 2016
    Posts
    10

    Red face How to get input from c++ without stopping for user to press a key ?

    Is there any way to get input from user in c++ such that I get input when he presses any key, if he did not press any key then program should proceed.
    When we use getch(), then program waits for user to press something and then it proceeds but I want a method such that program will rum but when user presses a key it should respond to it.
    I tried ReadConsoleInput(...); but it does not work, it still waits for Input.

    Thanks.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,477

    Re: How to get input from c++ without stopping for user to press a key ?

    One way is to use PeekConsoleInput(). This is very similar to ReadConsoleInput() except that it doesn't remove the record from the buffer and returns immediately if no input is available (lpNumberOfEventsRead is 0). If input is available (lpNumberOfEventsRead is not 0), then use ReadConsoleInput() to obtain the data.

    See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    There is also GetNumberOfConsoleInputEvents() - but this also indicates the number of mouse and window-resizing input records as well as keyboard records. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.5)

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,477

    Re: How to get input from c++ without stopping for user to press a key ?

    When we use getch(), then program waits for user to press something and then it proceeds
    ...or alternately, if you don't need the functionality of the console functions, then _kbhit() will determine if a key has been pressed or not. See https://msdn.microsoft.com/en-us/library/58w7c94c.aspx. Then if a key has been pressed use getch() to get the character.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.5)

  4. #4
    Join Date
    Dec 2016
    Posts
    10

    Smile Re: How to get input from c++ without stopping for user to press a key ?

    Quote Originally Posted by 2kaud View Post
    ...or alternately, if you don't need the functionality of the console functions, then _kbhit() will determine if a key has been pressed or not. See https://msdn.microsoft.com/en-us/library/58w7c94c.aspx. Then if a key has been pressed use getch() to get the character.
    Thanks the
    Code:
     _kbhit()
    function worked.
    I have one more question.
    The
    Code:
    _kbhit()
    function will report true every frame if the a button is just held down, I don't want that,
    Is there a way so that the function report true in only one frame if a button is held down and is not released, and subsequent frames the function returns false ?

    Like if I press and hold 'a' then the function reports true and if I keep hold it then function returns false in frames after that.
    Last edited by 2kaud; December 18th, 2016 at 04:04 AM.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,477

    Re: How to get input from c++ without stopping for user to press a key ?

    Is there a way so that the function report true in only one frame if a button is held down and is not released, and subsequent frames the function returns false ?
    I don't think so directly. However, what about a loop that tests _kbhit() and retrieves chars until no more remain? Something like
    Code:
    int ch = 0;
    
    if (_kbhit()) {
        ch = getch();
        while (_kbhit())
            getch();
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.5)

  6. #6
    Join Date
    Dec 2016
    Posts
    10

    Re: How to get input from c++ without stopping for user to press a key ?

    Quote Originally Posted by 2kaud View Post
    I don't think so directly. However, what about a loop that tests _kbhit() and retrieves chars until no more remain? Something like
    Code:
    int ch = 0;
    
    if (_kbhit()) {
        ch = getch();
        while (_kbhit())
            getch();
    }

    Yes I got this also working. Thank you very very much.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)