Skip to content

Commit

Permalink
Fixed sensitivity slowdown by using a timer, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Som1Lse committed Aug 23, 2017
1 parent 87e41ae commit e41d73d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.0)
project(Dish2Macro VERSION 1.1.0)
project(Dish2Macro VERSION 1.1.1)

include(CMakeToolsHelpers OPTIONAL)

Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ Oh, and don't forget to bind `wheel down` to jump in Dishonored 2.

# Is this allowed in speedruns?

Not yet.

# Why is the sensitivity suddenly so low?

If your mouse has a high polling rate, the mouse hook will, for some reason, slow the ingame sensitivity. I don't know why. Either reduce the mouse polling rate, or bind the macro to a keyboard key instead.
Yes.
56 changes: 30 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#define _CRT_SECURE_NO_WARNINGS

#include <atomic>

#include <cassert>
#include <cstdio>
#include <cwchar>

#include <windows.h>

HHOOK Hook;
bool ShouldJump = false;
std::atomic<bool> ShouldJump = false;
unsigned KeyCode;

bool IsDishonored2InFocus(){
bool IsGameInFocus(){
auto Window = GetForegroundWindow();

wchar_t ClassName[14];
Expand Down Expand Up @@ -72,12 +74,10 @@ LRESULT CALLBACK LowLevelMouseProc(int Code,WPARAM WParam,LPARAM LParam){
}
}

if(ButtonCode == KeyCode){
if(ButtonCode == KeyCode && IsGameInFocus()){
ShouldJump = IsDown;

if(IsDishonored2InFocus()){
return 1;//stop propagation
}
return 1;//stop propagation
}

return CallNextHookEx(Hook,Code,WParam,LParam);
Expand All @@ -88,20 +88,24 @@ LRESULT CALLBACK LowLevelKeyboardProc(int Code,WPARAM WParam,LPARAM LParam){

auto& Info = *reinterpret_cast<KBDLLHOOKSTRUCT*>(LParam);

if(Info.vkCode == KeyCode){
if(Info.vkCode == KeyCode && IsGameInFocus()){
ShouldJump = (WParam == WM_KEYDOWN || WParam == WM_SYSKEYDOWN);

if(IsDishonored2InFocus()){
return 1;//stop propagation
}
return 1;//stop propagation
}

return CallNextHookEx(Hook,Code,WParam,LParam);
}

void SendJump(){
if(IsDishonored2InFocus()){
mouse_event(MOUSEEVENTF_WHEEL,0,0,-WHEEL_DELTA,0);
mouse_event(MOUSEEVENTF_WHEEL,0,0,-WHEEL_DELTA,0);
}

void CALLBACK TimerProc(void*,BOOLEAN){
if(!IsGameInFocus()){
ShouldJump = false;
}else if(ShouldJump){
SendJump();
}
}

Expand All @@ -122,33 +126,33 @@ int main(){

std::fclose(File);

if((KeyCode >= VK_LBUTTON && KeyCode <= VK_RBUTTON) || (KeyCode >= VK_MBUTTON && KeyCode <= VK_XBUTTON2)){
auto IsMouseButton = (KeyCode >= VK_LBUTTON && KeyCode <= VK_RBUTTON) ||
(KeyCode >= VK_MBUTTON && KeyCode <= VK_XBUTTON2);

if(IsMouseButton){
Hook = SetWindowsHookExW(WH_MOUSE_LL,LowLevelMouseProc,nullptr,0);
}else{
Hook = SetWindowsHookExW(WH_KEYBOARD_LL,LowLevelKeyboardProc,nullptr,0);
}

if(!Hook){
std::fprintf(stderr,"Unable to keyboard hook.\nError code: 0x%08lX\n",GetLastError());
std::fprintf(stderr,"Unable to set hook.\nError code: 0x%08lX\n",GetLastError());
std::getchar();
return 0;
}

for(;;){
Sleep(5);
HANDLE Timer;
if(!CreateTimerQueueTimer(&Timer,nullptr,TimerProc,nullptr,0,5,WT_EXECUTEDEFAULT)){
std::fprintf(stderr,"Unable to create timer.\nError code: 0x%08lX\n",GetLastError());
std::getchar();
return 0;
}

for(;;){
MSG Message;
while(PeekMessage(&Message,0,0,0,PM_REMOVE)){
while(GetMessageW(&Message,0,0,0)){
TranslateMessage(&Message);
DispatchMessage(&Message);
}

if(!IsDishonored2InFocus()){
ShouldJump = false;
}

if(ShouldJump){
SendJump();
DispatchMessageW(&Message);
}
}

Expand Down

0 comments on commit e41d73d

Please sign in to comment.