diff --git a/.gitignore b/.gitignore index b33b9db..bc52a22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,13 @@ /build /out -/CMakePresets.json \ No newline at end of file +/CMakePresets.json +/cmake-build-debug-mingw/ +/cmake-build-release-mingw/ +/cmake-build-release-visual-studio/ +/.idea/codeStyles/codeStyleConfig.xml +/.idea/editor.xml +/.idea/HighBloodPressure.iml +/.idea/misc.xml +/.idea/modules.xml +/.idea/codeStyles/Project.xml +/.idea/vcs.xml diff --git a/InteractManager.cpp b/InteractManager.cpp index d5e60f5..508683a 100644 --- a/InteractManager.cpp +++ b/InteractManager.cpp @@ -263,3 +263,5 @@ InteractManager::InteractManager() { keyStatus[0xFE].name = L"OEM-Clear"; } +KeyStatus& InteractManager::getKey(const KeyBinding& binding) noexcept { return keyStatus[binding.keyCode]; } + diff --git a/InteractManager.h b/InteractManager.h index a2c6a82..588dd36 100644 --- a/InteractManager.h +++ b/InteractManager.h @@ -9,29 +9,72 @@ struct KeyStatus { String name; unsigned int pressTimes = 0; - bool isPressed = false; + bool pressed = false; bool notDealt = false; - [[nodiscard]] String toString() const noexcept { - return L"name: " + name + L" pressTimes: " + std::to_wstring(pressTimes) + L" isPressed"; + [[nodiscard]] bool isPressed() const noexcept { return pressed; } + [[nodiscard]] unsigned int wasPressed() const noexcept { return pressTimes; } + + void deals() noexcept { + notDealt = false; + pressTimes = 0; } + + [[nodiscard]] String toString() const noexcept { return L"KeyStatus: { name = \"" + name + L"\"; pressTimes = " + std::to_wstring(pressTimes) + L"; pressed = " + (pressed ? L"true }" : L"false }"); } }; +struct KeyBinding; + class InteractManager { KeyStatus keyStatus[256]; - + int mouseX = 0, mouseY = 0; + int mouseWheel = 0; + int rebindResult = 0; + bool rebinding = false; + bool inWindow = false; // 鼠标是否在窗口内部 public: explicit InteractManager(); void update(const int keyCode, const bool isPressed) noexcept { if (keyCode >= 256) return; - keyStatus[keyCode].isPressed = isPressed; + if (rebinding) { + rebindResult = keyCode; + return; + } + keyStatus[keyCode].pressed = isPressed; if (isPressed) { keyStatus[keyCode].pressTimes++; keyStatus[keyCode].notDealt = true; } - std::wcout << keyStatus[keyCode].toString() << std::endl; + } + + void updateMouse(const int x, const int y) noexcept { + mouseX = x; + mouseY = y; + std::wcout << x << " " << y << std::endl; + } + + void updateWheel(const int wheel) noexcept { mouseWheel += wheel; } + + [[nodiscard]] int getMouseX() const noexcept { return mouseX; } + [[nodiscard]] int getMouseY() const noexcept { return mouseY; } + [[nodiscard]] int getMouseWheel() const noexcept { return mouseWheel; } + KeyStatus& getKey(const int keyCode) noexcept { return keyStatus[keyCode]; } + KeyStatus& getKey(const KeyBinding& binding) noexcept; + + int dealMouseWheel() noexcept { + const int ret = mouseWheel; + mouseWheel = 0; + return ret; } }; inline static InteractManager interactManager = InteractManager(); + +struct KeyBinding { + String id; + int keyCode; + [[nodiscard]] bool isPressed() const noexcept { return interactManager.getKey(keyCode).isPressed(); } + [[nodiscard]] unsigned int wasPressed() const noexcept { return interactManager.getKey(keyCode).wasPressed(); } + void deals() const noexcept { interactManager.getKey(keyCode).deals(); } +}; diff --git a/def.h b/def.h index 62dbc4d..6f13fae 100644 --- a/def.h +++ b/def.h @@ -8,6 +8,8 @@ #include #include +using wchar = wchar_t; +using QWORD = unsigned long long int; using String = std::wstring; template, typename Alloc = std::allocator>> using Map = std::map; @@ -28,3 +30,16 @@ template, typename Alloc = s #define NOMINMAX #include +#include +#include +#include +#include +#include +#include +#include +#include + +#pragma comment(lib, "ws2_32.lib") +#pragma comment(lib, "dwmapi.lib") +#pragma comment(lib, "Uxtheme.lib") +#pragma comment(lib, "winmm.lib") diff --git a/hbp.h b/hbp.h index cf43745..bb4d142 100644 --- a/hbp.h +++ b/hbp.h @@ -13,7 +13,7 @@ inline BOOL NewProcess(const String& cmdline) noexcept { return CreateProcessW(nullptr, const_cast(cmdline.c_str()), nullptr, nullptr, 0, 0, nullptr, nullptr, &si, &pi); } -inline HRESULT RemoveDefaultCaption(const HWND hWnd, const MARGINS* p) noexcept { return DwmExtendFrameIntoClientArea(hWnd, p); } +inline HRESULT RemoveDefaultCaption(HWND const hWnd, const MARGINS* p) noexcept { return DwmExtendFrameIntoClientArea(hWnd, p); } inline void ShowConsoleIO() noexcept { AllocConsole(); diff --git a/main.cpp b/main.cpp index ba51dd7..fcefc1d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include -#include - #include "hbp.h" #include "InteractManager.h" @@ -20,6 +11,20 @@ LRESULT __stdcall WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { EndPaint(hwnd, &ps); break; } + _LIKELY + case WM_NCHITTEST: { + LRESULT lr = 0; + BOOL r = DwmDefWindowProc(hwnd, uMsg, wParam, lParam, &lr); + return HTCLIENT; + } + case WM_KEYDOWN: interactManager.update(static_cast(wParam), true); + break; + case WM_KEYUP: interactManager.update(static_cast(wParam), false); + break; + case WM_SYSKEYDOWN: interactManager.update(static_cast(wParam), true); + break; + case WM_SYSKEYUP: interactManager.update(static_cast(wParam), false); + break; case WM_LBUTTONDOWN: interactManager.update(VK_LBUTTON, true); break; case WM_RBUTTONDOWN: interactManager.update(VK_RBUTTON, true); @@ -28,17 +33,14 @@ LRESULT __stdcall WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { break; case WM_RBUTTONUP: interactManager.update(VK_RBUTTON, false); break; - _LIKELY - case WM_NCHITTEST: { - LRESULT lr = 0; - BOOL r = DwmDefWindowProc(hwnd, uMsg, wParam, lParam, &lr); - return lr; - } - case WM_MOUSEMOVE: { break; } - case WM_MBUTTONDOWN: interactManager.update(VK_MBUTTON, true); + case WM_MOUSEMOVE: interactManager.updateMouse(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); + break; + case WM_MBUTTONDOWN: if (wParam & 0x10) interactManager.update(VK_MBUTTON, true); break; case WM_MBUTTONUP: interactManager.update(VK_MBUTTON, false); break; + case WM_MOUSEWHEEL: interactManager.update(VK_MBUTTON, true); + break; case WM_COMMAND: std::wcout << L"WM_COMMAND" << std::endl; break; case WM_DWMCOMPOSITIONCHANGED: { @@ -60,16 +62,16 @@ LRESULT __stdcall WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return 0; } case WM_SIZE: switch (wParam) { - case SIZE_RESTORED: break; - case SIZE_MINIMIZED: break; - case SIZE_MAXIMIZED: break; + case SIZE_RESTORED: + case SIZE_MINIMIZED: + case SIZE_MAXIMIZED: case SIZE_MAXSHOW: case SIZE_MAXHIDE: default: break; } break; case WM_ACTIVATE: { - MARGINS margins{ + constexpr MARGINS margins{ .cxLeftWidth = 0, .cxRightWidth = 0, .cyTopHeight = 0, @@ -91,6 +93,7 @@ LRESULT __stdcall WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { int __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { AllocConsole(); + freopen("CONOUT$", "w", stdout); WNDCLASSEX wc = { 0 }; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; @@ -104,6 +107,7 @@ int __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCm wc.lpszMenuName = L"None"; wc.lpszClassName = ApplicationName.c_str(); if (!RegisterClassExW(&wc)) return FALSE; + if (!SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) std::wcout << L"SetProcessDpiAwarenessContext failed. LastError: " << GetLastError() << std::endl; MainInstance = hInstance; MainWindowHandle = CreateWindowExW(0, wc.lpszClassName, wc.lpszClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr); ShowWindow(MainWindowHandle, nCmdShow);