forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFWindow-windows.cpp
More file actions
186 lines (170 loc) · 4.78 KB
/
Copy pathDFWindow-windows.cpp
File metadata and controls
186 lines (170 loc) · 4.78 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "DFCommonInternal.h"
#include "DFWindow.h"
#include "DFProcess.h"
using namespace DFHack;
// should always reflect the enum in DFkeys.h
const static int ksTable[NUM_SPECIALS]=
{
VK_RETURN,
VK_SPACE,
VK_BACK,
VK_TAB,
VK_CAPITAL,
VK_LSHIFT,
VK_RSHIFT,
VK_LCONTROL,
VK_RCONTROL,
VK_MENU,
0, //XK_VoidSymbol, // WAIT
VK_ESCAPE,
VK_UP,
VK_DOWN,
VK_LEFT,
VK_RIGHT,
VK_F1,
VK_F2,
VK_F3,
VK_F4,
VK_F5,
VK_F6,
VK_F7,
VK_F8,
VK_F9,
VK_F10,
VK_F11,
VK_F12,
VK_PRIOR,
VK_NEXT,
VK_INSERT,
VK_DELETE,
VK_HOME,
VK_END,
VK_DIVIDE,
VK_MULTIPLY,
VK_SUBTRACT,
VK_ADD,
VK_RETURN,
VK_NUMPAD0,
VK_NUMPAD1,
VK_NUMPAD2,
VK_NUMPAD3,
VK_NUMPAD4,
VK_NUMPAD5,
VK_NUMPAD6,
VK_NUMPAD7,
VK_NUMPAD8,
VK_NUMPAD9,
VK_SEPARATOR
};
//Windows key handlers
struct window
{
HWND windowHandle;
uint32_t pid;
};
BOOL CALLBACK EnumWindowsProc (HWND hwnd, LPARAM lParam)
{
uint32_t pid;
GetWindowThreadProcessId (hwnd, (LPDWORD) &pid);
if (pid == ( (window *) lParam)->pid)
{
( (window *) lParam)->windowHandle = hwnd;
return FALSE;
}
return TRUE;
}
class DFWindow::Private
{
public:
Private(Process * _p)
{
p=_p;
};
~Private(){};
// our parent process
Process * p;
};
// ctor
DFWindow::DFWindow (Process * p)
{
d = new Private(p);
}
// dtor
DFWindow::~DFWindow ()
{
delete d;
}
// TODO: also investigate possible problems with UIPI on Vista and 7
void DFWindow::TypeStr (const char *input, int delay, bool useShift)
{
//sendmessage needs a window handle HWND, so have to get that from the process HANDLE
HWND currentWindow = GetForegroundWindow();
window myWindow;
myWindow.pid = d->p->getPID();
EnumWindows (EnumWindowsProc, (LPARAM) &myWindow);
char cChar;
DWORD dfProccess = GetWindowThreadProcessId(myWindow.windowHandle,NULL);
DWORD currentProccess = GetWindowThreadProcessId(currentWindow,NULL);
AttachThreadInput(currentProccess,dfProccess,TRUE); //The two threads have to have attached input in order to change the keyboard state, which is needed to set the shift state
while ( (cChar = *input++)) // loops through chars
{
short vk = VkKeyScan (cChar); // keycode of char
if (useShift || (vk >> 8) &1) // char is capital, so need to hold down shift
{
BYTE keybstate[256] = {0};
BYTE keybstateOrig[256] = {0};
GetKeyboardState((LPBYTE)&keybstateOrig);
GetKeyboardState((LPBYTE)&keybstate);
keybstate[VK_SHIFT] |= 0x80; //Set shift state to on in variable
SetKeyboardState((LPBYTE)&keybstate); //set the current state to the variable
SendMessage(myWindow.windowHandle,WM_KEYDOWN,vk,0);
SendMessage(myWindow.windowHandle,WM_KEYUP,vk,0);
SetKeyboardState((LPBYTE)&keybstateOrig); // reset the shift state to the original state
}
else
{
SendMessage(myWindow.windowHandle,WM_KEYDOWN,vk,0);
SendMessage(myWindow.windowHandle,WM_KEYUP,vk,0);
}
}
AttachThreadInput(currentProccess,dfProccess,FALSE); //detach the threads
Sleep (delay);
}
void DFWindow::TypeSpecial (t_special command, int count, int delay)
{
if (command != WAIT)
{
HWND currentWindow = GetForegroundWindow();
window myWindow;
myWindow.pid = d->p->getPID();
EnumWindows (EnumWindowsProc, (LPARAM) &myWindow);
for (int i = 0; i < count;i++)
{
SendMessage(myWindow.windowHandle,WM_KEYDOWN,ksTable[command],0);
SendMessage(myWindow.windowHandle,WM_KEYUP,ksTable[command],0);
}
Sleep (delay);
}
else
{
Sleep (delay*count);
}
}