I am working on a program that should detect when a user presses a keyboard or uses his mouse, even if the program is minimized or not focused. I think I need to use the windows API, keybd_event (user32), but I don’t know how to use the listener for this event. I have something like this:
[DllImport("user32.dll")] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo); void PressKey(byte keyCode) {
I did some research, but the first time I had to use DllImport, so I don’t know how to continue ... thanks (Ps: Sorry for my bad English, this is not my native language :))
(PPs: I read all your answers, but it takes some time to read each link, so I will work on it today, but I think I will find the answer. In any case, thanks for all the links;))
Edit: I just finished my code and it works :) It looks something like this:
[DllImport("user32.dll")] public static extern Boolean GetLastInputInfo(ref tagLASTINPUTINFO plii); public struct tagLASTINPUTINFO { public uint cbSize; public Int32 dwTime; } private void timerTemps_Inactif_Tick(object sender, EventArgs e) { tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO(); Int32 IdleTime; LastInput.cbSize = (uint)Marshal.SizeOf(LastInput); LastInput.dwTime = 0; if (GetLastInputInfo(ref LastInput)) { IdleTime = System.Environment.TickCount - LastInput.dwTime; if (IdleTime > 10000) {
Thanks for helping the guys;)
source share