Skip to content

Commit

Permalink
feat: add support for modifier keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-droid committed Feb 7, 2024
1 parent ecd1a07 commit 5dd4a20
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
2 changes: 1 addition & 1 deletion KeyLoggr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

# Add source to this project's executable.
add_executable (KeyLoggr WIN32 "KeyLoggr.cpp" "KeyLoggr.h")
add_executable (KeyLoggr WIN32 "KeyLoggr.cpp" "KeyLoggr.h" "Utils.cpp" "Utils.h")

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET KeyLoggr PROPERTY CXX_STANDARD 20)
Expand Down
22 changes: 19 additions & 3 deletions KeyLoggr/KeyLoggr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <windows.h>

#include "KeyLoggr.h"
#include "Utils.h"

wchar_t gLastKeyPressed[] = L"\0";
TCHAR gKeyboardMessage[16];

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
// Register the window class.
Expand Down Expand Up @@ -99,7 +100,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,

SetTextColor(hdc, RGB(255, 255, 255));
SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, gLastKeyPressed, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_MODIFYSTRING);
DrawText(hdc, gKeyboardMessage, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_MODIFYSTRING);

// Restore the original font and clean up
SelectObject(hdc, hOldFont);
Expand All @@ -124,8 +125,23 @@ LRESULT CALLBACK KeyboardProc(
if (code >= 0) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
auto *pKeyInfo = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);

wchar_t keyPressed = MapVirtualKey(pKeyInfo->vkCode, MAPVK_VK_TO_CHAR);
gLastKeyPressed[0] = keyPressed;

if ((pKeyInfo->vkCode >= 0x01) && (pKeyInfo->vkCode <= 0x2F)) {
wsprintf(gKeyboardMessage, TEXT("%s"), MapModifierKey(pKeyInfo->vkCode));
} else {
// Check if Shift etc keys are pressed
if (GetAsyncKeyState(VK_SHIFT) & 0x8000) {
wsprintf(gKeyboardMessage, TEXT("%s + %c"), MapModifierKey(VK_SHIFT) , (TCHAR) keyPressed);
} else if (GetAsyncKeyState(VK_CONTROL) & 0x8000) {
wsprintf(gKeyboardMessage, TEXT("%s + %c"), MapModifierKey(VK_CONTROL) , (TCHAR) keyPressed);
} else if (GetAsyncKeyState(VK_MENU) & 0x8000) {
wsprintf(gKeyboardMessage, TEXT("%s + %c"), MapModifierKey(VK_MENU) , (TCHAR) keyPressed);
} else {
wsprintf(gKeyboardMessage, TEXT("%c"), (TCHAR)keyPressed);
}
}

// Redraw the Keyboard Display Window
HWND hwnd = FindWindow(nullptr, L"KeyLoggrDisplay");
Expand Down
87 changes: 87 additions & 0 deletions KeyLoggr/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// spdx-license-identifier: gpl-3.0-or-later
/*
* copyright (c) 2024 chirag singla
*/

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include "Utils.h"

const wchar_t * MapModifierKey(DWORD code) {
switch (code) {
case VK_BACK:
return TEXT("Backspace");

case VK_TAB:
return TEXT("Tab");

case VK_RETURN:
return TEXT("Enter");

case VK_SHIFT:
return TEXT("Shift");

case VK_CONTROL:
return TEXT("Ctrl");

case VK_MENU:
return TEXT("Alt");

case VK_PAUSE:
return TEXT("Pause");

case VK_CAPITAL:
return TEXT("Caps");

case VK_ESCAPE:
return TEXT("ESC");

case VK_SPACE:
return TEXT("_");

case VK_PRIOR:
return TEXT("PgUp");

case VK_NEXT:
return TEXT("PgDn");

case VK_END:
return TEXT("End");

case VK_HOME:
return TEXT("Home");

case VK_LEFT:
return TEXT("Left");

case VK_UP:
return TEXT("Up");

case VK_RIGHT:
return TEXT("Right");

case VK_DOWN:
return TEXT("Down");

case VK_SELECT:
return TEXT("Select");

case VK_PRINT:
return TEXT("Print");

case VK_INSERT:
return TEXT("Ins");

case VK_DELETE:
return TEXT("Del");

case VK_HELP:
return TEXT("Help");

default:
return TEXT("-");
}
}
14 changes: 14 additions & 0 deletions KeyLoggr/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// spdx-license-identifier: gpl-3.0-or-later
/*
* copyright (c) 2024 chirag singla
*/

#pragma once

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

const wchar_t * MapModifierKey(DWORD code);

0 comments on commit 5dd4a20

Please sign in to comment.