Skip to content

Commit 1e4c27d

Browse files
authored
Merge pull request #2 from developer239/feat/windows-support
Feat/windows support
2 parents 79adaa6 + b78f3da commit 1e4c27d

11 files changed

Lines changed: 243 additions & 79 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
cmake_minimum_required(VERSION 3.24)
1+
cmake_minimum_required(VERSION 3.21)
22

33
project(RobotCPP)
44

55
set(CMAKE_CXX_STANDARD 23)
66
set(LIB_NAME RobotCPP)
77

8-
set(SOURCES src/ActionRecorder.h src/EventHook.h src/types.h src/Mouse.h src/Mouse.cpp src/Utils.h src/Utils.cpp src/Keyboard.cpp src/Keyboard.h src/Screen.cpp src/Screen.h)
9-
set(SOURCES_LODEPNG externals/lodepng/lodepng.cpp externals/lodepng/lodepng.h)
8+
set(COMMON_SOURCES
9+
src/ActionRecorder.h
10+
src/types.h
11+
src/Mouse.h
12+
src/Mouse.cpp
13+
src/Utils.h
14+
src/Utils.cpp
15+
src/Keyboard.cpp
16+
src/Keyboard.h
17+
src/Screen.cpp
18+
src/Screen.h)
1019

11-
add_library(${LIB_NAME} STATIC ${SOURCES} ${SOURCES_LODEPNG})
12-
target_include_directories(${LIB_NAME} PUBLIC src PRIVATE externals/lodepng)
20+
set(SOURCES_LODEPNG
21+
externals/lodepng/lodepng.cpp
22+
externals/lodepng/lodepng.h)
1323

1424
if (APPLE)
15-
target_link_libraries(${LIB_NAME}
16-
"-framework Carbon"
17-
)
25+
list(APPEND PLATFORM_SOURCES src/EventHookMacOS.h)
26+
find_library(CARBON_LIBRARY Carbon)
27+
mark_as_advanced(CARBON_LIBRARY)
28+
list(APPEND PLATFORM_LIBRARIES ${CARBON_LIBRARY})
29+
elseif (WIN32)
30+
list(APPEND PLATFORM_SOURCES src/EventHookWindows.h)
1831
endif ()
32+
33+
add_library(${LIB_NAME} STATIC ${COMMON_SOURCES} ${PLATFORM_SOURCES} ${SOURCES_LODEPNG})
34+
target_include_directories(${LIB_NAME} PUBLIC src PRIVATE externals/lodepng)
35+
target_link_libraries(${LIB_NAME} ${PLATFORM_LIBRARIES})

‎README.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Robot CPP
22

33
![master](https://github.com/developer239/robot-cpp/actions/workflows/ci.yml/badge.svg)
4+
![Windows](https://img.shields.io/badge/Windows-0078D6?style=for-the-badge&logo=windows&logoColor=white)
5+
![macOS](https://img.shields.io/badge/mac%20os-000000?style=for-the-badge&logo=macos&logoColor=F0F0F0)
46

57
This library is inspired by older unmaintained libraries like [octalmage/robotjs](https://github.com/octalmage/robotjs)
68
and [Robot/robot-js](https://github.com/Robot/robot-js). The goal is to provide cross-platform controls for various
@@ -9,7 +11,7 @@ devices such as keyboard, mouse, and screen for C++ applications.
911
**Supported system:**
1012

1113
- MacOS
12-
- Windows **(not tested yet)**
14+
- Windows
1315

1416
In case of Linux, please, create issue and leave a star and I will implement support. Right now I want to focus on port to
1517
Node.js using Node-API.
@@ -139,6 +141,7 @@ The `Keyboard` class provides a static interface for simulating keyboard key pre
139141
#include "robot.h"
140142
141143
int main() {
144+
// Note that this will type the text in lower case and likely without special characters like !@#$%^&*()
142145
Robot::Keyboard::TypeHumanLike("Hello, World");
143146
}
144147
```
@@ -206,7 +209,9 @@ int main() {
206209
}
207210
```
208211

209-
## Record and Replay Classes [MacOS Only]
212+
## Record and Replay Keybaord and Mouse Actions
213+
214+
**Note:** It seems that recorded mouse position is slightly shifted on Windows.
210215

211216
The `ActionRecorder` and `EventHook` classes provide functionality for recording user actions (such as mouse clicks and keyboard key presses) and replaying them later.
212217

‎example/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.24)
1+
cmake_minimum_required(VERSION 3.21)
22

33
project(RobotCPPExample)
44

‎example/main.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#include <EventHook.h>
21
#include <Utils.h>
32
#include <iostream>
3+
#include <ActionRecorder.h>
4+
// Comment out to test on MacOS
5+
#include <EventHookWindows.h>
6+
// Uncomment to test on MacOS
7+
// #include <EventHookMacOS.h>
48

59
int main() {
610
int recordFor = 10;
File renamed without changes.

‎src/EventHookWindows.h‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
3+
#include <windows.h>
4+
#include <iostream>
5+
#include "./ActionRecorder.h"
6+
7+
namespace Robot {
8+
9+
class EventHook {
10+
public:
11+
explicit EventHook(ActionRecorder& recorder) : recorder(recorder) {
12+
instance = this; // Store the instance pointer in the static variable
13+
}
14+
15+
void StartRecording() {
16+
HINSTANCE hInstance = GetModuleHandle(NULL);
17+
if (!hInstance) {
18+
std::cerr << "Failed to get instance handle" << std::endl;
19+
exit(1);
20+
}
21+
22+
mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, 0);
23+
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, 0);
24+
25+
if (!mouseHook || !keyboardHook) {
26+
std::cerr << "Failed to set hooks" << std::endl;
27+
exit(1);
28+
}
29+
30+
recordingThreadId = GetCurrentThreadId();
31+
32+
MSG msg;
33+
while (GetMessage(&msg, NULL, 0, 0)) {
34+
if (msg.message == WM_QUIT) {
35+
break;
36+
}
37+
TranslateMessage(&msg);
38+
DispatchMessage(&msg);
39+
}
40+
}
41+
42+
void StopRecording() {
43+
UnhookWindowsHookEx(mouseHook);
44+
UnhookWindowsHookEx(keyboardHook);
45+
46+
PostThreadMessage(recordingThreadId, WM_QUIT, 0, 0);
47+
}
48+
49+
private:
50+
DWORD recordingThreadId;
51+
52+
ActionRecorder& recorder;
53+
HHOOK mouseHook;
54+
HHOOK keyboardHook;
55+
static EventHook* instance; // Static variable to store instance pointer
56+
57+
static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
58+
if (nCode == HC_ACTION && instance) {
59+
auto* mouseData = reinterpret_cast<MSLLHOOKSTRUCT*>(lParam);
60+
switch (wParam) {
61+
case WM_MOUSEMOVE:
62+
instance->recorder.RecordMouseMove(mouseData->pt.x, mouseData->pt.y);
63+
break;
64+
case WM_LBUTTONDOWN:
65+
instance->recorder.RecordPressLeft(mouseData->pt.x, mouseData->pt.y);
66+
break;
67+
case WM_LBUTTONUP:
68+
instance->recorder.RecordReleaseLeft(mouseData->pt.x, mouseData->pt.y);
69+
break;
70+
}
71+
}
72+
return CallNextHookEx(NULL, nCode, wParam, lParam);
73+
}
74+
75+
static LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
76+
if (nCode == HC_ACTION && instance) {
77+
auto* keyboardData = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam);
78+
switch (wParam) {
79+
case WM_KEYDOWN:
80+
instance->recorder.RecordKeyPress(keyboardData->vkCode);
81+
break;
82+
case WM_KEYUP:
83+
instance->recorder.RecordKeyRelease(keyboardData->vkCode);
84+
break;
85+
}
86+
}
87+
return CallNextHookEx(NULL, nCode, wParam, lParam);
88+
}
89+
};
90+
91+
// Initialize the static instance pointer
92+
EventHook* EventHook::instance = nullptr;
93+
94+
} // namespace Robot

‎src/Keyboard.cpp‎

Lines changed: 100 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <iostream>
66
#include <random>
7+
#include <map>
78

89
#include "./Keyboard.h"
910
#include "./Utils.h"
@@ -228,6 +229,105 @@ std::map<Keyboard::SpecialKey, KeyCode> Keyboard::specialKeyToVirtualKeyMap = {
228229
{Keyboard::CAPSLOCK, kVK_CapsLock}};
229230
#endif
230231

232+
char Keyboard::VirtualKeyToAscii(KeyCode virtualKey) {
233+
#ifdef __APPLE__
234+
auto map = asciiToVirtualKeyMap;
235+
auto it = std::find_if(
236+
map.begin(),
237+
map.end(),
238+
[virtualKey](const std::pair<char, int> &p) {
239+
return p.second == virtualKey;
240+
}
241+
);
242+
243+
if (it == map.end()) {
244+
return INVALID_ASCII;
245+
}
246+
247+
return it->first;
248+
#endif
249+
250+
#ifdef _WIN32
251+
// Convert the virtual key code to a scan code
252+
UINT scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
253+
254+
// Convert the scan code to the corresponding character
255+
char character = 0;
256+
BYTE keyboardState[256] = {0};
257+
GetKeyboardState(keyboardState);
258+
wchar_t buffer[2];
259+
if (ToUnicode(virtualKey, scanCode, keyboardState, buffer, 2, 0) == 1) {
260+
character = static_cast<char>(buffer[0]);
261+
}
262+
return character;
263+
#endif
264+
}
265+
266+
Keyboard::SpecialKey Keyboard::VirtualKeyToSpecialKey(KeyCode virtualKey) {
267+
#ifdef __APPLE__
268+
switch (virtualKey) {
269+
case 123:
270+
return Keyboard::LEFT;
271+
case 124:
272+
return Keyboard::RIGHT;
273+
case 125:
274+
return Keyboard::DOWN;
275+
case 126:
276+
return Keyboard::UP;
277+
case 36:
278+
return Keyboard::ENTER;
279+
case 48:
280+
return Keyboard::TAB;
281+
case 51:
282+
return Keyboard::BACKSPACE;
283+
case 53:
284+
return Keyboard::ESCAPE;
285+
case 55:
286+
return Keyboard::META;
287+
case 56:
288+
return Keyboard::SHIFT;
289+
case 57:
290+
return Keyboard::CAPSLOCK;
291+
case 58:
292+
return Keyboard::ALT;
293+
case 59:
294+
return Keyboard::CONTROL;
295+
}
296+
#endif
297+
298+
#ifdef _WIN32
299+
switch (virtualKey) {
300+
case VK_LEFT:
301+
return Keyboard::LEFT;
302+
case VK_RIGHT:
303+
return Keyboard::RIGHT;
304+
case VK_DOWN:
305+
return Keyboard::DOWN;
306+
case VK_UP:
307+
return Keyboard::UP;
308+
case VK_RETURN:
309+
return Keyboard::ENTER;
310+
case VK_TAB:
311+
return Keyboard::TAB;
312+
case VK_BACK:
313+
return Keyboard::BACKSPACE;
314+
case VK_ESCAPE:
315+
return Keyboard::ESCAPE;
316+
case VK_LWIN:
317+
case VK_RWIN:
318+
return Keyboard::META;
319+
case VK_SHIFT:
320+
return Keyboard::SHIFT;
321+
case VK_CAPITAL:
322+
return Keyboard::CAPSLOCK;
323+
case VK_MENU:
324+
return Keyboard::ALT;
325+
case VK_CONTROL:
326+
return Keyboard::CONTROL;
327+
}
328+
#endif
329+
}
330+
231331
#ifdef _WIN32
232332
std::map<Keyboard::SpecialKey, KeyCode> Keyboard::specialKeyToVirtualKeyMap = {
233333
{Keyboard::BACKSPACE, VK_BACK},
@@ -284,62 +384,5 @@ std::map<char, int> Keyboard::asciiToVirtualKeyMap = {
284384
{'\\', kVK_ANSI_Backslash}, {39, kVK_ANSI_Quote}, // single quote
285385
{';', kVK_ANSI_Semicolon}, {',', kVK_ANSI_Comma},
286386
{'.', kVK_ANSI_Period}, {'/', kVK_ANSI_Slash}};
287-
288-
char Keyboard::VirtualKeyToAscii(KeyCode virtualKey) {
289-
#ifdef __APPLE__
290-
auto map = asciiToVirtualKeyMap;
291-
auto it = std::find_if(
292-
map.begin(),
293-
map.end(),
294-
[virtualKey](const std::pair<char, int> &p) {
295-
return p.second == virtualKey;
296-
}
297-
);
298-
299-
if (it == map.end()) {
300-
return INVALID_ASCII;
301-
}
302-
303-
return it->first;
304-
#endif
305-
}
306-
307-
Keyboard::SpecialKey Keyboard::VirtualKeyToSpecialKey(KeyCode virtualKey) {
308-
#ifdef __APPLE__
309-
switch (virtualKey) {
310-
case 123:
311-
return Keyboard::LEFT;
312-
case 124:
313-
return Keyboard::RIGHT;
314-
case 125:
315-
return Keyboard::DOWN;
316-
case 126:
317-
return Keyboard::UP;
318-
case 36:
319-
return Keyboard::ENTER;
320-
case 48:
321-
return Keyboard::TAB;
322-
case 51:
323-
return Keyboard::BACKSPACE;
324-
case 53:
325-
return Keyboard::ESCAPE;
326-
case 55:
327-
return Keyboard::META;
328-
case 56:
329-
return Keyboard::SHIFT;
330-
case 57:
331-
return Keyboard::CAPSLOCK;
332-
case 58:
333-
return Keyboard::ALT;
334-
case 59:
335-
return Keyboard::CONTROL;
336-
}
337-
#endif
338-
339-
#ifdef _WIN32
340-
// TODO: implement
341-
#endif
342-
}
343-
344387
#endif
345388
} // namespace Robot

‎src/Keyboard.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#include <cctype>
44
#include <string>
5+
#include <map>
56

67
#ifdef _WIN32
78
#include <Windows.h>
89
#endif
910

1011
#ifdef __APPLE__
1112
#import <Carbon/Carbon.h>
12-
#include <map>
1313
#endif
1414

1515
#include <atomic>

0 commit comments

Comments
 (0)