I am new to c ++, but I could not find an answer to my question on the Internet. I want to read what the user enters while typing. Now I get the text entered from the user via cin >> variable; But the problem is that this method allows me to get user input only after the user has pressed enter. I want to catch his input on the fly. I want to as soon as he pressed the "=" button to perform a specific function. Tell me how can this be done?

  • getch getchar or similar. It depends on the exact dialect of the language. - pavel
  • Is it possible in more detail how to implement this in code? - Wlodzimierz Sitdikowski
  • one
    for details, look for the desired function, then int t; while (t = getch() != -1) {cout << t}; int t; while (t = getch() != -1) {cout << t}; something like this. - pavel
  • one
    Undoubtedly, you should indicate what you are developing and what kind of this application (console or dialog), since console applications do not allow a simple way to respond to keystrokes. - goldstar_labs
  • Console application. Developed in Dev-C ++. - Wlodzimierz Sitdikowski

1 answer 1

 #include <conio.h> int main() { setlocale( LC_ALL,"Russian" ); while(true) { if(kbhit()) { char knop=getch(); if (knop == '=') cout << "Это ="; } } } 
  • Specify the OS to which the question relates. In Windows, kbhit() polls the keyboard and does not wait for a key to be pressed. Are you sure you want to load the CPU in an endless loop? Usually it is enough to call getch() , which returns the entered character without waiting for the <Enter> key. - avp
  • I still understand c ++. I tried to do it without an endless loop, but it didn't work out for me. I would be grateful if refactorite - Wlodzimierz Sitdikowski
  • In his commentary (c while(...getch()...) ) under your question, @pavel essentially showed everything. - avp
  • If you only understand, then khbit is quite a good option. You can try to make a separate thread that will poll the keyboard and send to send information about the keys pressed to another thread that will output them to the console. You can also try using PeekConsoleInput, which allows you to monitor not only the keyboard. - goldstar_labs