Skip to content
This repository has been archived by the owner on Sep 6, 2020. It is now read-only.

Commit

Permalink
Added a low level mouse hook
Browse files Browse the repository at this point in the history
  • Loading branch information
loligans committed Oct 31, 2019
1 parent b8a85ab commit e1d15cf
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions PowerToys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "interface", "interface", "{3BB8493E-D18E-4485-A320-CB40F90F55AE}"
ProjectSection(SolutionItems) = preProject
src\modules\interface\lowlevel_keyboard_event_data.h = src\modules\interface\lowlevel_keyboard_event_data.h
src\modules\interface\lowlevel_mouse_event_data.h = src\modules\interface\lowlevel_mouse_event_data.h
src\modules\interface\powertoy_module_interface.h = src\modules\interface\powertoy_module_interface.h
src\modules\interface\win_hook_event_data.h = src\modules\interface\win_hook_event_data.h
EndProjectSection
Expand Down
13 changes: 13 additions & 0 deletions src/modules/interface/lowlevel_mouse_event_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <Windows.h>

namespace
{
const wchar_t* ll_mouse = L"ll_mouse";
}

struct LowlevelMouseEvent
{
MSLLHOOKSTRUCT* lParam;
WPARAM wParam;
};
45 changes: 45 additions & 0 deletions src/runner/lowlevel_mouse_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "pch.h"
#include "lowlevel_mouse_event.h"
#include "powertoys_events.h"

namespace
{
HHOOK hook_handle = nullptr;
HHOOK hook_handle_copy = nullptr; // Why should we use this in CallNextHookEx call?
LRESULT CALLBACK hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
{
LowlevelMouseEvent event;
if (nCode == HC_ACTION)
{
event.lParam = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
event.wParam = wParam;
if (powertoys_events().signal_event(ll_mouse, reinterpret_cast<intptr_t>(&event)) != 0)
{
return 1;
}
}
return CallNextHookEx(hook_handle_copy, nCode, wParam, lParam);
}
}

void start_lowlevel_mouse_hook()
{
if (!hook_handle)
{
hook_handle = SetWindowsHookEx(WH_MOUSE_LL, hook_proc, GetModuleHandle(NULL), NULL);
hook_handle_copy = hook_handle;
if (!hook_handle)
{
throw std::runtime_error("Cannot install mouse listener");
}
}
}

void stop_lowlevel_mouse_hook()
{
if (hook_handle)
{
UnhookWindowsHookEx(hook_handle);
hook_handle = nullptr;
}
}
5 changes: 5 additions & 0 deletions src/runner/lowlevel_mouse_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <interface/lowlevel_mouse_event_data.h>

void start_lowlevel_mouse_hook();
void stop_lowlevel_mouse_hook();
5 changes: 5 additions & 0 deletions src/runner/powertoys_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
#include "powertoys_events.h"
#include "lowlevel_keyboard_event.h"
#include "win_hook_event.h"
#include "lowlevel_mouse_event.h"

void first_subscribed(const std::wstring& event) {
if (event == ll_keyboard)
start_lowlevel_keyboard_hook();
else if (event == win_hook_event)
start_win_hook_event();
else if (event == ll_mouse)
start_lowlevel_mouse_hook();
}

void last_unsubscribed(const std::wstring& event) {
if (event == ll_keyboard)
stop_lowlevel_keyboard_hook();
else if (event == win_hook_event)
stop_win_hook_event();
else if (event == ll_mouse)
stop_lowlevel_mouse_hook();
}

PowertoysEvents& powertoys_events() {
Expand Down
2 changes: 2 additions & 0 deletions src/runner/runner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<ClCompile Include="auto_start_helper.cpp" />
<ClCompile Include="general_settings.cpp" />
<ClCompile Include="lowlevel_keyboard_event.cpp" />
<ClCompile Include="lowlevel_mouse_event.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
Expand All @@ -116,6 +117,7 @@
<ClInclude Include="auto_start_helper.h" />
<ClInclude Include="general_settings.h" />
<ClInclude Include="lowlevel_keyboard_event.h" />
<ClInclude Include="lowlevel_mouse_event.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="powertoys_events.h" />
<ClInclude Include="powertoy_module.h" />
Expand Down
6 changes: 6 additions & 0 deletions src/runner/runner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<ClCompile Include="win_hook_event.cpp">
<Filter>Events</Filter>
</ClCompile>
<ClCompile Include="lowlevel_mouse_event.cpp">
<Filter>Events</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -67,6 +70,9 @@
<ClInclude Include="win_hook_event.h">
<Filter>Events</Filter>
</ClInclude>
<ClInclude Include="lowlevel_mouse_event.h">
<Filter>Events</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Utils">
Expand Down

0 comments on commit e1d15cf

Please sign in to comment.