我在底层键盘挂钩过程中调用WIN32 API FindWindow(),试图将焦点设置到我的Python脚本GUI窗口(这是tkinter顶层窗口之一),但是每次获取结果为NULL时,并且GetLastError()始终返回5(ERROR_ACCESS_DENIED),有人知道原因吗?它运行在WIN10 64位系统上。
LRESULT CALLBACK MyllKeyboardProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(hHookllkb, nCode, wParam, lParam);
}
else
{
//printf("Key paras:%d %d %d\n", nCode, wParam, lParam);
KBDLLHOOKSTRUCT *Key_Info = (KBDLLHOOKSTRUCT*)lParam;
if (HC_ACTION == nCode)
{
if (WM_KEYDOWN == wParam) //如果按键为按下状态
{
/*
if (Key_Info->vkCode == VK_LWIN || Key_Info->vkCode == VK_RWIN) //屏敝 WIN(左右) 键
{
return TRUE;
}
if (Key_Info->vkCode == 0x4D && ((GetKeyState(VK_LWIN) & 0x8000) ||
(GetKeyState(VK_RWIN) & 0x8000))) //屏敝 WIN+D 组合键(左右)
{
return TRUE;
}
if (Key_Info->vkCode == 0x44 && ((GetKeyState(VK_LWIN) & 0x8000) ||
(GetKeyState(VK_LWIN) & 0x8000))) //屏敝 WIN+M 组合键(左右)
{
return TRUE;
}
*/
if (Key_Info->vkCode == VK_F11 && GetKeyState(VK_CONTROL) & 0x8000) //屏敝 CTRL + F12 组合键
{
//printf("CTRL+F2 %x\n", wParam);
if (bHide)
bHide = FALSE;
else
bHide = TRUE;
HideTaskBar(bHide);
return TRUE;
}
if (Key_Info->vkCode == VK_F12 && GetKeyState(VK_CONTROL) & 0x8000) //屏敝 CTRL + F11 组合键
{
int nCmdShow;
if (bHideConsole)
{
nCmdShow = SW_SHOW;
bHideConsole = FALSE;
}
else
{
nCmdShow = SW_HIDE;
bHideConsole = TRUE;
}
ShowWindow(hWndConsole, nCmdShow);
if (hWndConsole && !bHideConsole)
{
//Tricks for stealing keyboard input focus. it works
ShowWindow(hWndConsole, SW_HIDE);
ShowWindow(hWndConsole, SW_SHOW);
HWND hWndFocus = NULL;
if(bScriptRunning==TRUE){
HWND hWndRun = FindWindow(NULL, _T("The user interface for A - code supervision"));
if (hWndRun)
{
printf("Py running GUI window found:%x\n", hWndRun);
hWndFocus = hWndRun;
SetForegroundWindow(hWndFocus);
SetFocus(hWndFocus);
SetActiveWindow(hWndFocus);
EnableWindow(hWndFocus, TRUE);
}
else
{
printf("Py running GUI window not found error code: %d\n", GetLastError());
}
}
else {
hWndFocus = hWndConsole;
SetForegroundWindow(hWndFocus);
SetFocus(hWndFocus);
SetActiveWindow(hWndFocus);
EnableWindow(hWndFocus, TRUE);
}
}
return TRUE;
}
/*
if (Key_Info->vkCode == VK_TAB && Key_Info->flags & LLKHF_ALTDOWN) //屏敝 ATL + TAB 组合键
{
return TRUE;
}
if (Key_Info->vkCode == VK_ESCAPE && Key_Info->flags & LLKHF_ALTDOWN) //屏敝 ATL + ESC 组合键
{
return TRUE;
}
*/
}
}
return CallNextHookEx(hHookllkb, nCode, wParam, lParam);
}
}