KBM - Set treat warnings as errors, and clean up the dllmain.cpp file (#3203)

* Add test proj, refactor proj with filters, and move single remap function to a separate header

* Moved all methods to header files

* remove more unused commented code

* Undo test project addition

* Treat warnings as errors
This commit is contained in:
Arjun Balgovind
2020-05-28 14:47:32 -07:00
committed by GitHub
parent 1cbcd41b17
commit 3bb3c06456
11 changed files with 722 additions and 594 deletions

View File

@@ -2,6 +2,7 @@
#include "Helpers.h"
#include <sstream>
#include "../common/shared_constants.h"
#include <shlwapi.h>
using namespace winrt::Windows::Foundation;
@@ -163,4 +164,63 @@ namespace KeyboardManagerHelper
return L"Unexpected error";
}
}
// Function to set the value of a key event based on the arguments
void SetKeyEvent(LPINPUT keyEventArray, int index, DWORD inputType, WORD keyCode, DWORD flags, ULONG_PTR extraInfo)
{
keyEventArray[index].type = inputType;
keyEventArray[index].ki.wVk = keyCode;
keyEventArray[index].ki.dwFlags = flags;
if (IsExtendedKey(keyCode))
{
keyEventArray[index].ki.dwFlags |= KEYEVENTF_EXTENDEDKEY;
}
keyEventArray[index].ki.dwExtraInfo = extraInfo;
}
// Function to return the window in focus
HWND GetFocusWindowHandle()
{
// Using GetGUIThreadInfo for getting the process of the window in focus. GetForegroundWindow has issues with UWP apps as it returns the Application Frame Host as its linked process
GUITHREADINFO guiThreadInfo;
guiThreadInfo.cbSize = sizeof(GUITHREADINFO);
GetGUIThreadInfo(0, &guiThreadInfo);
// If no window in focus, use the active window
if (guiThreadInfo.hwndFocus == nullptr)
{
return guiThreadInfo.hwndActive;
}
return guiThreadInfo.hwndFocus;
}
// Function to return the executable name of the application in focus
std::wstring GetCurrentApplication(bool keepPath)
{
HWND current_window_handle = GetFocusWindowHandle();
DWORD process_id;
DWORD nSize = MAX_PATH;
WCHAR buffer[MAX_PATH] = { 0 };
// Get process ID of the focus window
DWORD thread_id = GetWindowThreadProcessId(current_window_handle, &process_id);
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process_id);
// Get full path of the executable
bool res = QueryFullProcessImageName(hProc, 0, buffer, &nSize);
std::wstring process_name;
CloseHandle(hProc);
process_name = buffer;
if (res)
{
PathStripPath(buffer);
if (!keepPath)
{
process_name = buffer;
}
}
return process_name;
}
}

View File

@@ -69,4 +69,13 @@ namespace KeyboardManagerHelper
// Function to return the list of key name in the order for the drop down based on the key codes
winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Foundation::IInspectable> ToBoxValue(const std::vector<std::wstring>& list);
// Function to set the value of a key event based on the arguments
void SetKeyEvent(LPINPUT keyEventArray, int index, DWORD inputType, WORD keyCode, DWORD flags, ULONG_PTR extraInfo);
// Function to return the window in focus
HWND GetFocusWindowHandle();
// Function to return the executable name of the application in focus
std::wstring GetCurrentApplication(bool keepPath);
}

View File

@@ -63,6 +63,8 @@
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>..\;..\..\..\common;..\..\..\common\telemetry;..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
<DisableSpecificWarnings>4002</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -80,6 +82,8 @@
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>..\;..\..\..\common;..\..\..\common\telemetry;..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
<DisableSpecificWarnings>4002</DisableSpecificWarnings>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@@ -76,4 +76,12 @@ namespace KeyboardManagerConstants
// Shared style constants for both Remap Table and Shortcut Table
inline const double HeaderButtonWidth = 100;
// Flags used for distinguishing key events sent by Keyboard Manager
inline const ULONG_PTR KEYBOARDMANAGER_SINGLEKEY_FLAG = 0x11;
inline const ULONG_PTR KEYBOARDMANAGER_SHORTCUT_FLAG = 0x101;
// Dummy key event used in between key up and down events to prevent certain global events from happening
inline const DWORD DUMMY_KEY = 0xFF;
}