Good afternoon, my program is running on a while loop close to infinite, and i need program to do something if user presses letter 'k'. I do not want the program to wait for an input i just want it to run real time and if a user presses 'k' it does something. Do you have any ideas or where i could read upon it?
Well, this is non-standard, but should run on a Windows system
Press the escape key to exit from the loop. Press k to see the message at line 30 displayed.
(Use of Sleep() here was just for this example, and isn't strictly necessary).
#include <iostream>
#include <conio.h>
#include <windows.h>
constint key_K = 0x4B;
int keypress()
{
if (_kbhit())
{
int temp = _getch();
if (temp == 0 || temp == 0xE0) // special keys such as arrow or function
temp = _getch() + 0x100; // make it distinguishable from ordinary key
return temp;
}
return 0;
}
int main()
{
for (int i=0; i<10000; ++i)
{
std::cout << i << '\n';
int key = keypress();
if (key == 'K' || key == 'k' )
{
std::cout << "K key was pressed\n";
}
if (key == 27) // test for ESCape key
break;
Sleep(250);
}
std::cout << "Escape key was pressed\n";
}
#include <conio.h>
#include <iostream>
usingnamespace std;
int main()
{
while (true)
{
// do some stuff here like
cout << '.';
if (_kbhit() && _getch() == 'k')
{
// so sth else
break;
}
}
system("pause");
return 0;
}