'how to create a timer that pauses when the user has not pressed any key for more than 1 minute and resumes when the user starts typing again

I am trying to build an timer in c# that is able to count when the user is typing, stop counting when the keyboard is in active for more that one minute, and continue counting when the user resumes work. its part of my final year project.

any advice/ help is welcomed

[DllImport("user32.dll")] public static extern int GetAsyncKeyState(Int32 i);

    public static void keylogg(int i)
    {
        int hr = 0, min = 0, sec = 0;
        bool active;
        
        String filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        //creates a new directory if the on in question is missing

        if (!Directory.Exists(filepath))
        {
            Directory.CreateDirectory(filepath);
        }

        //creating file to save the data

        string path = (filepath + @"\keyloggs.txt");
        int keyState = GetAsyncKeyState(i);

        if (File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path)) ;
        }

        while (true)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            //give other programs a chance to run
            Thread.Sleep(5);
            // Timer 
            //check key state
            //print to console
            for (i = 32; i < 127; i++)
            {
                keyState = GetAsyncKeyState(i);
                if (keyState == 32769)
                {
                    Console.Write((char)i + ",");

                    //Store key strokes into a text file

                    using (StreamWriter sw = File.AppendText(path))
                    {
                        sw.Write((char)i);

                    }
                }
            }
            //print to console
            if (keyState != null)
            {
                continue;
            }
            else
            {
                // Key stroke timer
                stopWatch.Stop();
                // Get the elapsed time as a TimeSpan value.
                TimeSpan ts = stopWatch.Elapsed;

                // Format and display the TimeSpan value. 
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds / 10);

                Console.WriteLine("Runtime" + elapsedTime);

                // print to file
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.Write("Runtime", elapsedTime);

                }

            }
            
        }

""This is the keylogger code plus the timer code but the timer code is not returning any results yet the keylogger works fine and there are no errors""



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source