mardi 14 avril 2015

Windows keyboard hook without knowledge of what key has been pressed

I am looking for a windows hook that simply registers keyDown events, without knowledge of the key that has been pressed. This is because I am creating an application that will be always running, and I don't want to inadvertently make it easier for viruses (or any application) to log my keys.


I am using C#, and DllImport to import the required DLL's. The code is based on http://ift.tt/1aBIvJ4


The hook is:



private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
//If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function
//without further processing and should return the value returned by CallNextHookEx.
if (nCode >= 0)
{
if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
{
//int vkCode = Marshal.ReadInt32(lParam);
//Console.WriteLine((Keys)vkCode);

//Reset the timer
Reset(timer);

//If the backlight is off, turn it on.
if (isOn == false)
{
Start(false);
isOn = true;
}
}
}

return CallNextHookEx(_hookID, nCode, wParam, lParam);
}


I've learned that wParam contains information on the event (keydown, syskeydown), and lParam contains information on the specific key. I'm looking for a hook that can do everything I did above, but without receiving the individual key that has been pressed.


My question is: Does such a hook exist? I can't find one on MSDN.


If there isn't, is there a way to change the method signature to remove lParam? I have tried this, but every time the method exists, the program crashes. I have also simply added lParam = (IntPtr)0; as the first line in the method, but I don't think this will help, as the event handler still passes in the specific key in lParam, I just change it during the method.


Thanks!


Aucun commentaire:

Enregistrer un commentaire