Skip to content

Commit

Permalink
Clamp mouse in window instead enabling with hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobakanoosh committed Dec 16, 2022
1 parent fb25add commit b39f3d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
8 changes: 8 additions & 0 deletions BrawlhallaMouseCenterer/BrawlhallaMouseCenterer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -102,6 +104,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -116,6 +120,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -130,6 +136,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
56 changes: 31 additions & 25 deletions BrawlhallaMouseCenterer/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,52 @@
#include <chrono>
#include <thread>
#include <iostream>
#include <algorithm>

#define WINDOW_CLASS "ApolloRuntimeContentWindow"
constexpr auto WINDOW_CLASS = "ApolloRuntimeContentWindow";
constexpr auto MOUSE_POSITION_BUFFER = 40;
constexpr auto WINDOW_UPDATE_INTERVAL = std::chrono::milliseconds(100);
constexpr auto WINDOW_SEEK_INTERVAL = std::chrono::seconds(5);

int main()
{
bool enabled = false;
auto lastWindowTime = std::chrono::steady_clock::now();
HWND window = NULL;

printf("Cursor locked inside brawlhalla");

while (true)
{
if (GetAsyncKeyState(VK_F5))
if (window != GetForegroundWindow())
{
enabled = !enabled;
printf("%s\n", enabled ? "enabled" : "disabled");
auto currentWindowTime = std::chrono::steady_clock::now();
if ((currentWindowTime - lastWindowTime).count() >= WINDOW_UPDATE_INTERVAL.count())
{
lastWindowTime = currentWindowTime;
window = FindWindowA(WINDOW_CLASS, nullptr);
}

std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}

if (enabled)
if (!window)
{
HWND window = FindWindowA(WINDOW_CLASS, nullptr);
if (!window)
{
printf("Window not open\n");
std::this_thread::sleep_for(std::chrono::seconds(5));
continue;
}
printf("Window not open\n");
std::this_thread::sleep_for(WINDOW_SEEK_INTERVAL);
continue;
}

if (window != GetForegroundWindow())
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
RECT rect;
GetWindowRect(window, &rect);

RECT rect;
GetWindowRect(window, &rect);
POINT point;
GetCursorPos(&point);

const int mouseX = rect.left + 50;
const int mouseY = rect.top + 500;
const int mouseX = std::clamp(point.x, rect.left + MOUSE_POSITION_BUFFER, rect.right - MOUSE_POSITION_BUFFER);
const int mouseY = std::clamp(point.y, rect.top + MOUSE_POSITION_BUFFER, rect.bottom - MOUSE_POSITION_BUFFER);

SetCursorPos(mouseX, mouseY);
}
SetCursorPos(mouseX, mouseY);
}

return 0;
Expand Down

0 comments on commit b39f3d4

Please sign in to comment.