If you are using Visual C++, check this out. The trick is
_kbhit() under conio.h.
ADVICE: Please maintain indents (tabs) which is a good programming practice. Notice the tabs I maintained for the loop.
Code: Select all
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
int main(int argc, char *argv[])
{
int i;
do{
i = rand();
printf("%d \n", i ) ;
Sleep(1000);
Beep(3000,50);
} while (!_kbhit());
system("PAUSE");
return EXIT_SUCCESS;
}
If you are using Dev C++, you need to use something as follows. I don't have the IDE to test, so you need to play a bit with it.
Code: Select all
#include <windows.h>
int main(void )
{
keybd_event(VkKeyScan('y'), 0, 0, 0);
keybd_event(VkKeyScan('y'), 0, KEYEVENTF_KEYUP, 0);
keybd_event(VkKeyScan('q'), 0, 0, 0);
keybd_event(VkKeyScan('q'), 0, KEYEVENTF_KEYUP, 0);
// or alternatively...
keybd_event(0x59, 0, 0, 0);
keybd_event(0x59, 0, KEYEVENTF_KEYUP, 0);
keybd_event(0x51, 0, 0, 0);
keybd_event(0x51, 0, KEYEVENTF_KEYUP, 0);
return 0;
}