Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(cd C:\\\\Users\\\\vankh\\\\Documents\\\\Sources\\\\OpenKey:*)",
"Bash(curl:*)"
]
}
}
21 changes: 21 additions & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/OpenKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ void OpenKeyFree() {
UnhookWinEvent(hSystemEvent);
}

void OpenKeyReinitHooks() {
// Unhook existing hooks
UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyboardHook);
UnhookWinEvent(hSystemEvent);

// IMPORTANT: Reset modifier key state to prevent stale state issues
// This fixes the bug where hotkeys don't work after unlock because
// _lastFlag still contains old modifier key values
_flag = 0;
_lastFlag = 0;
_keycode = 0;
_isFlagKey = false;

// Reinitialize hooks
HINSTANCE hInstance = GetModuleHandle(NULL);
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProcess, hInstance, 0);
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseHookProcess, hInstance, 0);
hSystemEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL, winEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
}

void OpenKeyInit() {
APP_GET_DATA(vLanguage, 1);
APP_GET_DATA(vInputType, 0);
Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/OpenKey.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<AdditionalDependencies>wtsapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -111,6 +112,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wtsapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -131,6 +133,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<AdditionalDependencies>wtsapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -151,6 +154,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
<AdditionalDependencies>wtsapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/OpenKeyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ void OpenKeyManager::freeEngine() {
OpenKeyFree();
}

void OpenKeyManager::reinitHooks() {
// Reinitialize keyboard hooks after session unlock
extern void OpenKeyReinitHooks();
OpenKeyReinitHooks();
}

bool OpenKeyManager::checkUpdate(string& newVersion) {
wstring dataW = OpenKeyHelper::getContentOfUrl(L"https://raw.githubusercontent.com/tuyenvm/OpenKey/master/version.json");
string data = wideStringToUtf8(dataW);
Expand Down
1 change: 1 addition & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/OpenKeyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OpenKeyManager {

static void initEngine();
static void freeEngine();
static void reinitHooks();

static bool checkUpdate(string& newVersion);

Expand Down
23 changes: 23 additions & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/SystemTrayHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,27 @@ map<UINT, LPCTSTR> menuData = {

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static UINT taskbarCreated;
const UINT_PTR HOOK_HEALTH_TIMER = 1; // Timer ID for hook health check

switch (message) {
case WM_CREATE:
taskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));
// Register for session change notifications to handle lock/unlock
WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION);
// Set up a 10-second timer to periodically fix stuck keyboard hooks
SetTimer(hWnd, HOOK_HEALTH_TIMER, 10000, NULL);
break;
case WM_WTSSESSION_CHANGE:
// Reinitialize keyboard hooks on session unlock (wParam == WTS_SESSION_UNLOCK)
if (wParam == WTS_SESSION_UNLOCK) {
OpenKeyManager::reinitHooks();
}
break;
case WM_TIMER:
// Periodic hook health check - reinitialize hooks to fix random stuck issues
if (wParam == HOOK_HEALTH_TIMER) {
OpenKeyManager::reinitHooks();
}
break;
case WM_USER+2019:
AppDelegate::getInstance()->onControlPanel();
Expand Down Expand Up @@ -169,6 +186,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Shell_NotifyIcon(NIM_ADD, &nid);
}
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_DESTROY:
// Unregister session change notifications
WTSUnRegisterSessionNotification(hWnd);
// Kill the health check timer
KillTimer(hWnd, HOOK_HEALTH_TIMER);
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions Sources/OpenKey/win32/OpenKey/OpenKey/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ redistribute your new version, it MUST be open source.
#include <shellapi.h>
#include <Commctrl.h>
#include <psapi.h>
#include <wtsapi32.h>

#include "resource.h"

Expand Down