-
May 30th, 2017, 06:33 AM
#1
Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
I have the following code used in WH_KEYBOARD_LL hook:
Code:
#include <windows.h>
#include <string>
bool WindowSwitcherUIRun = false;
HWND WindowSwitcherUIHandle = 0;
//---------------------------------------------------------------------------
static void ShowErrorMessage(std::string Msg, const DWORD ErrorCode)
{
char ECStr[50] = {0};
itoa(ErrorCode, ECStr, 10);
Msg += ". Error code: " + std::string(ECStr) + ".";
MessageBoxA(0, Msg.c_str(), "AltTabHookInstaller", MB_OK | MB_ICONERROR);
}
//---------------------------------------------------------------------------
static void PressKey(const WORD Key)
{
KEYBDINPUT ki;
ki.wVk = Key;
ki.wScan = MapVirtualKeyA(Key, 0);
ki.dwFlags = 0;
ki.time = 0;
ki.dwExtraInfo = 0;
INPUT Input;
Input.type = INPUT_KEYBOARD;
Input.ki = ki;
SendInput(1, &Input, sizeof(INPUT));
}
//---------------------------------------------------------------------------
static void ReleaseKey(const WORD Key)
{
KEYBDINPUT ki;
ki.wVk = Key;
ki.wScan = MapVirtualKeyA(Key, 0);
ki.dwFlags = KEYEVENTF_KEYUP;
ki.time = 0;
ki.dwExtraInfo = 0;
INPUT Input;
Input.type = INPUT_KEYBOARD;
Input.ki = ki;
SendInput(1, &Input, sizeof(INPUT));
}
//---------------------------------------------------------------------------
static void SendKey(const WORD Key)
{
PressKey(Key);
ReleaseKey(Key);
}
//---------------------------------------------------------------------------
static LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
DWORD Msg = wParam;
PKBDLLHOOKSTRUCT hs = (PKBDLLHOOKSTRUCT)lParam;
if ((Msg == WM_SYSKEYDOWN || Msg == WM_SYSKEYUP) && code >= 0 && hs->vkCode == VK_TAB)
{
if (!WindowSwitcherUIRun)
{
WindowSwitcherUIRun = (long long int)ShellExecuteA(
0,
"open",
"C:\\Users\\Default\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch\\Window Switcher.lnk",
0,
0,
SW_SHOW) > 32;
if (WindowSwitcherUIRun)
{
WindowSwitcherUIHandle = FindWindowA("TaskSwitcherWnd", 0);
}
}
else if ((hs->flags & LLKHF_UP) == 0)
{
if (SetForegroundWindow(WindowSwitcherUIHandle))
{
SendKey(VK_RIGHT);
}
}
return 1;
}
else if (WindowSwitcherUIRun && ((hs->flags & LLKHF_ALTDOWN) == 0))
{
WindowSwitcherUIRun = false;
ReleaseKey(VK_MENU);
if (SetForegroundWindow(WindowSwitcherUIHandle))
{
SendKey(VK_RETURN);
}
return 1;
}
return CallNextHookEx(0, code, wParam, lParam);
}
//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HHOOK LLKeyHook = 0;
HANDLE Mutex = CreateMutex(0, 0, "AltTabHookInstalleMutex");
MSG Msg;
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
goto Return;
}
if (!(LLKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)&LowLevelKeyboardProc, 0, 0)))
{
ShowErrorMessage("Couldn't install Alt Tab Hook", GetLastError());
goto Return;
}
while (GetMessage(&Msg, NULL, 0, 0) != 0)
{
}
Return:
if (Mutex)
{
CloseHandle(Mutex);
}
if (LLKeyHook)
{
UnhookWindowsHookEx(LLKeyHook);
}
return 0;
}
I run this application on a remote Windows 8 machine when I connect to it via AnyDesk.
Because the Alt+Tab functionality doesn't work when I connect via AnyDesk.
My question is why does SetForegroundWindow(WindowSwitcherUIHandle) fail with "Access is denied" error?
It only fails when the Alt+Tab is first pressed when a window not belonging to Explorer.exe is active/foreground.
Note: the Task Switcher window opened by ShellExecuteA() also belongs to Explorer.exe.
Thanks.
-
May 30th, 2017, 06:58 AM
#2
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
How do you know that SetForegroundWindow() fails with Access is Denied? There's nothing in your posted code re this. SetForegroundWindow() will either succeed or fail. If it fails there's no further info available as it doesn't set last error so GetLastError() isn't valid. SetForegroundWindow() will only work when one of these is true
The process is the foreground process.
The process was started by the foreground process.
The process received the last input event.
There is no foreground process.
The process is being debugged.
The foreground process is not a Modern Application or the Start Screen.
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
No menus are active.
See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 30th, 2017, 07:10 AM
#3
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
I checked GetLastError() immediately after SetForegroundWindow() when it failed though I haven't provided this check here.
-
May 30th, 2017, 07:28 AM
#4
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
I am looking for a solution to make the Task Switcher window foreground/active immediately after it is opened.
-
May 30th, 2017, 07:34 AM
#5
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
Originally Posted by Askarali
I checked GetLastError() immediately after SetForegroundWindow() when it failed though I haven't provided this check here.
As mentioned in post #2, SetForegroundWindow() isn't documented as setting last error so GetLastError() probably isn't valid.
Also, there doesn't appear to be a check that WindowSwitcherUIHandle is valid before being used. It is set from FindWindow() but there's no check that FindWindow() is successful.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 30th, 2017, 07:50 AM
#6
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
I am pretty sure FindWindow() is returning a valid window handle. I checked this.
When SetForegroundWindow() fails, if I click on the Task Switcher window (by doing this, I make it foreground), the hook continues to work without errors.
-
May 30th, 2017, 05:52 PM
#7
Re: Implementing Alt+Tab functionality using WH_KEYBOARD_LL hook.
You might want to read up on AllowSetForegroundWindow in msdn.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|