Skip to content

Commit 615fe66

Browse files
committed
Start menu keyboard hotkey fix for Win11 Insider (#1165)
Windows 11 Insider builds started to register global shell hotkey for Win key (and Ctrl+Esc) that are used to trigger start menu. The problem is that it is not (easily) possible to intercept messages about these hotkeys. Thus we will unregister them during init and install hook that will prevent their further registration. Fixes #1165
1 parent 01c1227 commit 615fe66

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Src/StartMenu/StartMenuDLL/StartMenuDLL.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,34 @@ static BOOL WINAPI SetWindowCompositionAttribute2( HWND hwnd, WINCOMPATTRDATA *p
28532853
return SetWindowCompositionAttribute(hwnd,pAttrData);
28542854
}
28552855

2856+
///////////////////////////////////////////////////////////////////////////////
2857+
// hooks for preventing shell hotkeys registration on Win11
2858+
2859+
using ShellRegisterHotKey_t = BOOL(WINAPI*)(HWND, int, UINT, UINT, HWND);
2860+
2861+
static IatHookData* g_ShellRegisterHotKeyHook;
2862+
static ShellRegisterHotKey_t g_ShellRegisterHotKey;
2863+
2864+
static BOOL WINAPI ShellRegisterHotKeyHook(HWND hWnd, int id, UINT fsModifiers, UINT vk, HWND hWndTarget)
2865+
{
2866+
// Win key
2867+
if (fsModifiers == MOD_WIN && vk == 0)
2868+
return FALSE;
2869+
2870+
// Ctrl+Esc
2871+
if (fsModifiers == MOD_CONTROL && vk == VK_ESCAPE)
2872+
return FALSE;
2873+
2874+
return g_ShellRegisterHotKey(hWnd, id, fsModifiers, vk, hWndTarget);
2875+
}
2876+
2877+
// one-time APC function to unregister shell hotkeys
2878+
void NTAPI DisableShellHotkeysFunc(ULONG_PTR Parameter)
2879+
{
2880+
UnregisterHotKey(NULL, 1);
2881+
UnregisterHotKey(NULL, 2);
2882+
}
2883+
28562884
///////////////////////////////////////////////////////////////////////////////
28572885

28582886
static void OpenCortana( void )
@@ -2951,8 +2979,33 @@ static void InitStartMenuDLL( void )
29512979
g_ProgHook=SetWindowsHookEx(WH_GETMESSAGE,HookProgManThread,NULL,progThread);
29522980
g_StartHook=SetWindowsHookEx(WH_GETMESSAGE,HookDesktopThread,NULL,GetCurrentThreadId());
29532981
if (IsWin11())
2982+
{
29542983
g_StartMouseHook=SetWindowsHookEx(WH_MOUSE,HookDesktopThreadMouse,NULL,GetCurrentThreadId());
29552984

2985+
// hook ShellRegisterHotKey to prevent twinui.dll to install shell hotkeys (Win, Ctrl+Esc)
2986+
// without these hotkeys there is standard WM_SYSCOMMAND+SC_TASKLIST sent when start menu is invoked by keyboard shortcut
2987+
g_ShellRegisterHotKey = (ShellRegisterHotKey_t)GetProcAddress(GetModuleHandle(L"user32.dll"), MAKEINTRESOURCEA(2671));
2988+
auto twinui = GetModuleHandle(L"twinui.dll");
2989+
2990+
if (g_ShellRegisterHotKey && twinui)
2991+
{
2992+
g_ShellRegisterHotKeyHook = SetIatHook(twinui, "user32.dll" ,MAKEINTRESOURCEA(2671), ShellRegisterHotKeyHook);
2993+
2994+
// unregister shell hotkeys as they may be registered already
2995+
// this has to be done from context of thread that registered them
2996+
auto hwnd = FindWindow(L"ApplicationManager_ImmersiveShellWindow", NULL);
2997+
if (hwnd)
2998+
{
2999+
auto thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetWindowThreadProcessId(hwnd, NULL));
3000+
if (thread)
3001+
{
3002+
QueueUserAPC(DisableShellHotkeysFunc, thread, 0);
3003+
CloseHandle(thread);
3004+
}
3005+
}
3006+
}
3007+
}
3008+
29563009
HWND hwnd=FindWindow(L"OpenShellMenu.CStartHookWindow",L"StartHookWindow");
29573010
LoadLibrary(L"StartMenuDLL.dll"); // keep the DLL from unloading
29583011
if (hwnd) PostMessage(hwnd,WM_CLEAR,0,0); // tell the exe to unhook this hook
@@ -3150,6 +3203,8 @@ static void CleanStartMenuDLL( void )
31503203
g_DrawThemeTextCtlHook=NULL;
31513204
ClearIatHook(g_SetWindowCompositionAttributeHook);
31523205
g_SetWindowCompositionAttributeHook=NULL;
3206+
ClearIatHook(g_ShellRegisterHotKeyHook);
3207+
g_ShellRegisterHotKeyHook=NULL;
31533208

31543209
CloseManagers(false);
31553210
ClearIatHooks();

0 commit comments

Comments
 (0)