C input without waiting for user to press enter | Sololearn: Learn to code for FREE!

+2

C input without waiting for user to press enter

Hi everyone I want create mini game with c I want it to immedietly read character from keyboard as it typed in console without waiting for user to press enter button Any idea how to do that ?

3/3/2019 6:06:14 PM

AliR૯za

16 Answers

New Answer

+11

You can use getch() or getche() functions defined in the conio.h library. For more information and difference between these two functions, check out the link below: https://www.quora.com/What-are-the-uses-of-getche-in-C++

+6

Aaron Eberhardt Ok thank you 🙏

+6

its in library called conio.h #include <conio. h> getch(); or getchar() ;

+5

nAutAxH AhmAd Thank you for reference 🙏

+4

AliR૯za I've had the same doubts in the past. Although getch() works, remember that it isn't standard C++ (and hence whatever isn't should be avoided if possible). Some has claimed that std::cin.get() or getchar() can be feasible replacements, but these functions do echo the keyboard hit onto the terminal. If you have to use getch(), just keep in mind the cons that come along. https://www.sololearn.com/Discuss/210329/?ref=app https://www.sololearn.com/Discuss/302236/?ref=app

+4

Aaron Eberhardt 👍 Thanks for reference

+4

Hatsy Rei Ok ,thank you

+3

What you might want is usually called raw input because it won't be buffered nor processed by the OS. While reading the other answers I was a little surprised that Windows can't offer such simple solutions as Linux. However when looking for raw input on Windows I found this page: https://docs.microsoft.com/en-us/windows/desktop/inputdev/raw-input Maybe it's a bit complicated to use but it should be the best standardized solution for Windows.

+2

Which OS do you use? Input will be handled differently depending on that...

+2

With Windows I have no experience...

+2

🇮🇷 M N سلام دقیقا همونیه که میخواستم ممنونم❤

+1

Aaron Eberhardt I want it for windows

+1

سلام Use getch() function, it works as you want https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017

0

Hatsy Rei Aaron Eberhardt 🇮🇷 M N Hi,Sorry for bothering you again my friends You are always helping me in c/c++ problems 🙏 & in this case if you have any idea or refrences i will happy to see them,thank you🙏