-3

What I am trying to do is to create a program that will measure the time between each keystroke and then save it at the moment I have two programs one of them logs the keys in a text file and the other only counts the time between the time when enter is pressed and a. I am struggling with combining the two so that any key will start the stopwatch/timer and then the same key or a different key will stop the timer and then save that time to the text file. For example : If I wrote the word 'program' it would save the time it took for each keystroke. I hope you will offer your help :D

The stopwatch program:

   #include <iostream>
  #include <iomanip>
  #include <windows.h>

using namespace std;

 int main() {

cout << "Hit enter to start timer" << endl;
cout << "Hit 'a' to stop timer" << endl;
cin.get();

unsigned short seconds = 0;
unsigned short minutes = 0;

while (!GetAsyncKeyState(0x41)) {
//while 'a' is not being pressed
    seconds++;
    if (seconds == 60) {
        seconds = 0;
        minutes++;
    }
    cout << std::setfill('0') << setw(2) << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
    Sleep(1000);
}
return 0;
}

The key logger

 #include<iostream>
  #include<windows.h>
  #include<Winuser.h>
  #include<string>


  int Save(int key_stroke, char *file);


using namespace std;

    int main()
     {
       char i;
       int Save(int key_stroke, char *file);


while (1)

{
    for (i = 8; i <= 190; i++)
    {

        if (GetAsyncKeyState(i) == -32767)

                Save(i, "LOGER.TXT");



        }

         }
          system ("PAUSE");
          return 0;
       }

     int Save (int key_stroke, char *file)
      {
     cout<< key_stroke << endl;
      return 0;
 }
  • Using `GetAsyncKeyState` correctly is too complicated; you can find an alternative approach [here](http://www.cplusplus.com/forum/windows/6632/#msg30578) – anatolyg Feb 01 '16 at 12:39

1 Answers1

-1
 #include<iostream>
 #include<windows.h>
  #include<Winuser.h>
  #include<string>
  #include <iomanip>
   #include<ctime>
   #include<cstdio>


  int Save(int key_stroke, char *file);
  char i;

  clock_t corrected = 0;

  using namespace std;

   int main()
  {

    int Save(int key_stroke, char *file);

   while (1)
    {

    for (i = 8; i <= 190; i++)
    {

        if
            (GetAsyncKeyState(i) == -32767 )
        {
             clock_t start = clock();
   cout<<"clock: " << start-corrected <<endl;


          Save(i, "LOGER.TXT");


                              clock_t end= clock();


                         corrected=clock();
    }
    }

  }
  system ("PAUSE");
   return 0;
 }

   int Save (int key_stroke, char *file)
    {
    cout<< key_stroke << endl;

      return 0;
       }
  • If someone else wants to use this answer, it's currently hard, because it has no explanations. To make a good answer, you should explain why and how it works. Also, you could fix the broken [indentation](https://en.wikipedia.org/wiki/Indent_style) - even though the question had horrible indentation, the answer doesn't need to be as bad. – anatolyg Feb 01 '16 at 12:48