How to detect keybd_event without focus

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) { //My code here } 

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) { //My code here } } } 

Thanks for helping the guys;)

+4
source share
3 answers

You will need to connect to Windows using the SetWindowsHookEx function. You should read the Keyloggers article : how they work and how to detect them , published by SecureList to get an idea of ​​the process.

+2
source

I always have good performance using RegisterHotKey / UnregisterHotKey functions. Code example:

 [DllImport("User32")] public static extern bool RegisterHotKey( IntPtr hWnd, int id, int fsModifiers, int vk ); [DllImport("User32")] public static extern bool UnregisterHotKey( IntPtr hWnd, int id ); public const int MOD_SHIFT = 0x4; public const int MOD_CONTROL = 0x2; public const int MOD_ALT = 0x1; public const int WM_HOTKEY = 0x312; protected override void WndProc(ref Message m) { if (m.Msg == WM_HOTKEY && m.WParam == (IntPtr)0) { IntPtr lParamCTRLA = (IntPtr)4259842; IntPtr lParamB = (IntPtr)4325376; if (m.LParam == lParamCTRLA) { MessageBox.Show("CTRL+A was pressed"); } else if (m.LParam == lParamB) { MessageBox.Show("B was pressed"); } } base.WndProc(ref m); } private void Form1_Load(object sender, EventArgs e) { this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); RegisterHotKey(this.Handle, 0, MOD_CONTROL, (int)Keys.A); RegisterHotKey(this.Handle, 0, 0, (int)Keys.B); } private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { UnregisterHotKey(this.Handle, 0); } 

You can “register” as many keys (or key combinations) as you want by emulating the specified structure. All registered keys will fall into the if (m.Msg == WM_HOTKEY && m.WParam == (IntPtr)0) state if (m.Msg == WM_HOTKEY && m.WParam == (IntPtr)0) ; if pressed at all (regardless of the currently selected program). The easiest way to find out which particular key / combination is pressed is based on m.LParam (I got two values ​​that I turn on after a quick test with the given keys). You can do a quick research to find out a list of LParam or constant modifiers (mouse wheel, for example).

+2
source

End Code:

 [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) { //My code here } } } 
+2
source

Source: https://habr.com/ru/post/1501313/


All Articles