keyboard and mouse activity

I have this program which I didn't write it is for checking activity of keyboard and mouse. When user use mouse or keyboard it makes a txt file. Now problem is, because I like to run it when computer starts and doesn't work. It works only when I make double click on exe. I tried all so I think there must be something wrong with that program. Can someone please check it and see why this doesn't work If I put it on start computer folder or task scheduler when computer starts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            private static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

            [MarshalAs(UnmanagedType.U4)]
            public uint cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }

        static uint GetLastInputTime()
        {
            uint idleTime = 0;
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            uint envTicks = (uint)Environment.TickCount;

            if (GetLastInputInfo(ref lastInputInfo))
            {
                uint lastInputTick = lastInputInfo.dwTime;
                idleTime = envTicks - lastInputTick;
            }

            return (idleTime > 0) ? (idleTime / 1000) : 0;
        }

        private static bool _idle = true;
        private static string _fileName;
        private static int _idleTime = 1800;

        static void Main()
        {
            var cfg = File.ReadAllLines("settings.conf");
            _fileName = cfg[0];
            int.TryParse(cfg[1], out _idleTime);

            while (true)
            {
                var inputTime = GetLastInputTime();

                if (inputTime < 1)
                {
                    _idle = false;

                    File.Create(_fileName).Close();
                }

                if (!_idle && inputTime > _idleTime)
                {
                    _idle = true;

                    if (File.Exists(_fileName))
                        File.Delete(_fileName);
                }

                Thread.Sleep(1000);
            }
        }
    }
}
This is C# not C++.

The reason that it doesn't work is most likely line 48. The programm expects that "settings.conf" is in the current directory. When this isn't the case an an unhandled exception will be thrown. This will end the programm prematurely.
settings.conf is always in the same folder as that exe file and still doesn't work when program start when computer start. It works only when I dobule click exe.
I see in task manager that exe runs, but it will not create txt file.
Last edited on
> why this doesn't work If I put it on start computer folder or task scheduler when computer starts?

GetLastInputInfo function:
This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function.
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getlastinputinfo

I also set this to start when user login, so it should work with this also GetLastInputInfo.
But it doesn't, I see exe runs but it doesn't create txt file.
I am trying to make this to work for a year and still doesn't work.
So I am really frustrated, have no idea anymore how to make this work??
settings.conf is always in the same folder as that exe file and still doesn't work when program start when computer start.
The current path doesn't need to be the exe path.

This might be the first step: Find out what the current path is.

However I would suggest that you make a [rudimentary] log file where you write the steps done and the result of the operations which might fail and other relevant output. This should tell you more about the nature of problem.
I have also changed that line and put in exact path for settings.conf I get the same results.
How to make that log file I don't know. I didn't make this code, I just check some parts and I think I know what they do. But I am here because some of you are experts and maybe you find a way to make this work.
Topic archived. No new replies allowed.