Skip to content

Commit

Permalink
fix: clear imgui keyEventQueue when window focus lost (alt-tabbing)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN committed Jan 29, 2024
1 parent ace5235 commit 60d2be2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/PCH.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ namespace stl
T::func = trampoline.write_call<5>(a_src, T::thunk);
}

template <class T>
void write_thunk_call_6(std::uintptr_t a_src)
{
SKSE::AllocTrampoline(14);
auto& trampoline = SKSE::GetTrampoline();
T::func = *(uintptr_t*)trampoline.write_call<6>(a_src, T::thunk);
}

template <class F, size_t index, class T>
void write_vfunc()
{
Expand Down
27 changes: 27 additions & 0 deletions src/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ namespace Hooks
static inline REL::Relocation<decltype(thunk)> func;
};

struct WndProcHandler_Hook
{
static LRESULT thunk(HWND a_hwnd, UINT a_msg, WPARAM a_wParam, LPARAM a_lParam)
{
if (a_msg == WM_KILLFOCUS) {
Menu::GetSingleton()->OnFocusLost();
}
return func(a_hwnd, a_msg, a_wParam, a_lParam);
}
static inline REL::Relocation<decltype(thunk)> func;
};

struct RegisterClassA_Hook
{
static ATOM thunk(WNDCLASSA* a_wndClass)
{
WndProcHandler_Hook::func = reinterpret_cast<uintptr_t>(a_wndClass->lpfnWndProc);
a_wndClass->lpfnWndProc = &WndProcHandler_Hook::thunk;

return func(a_wndClass);
}
static inline REL::Relocation<decltype(thunk)> func;
};

void Install()
{
SKSE::AllocTrampoline(14);
Expand All @@ -330,6 +354,9 @@ namespace Hooks
stl::write_vfunc<0x2, BSImagespaceShaderISSAOCompositeFog_SetupTechnique>(RE::VTABLE_BSImagespaceShaderISSAOCompositeFog[0]);
stl::write_vfunc<0x2, BSImagespaceShaderISSAOCompositeSAOFog_SetupTechnique>(RE::VTABLE_BSImagespaceShaderISSAOCompositeSAOFog[0]);

logger::info("Hooking WndProcHandler");
stl::write_thunk_call_6<RegisterClassA_Hook>(REL::VariantID(75591, 77226, 0xDC4B90).address() + REL::VariantOffset(0x8E, 0x15C, 0x99).offset());

//logger::info("Hooking D3D11CreateDeviceAndSwapChain");
//*(FARPROC*)&ptrD3D11CreateDeviceAndSwapChain = GetProcAddress(GetModuleHandleA("d3d11.dll"), "D3D11CreateDeviceAndSwapChain");
//SKSE::PatchIAT(hk_D3D11CreateDeviceAndSwapChain, "d3d11.dll", "D3D11CreateDeviceAndSwapChain");
Expand Down
2 changes: 1 addition & 1 deletion src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ void Menu::addToEventQueue(KeyEvent e)
_keyEventQueue.emplace_back(e);
}

void Menu::OnFocusLost() //todo implement wndproc hook to catch WM_KILLFOCUS
void Menu::OnFocusLost()
{
std::unique_lock<std::shared_mutex> mutex(_inputEventMutex);
_keyEventQueue.clear();
Expand Down

0 comments on commit 60d2be2

Please sign in to comment.