-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_injection.cpp
More file actions
131 lines (107 loc) · 3.18 KB
/
Copy pathtext_injection.cpp
File metadata and controls
131 lines (107 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "text_injection.hpp"
#include <windows.h>
#include <iostream>
#include "emacs.hpp"
bool SetClipboardText(const std::string& text)
{
// Convert the UTF-8 string to a wide string
int size_needed = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), (int)text.size(), NULL, 0);
std::wstring wide_text(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), (int)text.size(), &wide_text[0], size_needed);
// Open the clipboard
if (!OpenClipboard(NULL))
{
return false; // If we can't open the clipboard, exit the function
}
// Empty the clipboard
EmptyClipboard();
// Allocate global memory for the text
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, wide_text.size() * 2 + 2);
if (!hMem)
{
CloseClipboard();
return false;
}
// Copy the text to the global memory
memcpy(GlobalLock(hMem), wide_text.c_str(), wide_text.size() * 2 + 2);
GlobalUnlock(hMem);
// Set the clipboard data
SetClipboardData(CF_UNICODETEXT, hMem);
// Close the clipboard
CloseClipboard();
return true;
}
void InjectTextViaClipboard(const std::string& text, HWND hWnd)
{
if (!SetClipboardText(text))
{
return;
}
// Bring the target window to the foreground
SetForegroundWindow(hWnd);
// Simulate Ctrl+V key press
INPUT inputs[4] = {};
ZeroMemory(inputs, sizeof(inputs));
// Set up a generic keyboard event structure
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VK_CONTROL;
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki.wVk = 'V';
inputs[2].type = INPUT_KEYBOARD;
inputs[2].ki.wVk = 'V';
inputs[2].ki.dwFlags = KEYEVENTF_KEYUP;
inputs[3].type = INPUT_KEYBOARD;
inputs[3].ki.wVk = VK_CONTROL;
inputs[3].ki.dwFlags = KEYEVENTF_KEYUP;
// Send the input to the system
SendInput(4, inputs, sizeof(INPUT));
}
void InjectTextViaConsoleRightClick(const std::string& text, HWND hWnd)
{
if (!SetClipboardText(text))
{
return;
}
SetForegroundWindow(hWnd);
RECT rc;
if (!GetClientRect(hWnd, &rc))
{
return;
}
const int x = (rc.left + rc.right) / 2;
const int y = (rc.top + rc.bottom) / 2;
const LPARAM click_pos = MAKELPARAM(x, y);
PostMessage(hWnd, WM_RBUTTONDOWN, MK_RBUTTON, click_pos);
PostMessage(hWnd, WM_RBUTTONUP, 0, click_pos);
}
void InjectTextToTarget(const std::string& text)
{
HWND hwndForeground = GetForegroundWindow();
if (hwndForeground == NULL)
{
std::cout << "couldn't get foreground window" << std::endl;
return;
}
wchar_t className[256];
if (GetClassName(hwndForeground, className, sizeof(className) / sizeof(wchar_t)) > 0)
{
std::wcout << "got class name: " << className << std::endl;
}
else
{
std::wcout << "oops couldn't get class name" << std::endl;
}
std::wstring classNameString{className};
if (classNameString == L"Emacs")
{
InjectTextToEmacs(text);
}
else if (classNameString == L"ConsoleWindowClass")
{
InjectTextViaConsoleRightClick(text, hwndForeground);
}
else
{
InjectTextViaClipboard(text, hwndForeground);
}
}