-
December 17th, 2016, 02:13 PM
#1
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.
-
December 17th, 2016, 02:31 PM
#2
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
December 17th, 2016, 03:51 PM
#3
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
December 18th, 2016, 12:56 AM
#4
Re: How to get input from c++ without stopping for user to press a key ?
 Originally Posted by 2kaud
Thanks the function worked.
I have one more question.
The 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.
-
December 18th, 2016, 04:13 AM
#5
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++23 Compiler: Microsoft VS2022 (17.6.2)
-
December 18th, 2016, 06:59 AM
#6
Re: How to get input from c++ without stopping for user to press a key ?
 Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|