|
Answer» I need a small program which will detect if any key from A to Z or 0 to 9 is pressed than it makes a txt file, after that if nothing happend for 30 minutes it delete that file and it should work as process. And there is config file to set PATH and time. I get this in C# but it doesn't work, can someone look at it and see why it is not working, please? here is C# version
Code: [SELECT]using System.Runtime.InteropServices; using System.Threading.Tasks;
[DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { PUBLIC static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)] public UInt32 cbSize; [MarshalAs(UnmanagedType.U4)] public UInt32 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 uint _lastInputTime; private bool _idle = false; private string _fileName; private int _idleTime = 1800;
void Main() { VAR cfg = File.ReadAllLines("nastavitve.cfg"); _fileName = cfg[0]; int.TryParse(cfg[1], out _idleTime); _lastInputTime = GetLastInputTime(); while (true) { var inputTime = GetLastInputTime();
if (_idle && inputTime - _lastInputTime < 1) { _idle = false; File.Create(_fileName).Close(); } if (!_idle && inputTime - _lastInputTime > 5) { _idle = true; if (File.Exists(_fileName)) File.Delete(_fileName); } Thread.Sleep(1000); } }
and here is version for compiling with VS
Code: [Select]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 uint _lastInputTime; private static bool _idle = false; private static string _fileName; private static int _idleTime = 1800;
static void Main() { var cfg = File.ReadAllLines("nastavitve.cfg"); _fileName = cfg[0]; int.TryParse(cfg[1], out _idleTime);
_lastInputTime = GetLastInputTime();
while (true) { var inputTime = GetLastInputTime();
if (_idle && inputTime - _lastInputTime < 1) { _idle = false;
File.Create(_fileName).Close(); }
if (!_idle && inputTime - _lastInputTime > _idleTime) { _idle = true;
if (File.Exists(_fileName)) File.Delete(_fileName); }
Thread.Sleep(1000); } } } }
|