1
0
mirror of https://github.com/narzoul/DDrawCompat synced 2024-12-30 08:55:36 +01:00

Added TerminateHotKey setting

This commit is contained in:
narzoul 2022-12-04 22:36:28 +01:00
parent 88708ce398
commit 0c7358b449
8 changed files with 71 additions and 10 deletions

View File

@ -30,6 +30,7 @@ namespace Config
Settings::SpriteTexCoord spriteTexCoord; Settings::SpriteTexCoord spriteTexCoord;
Settings::StatsHotKey statsHotKey; Settings::StatsHotKey statsHotKey;
Settings::SupportedResolutions supportedResolutions; Settings::SupportedResolutions supportedResolutions;
Settings::TerminateHotKey terminateHotKey;
Settings::TextureFilter textureFilter; Settings::TextureFilter textureFilter;
Settings::ThreadPriorityBoost threadPriorityBoost; Settings::ThreadPriorityBoost threadPriorityBoost;
Settings::VSync vSync; Settings::VSync vSync;

View File

@ -28,6 +28,7 @@
#include <Config/Settings/SpriteTexCoord.h> #include <Config/Settings/SpriteTexCoord.h>
#include <Config/Settings/StatsHotKey.h> #include <Config/Settings/StatsHotKey.h>
#include <Config/Settings/SupportedResolutions.h> #include <Config/Settings/SupportedResolutions.h>
#include <Config/Settings/TerminateHotKey.h>
#include <Config/Settings/TextureFilter.h> #include <Config/Settings/TextureFilter.h>
#include <Config/Settings/ThreadPriorityBoost.h> #include <Config/Settings/ThreadPriorityBoost.h>
#include <Config/Settings/VSync.h> #include <Config/Settings/VSync.h>
@ -63,6 +64,7 @@ namespace Config
extern Settings::SpriteTexCoord spriteTexCoord; extern Settings::SpriteTexCoord spriteTexCoord;
extern Settings::StatsHotKey statsHotKey; extern Settings::StatsHotKey statsHotKey;
extern Settings::SupportedResolutions supportedResolutions; extern Settings::SupportedResolutions supportedResolutions;
extern Settings::TerminateHotKey terminateHotKey;
extern Settings::TextureFilter textureFilter; extern Settings::TextureFilter textureFilter;
extern Settings::ThreadPriorityBoost threadPriorityBoost; extern Settings::ThreadPriorityBoost threadPriorityBoost;
extern Settings::VSync vSync; extern Settings::VSync vSync;

View File

@ -0,0 +1,15 @@
#pragma once
#include <Config/HotKeySetting.h>
namespace Config
{
namespace Settings
{
class TerminateHotKey : public HotKeySetting
{
public:
TerminateHotKey() : HotKeySetting("TerminateHotKey", "ctrl+alt+end") {}
};
}
}

View File

@ -187,6 +187,7 @@
<ClInclude Include="Config\Settings\SpriteTexCoord.h" /> <ClInclude Include="Config\Settings\SpriteTexCoord.h" />
<ClInclude Include="Config\Settings\StatsHotKey.h" /> <ClInclude Include="Config\Settings\StatsHotKey.h" />
<ClInclude Include="Config\Settings\SupportedResolutions.h" /> <ClInclude Include="Config\Settings\SupportedResolutions.h" />
<ClInclude Include="Config\Settings\TerminateHotKey.h" />
<ClInclude Include="Config\Settings\TextureFilter.h" /> <ClInclude Include="Config\Settings\TextureFilter.h" />
<ClInclude Include="Config\Settings\ThreadPriorityBoost.h" /> <ClInclude Include="Config\Settings\ThreadPriorityBoost.h" />
<ClInclude Include="Config\Settings\VSync.h" /> <ClInclude Include="Config\Settings\VSync.h" />

View File

@ -630,6 +630,9 @@
<ClInclude Include="Config\Settings\StatsHotKey.h"> <ClInclude Include="Config\Settings\StatsHotKey.h">
<Filter>Header Files\Config\Settings</Filter> <Filter>Header Files\Config\Settings</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Config\Settings\TerminateHotKey.h">
<Filter>Header Files\Config\Settings</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Gdi\Gdi.cpp"> <ClCompile Include="Gdi\Gdi.cpp">

View File

@ -148,6 +148,15 @@ namespace
return names; return names;
}(); }();
const std::map<std::string, UINT> g_altKeyNames = {
{ "alt", VK_MENU },
{ "lalt", VK_LMENU },
{ "ralt", VK_RMENU },
{ "ctrl", VK_CONTROL },
{ "lctrl", VK_LCONTROL },
{ "rctrl", VK_RCONTROL }
};
bool areExcludedModifierKeysDown(const std::set<UINT>& modifiers, UINT either, UINT left, UINT right) bool areExcludedModifierKeysDown(const std::set<UINT>& modifiers, UINT either, UINT left, UINT right)
{ {
return modifiers.find(either) == modifiers.end() && return modifiers.find(either) == modifiers.end() &&
@ -156,6 +165,12 @@ namespace
(GetAsyncKeyState(either) & 0x8000); (GetAsyncKeyState(either) & 0x8000);
} }
UINT getKeyCode(const std::string& name, const std::map<std::string, UINT>& keyNames)
{
auto it = keyNames.find(name);
return it != keyNames.end() ? it->second : 0;
}
UINT getKeyCode(const std::string& name) UINT getKeyCode(const std::string& name)
{ {
if (1 == name.length()) if (1 == name.length())
@ -167,12 +182,22 @@ namespace
} }
} }
auto it = g_keyNames.find(name); auto code = getKeyCode(name, g_altKeyNames);
if (it == g_keyNames.end()) if (0 == code)
{
code = getKeyCode(name, g_keyNames);
if (0 == code)
{ {
throw Config::ParsingError("Invalid hotkey: '" + name + "'"); throw Config::ParsingError("Invalid hotkey: '" + name + "'");
} }
return it->second; }
return code;
}
std::string getKeyName(UINT key, const std::map<std::string, UINT>& keyNames)
{
auto it = std::find_if(keyNames.begin(), keyNames.end(), [&](const auto& pair) { return pair.second == key; });
return it != keyNames.end() ? it->first : std::string();
} }
std::string getKeyName(UINT key) std::string getKeyName(UINT key)
@ -183,8 +208,12 @@ namespace
return std::string(1, static_cast<char>(std::tolower(key, std::locale()))); return std::string(1, static_cast<char>(std::tolower(key, std::locale())));
} }
auto it = std::find_if(g_keyNames.begin(), g_keyNames.end(), [&](const auto& pair) { return pair.second == key; }); auto name(getKeyName(key, g_altKeyNames));
return it != g_keyNames.end() ? it->first : "none"; if (name.empty())
{
name = getKeyName(key, g_keyNames);
}
return name.empty() ? "none" : name;
} }
} }

View File

@ -7,6 +7,7 @@
#include <Common/Hook.h> #include <Common/Hook.h>
#include <Common/Log.h> #include <Common/Log.h>
#include <Config/Config.h>
#include <Dll/Dll.h> #include <Dll/Dll.h>
#include <DDraw/RealPrimarySurface.h> #include <DDraw/RealPrimarySurface.h>
#include <Gdi/GuiThread.h> #include <Gdi/GuiThread.h>
@ -21,6 +22,7 @@ namespace
{ {
std::function<void(void*)> action; std::function<void(void*)> action;
void* context; void* context;
bool onKeyDown;
}; };
HANDLE g_bmpArrow = nullptr; HANDLE g_bmpArrow = nullptr;
@ -93,7 +95,7 @@ namespace
{ {
if (hotkey.first.vk == llHook->vkCode && Input::areModifierKeysDown(hotkey.first.modifiers)) if (hotkey.first.vk == llHook->vkCode && Input::areModifierKeysDown(hotkey.first.modifiers))
{ {
if (WM_KEYDOWN == wParam || WM_SYSKEYDOWN == wParam) if (hotkey.second.onKeyDown == (WM_KEYDOWN == wParam || WM_SYSKEYDOWN == wParam))
{ {
hotkey.second.action(hotkey.second.context); hotkey.second.action(hotkey.second.context);
} }
@ -146,6 +148,12 @@ namespace
return CallNextHookEx(nullptr, nCode, wParam, lParam); return CallNextHookEx(nullptr, nCode, wParam, lParam);
} }
void onTerminate(void* /*context*/)
{
LOG_INFO << "Terminating application via TerminateHotKey";
TerminateProcess(GetCurrentProcess(), 0);
}
void resetKeyboardHook() void resetKeyboardHook()
{ {
Gdi::GuiThread::execute([]() Gdi::GuiThread::execute([]()
@ -251,13 +259,15 @@ namespace Input
HOOK_FUNCTION(user32, SetWindowsHookExA, setWindowsHookExA); HOOK_FUNCTION(user32, SetWindowsHookExA, setWindowsHookExA);
HOOK_FUNCTION(user32, SetWindowsHookExW, setWindowsHookExW); HOOK_FUNCTION(user32, SetWindowsHookExW, setWindowsHookExW);
registerHotKey(Config::terminateHotKey.get(), onTerminate, nullptr, false);
} }
void registerHotKey(const HotKey& hotKey, std::function<void(void*)> action, void* context) void registerHotKey(const HotKey& hotKey, std::function<void(void*)> action, void* context, bool onKeyDown)
{ {
if (0 != hotKey.vk) if (0 != hotKey.vk)
{ {
g_hotKeys[hotKey] = { action, context }; g_hotKeys[hotKey] = { action, context, onKeyDown };
if (!g_keyboardHook) if (!g_keyboardHook)
{ {
resetKeyboardHook(); resetKeyboardHook();

View File

@ -21,7 +21,7 @@ namespace Input
POINT getCursorPos(); POINT getCursorPos();
HWND getCursorWindow(); HWND getCursorWindow();
void installHooks(); void installHooks();
void registerHotKey(const HotKey& hotKey, std::function<void(void*)> action, void* context); void registerHotKey(const HotKey& hotKey, std::function<void(void*)> action, void* context, bool onKeydown = true);
void setCapture(Overlay::Control* control); void setCapture(Overlay::Control* control);
void updateCursor(); void updateCursor();
} }