I want to check when a key is released, but I can't do so without having an infinite cycle, and this puts the rest of the code on pause. How can I detect if a key is released while running the rest of my program without an infinite cycle? This is the code I found and that I have been using:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int counter=0;
ofstream myfile;
short prev_escape = 0, curr_escape = 0;
myfile.open("c:\example.txt");
while(true)
{
if(GetAsyncKeyState(VK_ESCAPE))
curr_escape = 1;
else
curr_escape = 0;
if(prev_escape != curr_escape)
{
counter++;
if(curr_escape)
{
myfile <<"Escape pressed : " << counter << endl;
cout<<"Escape pressed !" << endl;
}
else
{
myfile <<"Escape released : " << counter << endl;
cout<<"Escape released !" << endl;
}
prev_escape = curr_escape;
}
}
myfile.close();
return 0;
}
See Question&Answers more detail:os