Skip to content

Commit e8ba711

Browse files
committed
Add send type DD
AhkDll: Add VirtualKeyStates; Ahk: Fix mode ahk; Test: Add TestMouseAbsoluteMove
1 parent 993154e commit e8ba711

22 files changed

+1094
-246
lines changed

AhkDll.Ahk/v1/IbAhkSend.ahk

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,38 @@
44
; Version: 210815
55
; Git: https://github.com/Chaoses-Ib/IbAhkSendLib
66

7-
IbSendInit(send_type := "AnyDriver", mode := 1){
8-
static hModule := DllCall("LoadLibrary", "Str", A_ScriptDir "\IbAhkSend.dll", "Ptr")
7+
IbSendInit(send_type := "AnyDriver", mode := 1, args*){
8+
workding_dir := A_WorkingDir
9+
SetWorkingDir, %A_ScriptDir%
10+
11+
static hModule := DllCall("LoadLibrary", "Str", "IbAhkSend.dll", "Ptr")
912
if (hModule == 0){
1013
if (A_PtrSize == 4)
11-
throw "LibLoadingFailed: Please use AutoHotkey x64"
12-
else if (!FileExist(A_ScriptDir "\IbAhkSend.dll"))
13-
throw "LibLoadingFailed: Please put IbAhkSend.dll with your script file (or use AHK v2 instead, which can locate those DLLs that are put with the library files)"
14+
throw "SendLibLoadFailed: Please use AutoHotkey x64"
15+
else if (!FileExist("IbAhkSend.dll"))
16+
throw "SendLibLoadFailed: Please put IbAhkSend.dll with your script file (or use AHK v2 instead, which can locate those DLLs that are put with the library files)"
1417
else
15-
throw "LibLoadingFailed"
18+
throw "SendLibLoadFailed"
1619
}
1720

18-
send_type_table := ["AnyDriver", "SendInput", "Logitech"]
19-
send_type_v := 0
20-
for i, e in send_type_table
21-
{
22-
if (e == send_type){
23-
send_type_v := i
24-
break
25-
}
26-
}
27-
if (send_type_v == 0)
21+
if (send_type == "AnyDriver")
22+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 0, "Int", 0, "Ptr", 0, "Int")
23+
else if (send_type == "SendInput")
24+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 1, "Int", 0, "Ptr", 0, "Int")
25+
else if (send_type == "Logitech")
26+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 2, "Int", 0, "Ptr", 0, "Int")
27+
else if (send_type == "DD"){
28+
if (args.MaxIndex() == 1)
29+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 3, "Int", 0, "WStr", args[1], "Int")
30+
else
31+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 3, "Int", 0, "Ptr", 0, "Int")
32+
} else
2833
throw "Invalid send type"
29-
send_type_v := send_type_v - 1
30-
31-
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", send_type_v, "Int", 0, "Ptr", 0, "Int")
34+
35+
SetWorkingDir, %workding_dir%
36+
3237
if (result != 0){
33-
error_text := ["DeviceNotFound", "DeviceOpeningFailed"]
38+
error_text := ["InvalidArgument", "LibraryNotFound", "LibraryLoadFailed", "LibraryError", "DeviceCreateFailed", "DeviceNotFound", "DeviceOpenFailed"]
3439
throw error_text[result]
3540
}
3641

@@ -49,7 +54,7 @@ IbSendMode(mode){
4954
SendMode %ahk_mode%
5055
DllCall("IbAhkSend\IbAhkSendInputHook", "Int", 0)
5156
} else {
52-
throw "Invalid argument"
57+
throw "Invalid send mode"
5358
}
5459
}
5560

AhkDll.Ahk/v1/test/mode ahk.ahk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Send notepad`n
77
WinWaitActive, ahk_exe notepad.exe
88
Send Hello world+1
99
Sleep 100
10-
MouseClickDrag, Left, 0, 0, 150, 50
10+
CoordMode, Mouse, Client
11+
MouseClickDrag, Left, 5, 5, 150, 50

AhkDll.Ahk/v1/test/type DD.ahk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Include ..\IbAhkSend.ahk
2+
IbSendInit("DD") ;or IbSendInit("DD", 1, "C:\SomeDir\DD64.dll")
3+
#include mode ahk.ahk

AhkDll.Ahk/v2/IbAhkSend.ahk

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,43 @@
66

77
#DllLoad "*i IbAhkSend.dll" ;DllCall("LoadLibrary") cannot locate DLL correctly
88

9-
IbSendInit(send_type := "AnyDriver", mode := 1){
9+
IbSendInit(send_type := "AnyDriver", mode := 1, args*){
10+
workding_dir := A_WorkingDir
11+
SetWorkingDir(A_ScriptDir)
12+
1013
static hModule := DllCall("GetModuleHandle", "Str", "IbAhkSend.dll", "Ptr")
1114
if (hModule == 0){
1215
if (A_PtrSize == 4)
13-
throw "LibLoadingFailed: Please use AutoHotkey x64"
16+
throw "SendLibLoadFailed: Please use AutoHotkey x64"
1417
else
15-
throw "LibLoadingFailed"
18+
throw "SendLibLoadFailed"
1619
}
1720

18-
send_type_table := ["AnyDriver", "SendInput", "Logitech"]
19-
send_type_v := 0
20-
for i, e in send_type_table
21-
{
22-
if (e == send_type){
23-
send_type_v := i
24-
break
25-
}
26-
}
27-
if (send_type_v == 0)
21+
if (send_type == "AnyDriver")
22+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 0, "Int", 0, "Ptr", 0, "Int")
23+
else if (send_type == "SendInput")
24+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 1, "Int", 0, "Ptr", 0, "Int")
25+
else if (send_type == "Logitech")
26+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 2, "Int", 0, "Ptr", 0, "Int")
27+
else if (send_type == "DD"){
28+
if (args.Length == 1)
29+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 3, "Int", 0, "WStr", args[1], "Int")
30+
else
31+
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", 3, "Int", 0, "Ptr", 0, "Int")
32+
} else
2833
throw "Invalid send type"
29-
send_type_v := send_type_v - 1
3034

31-
result := DllCall("IbAhkSend\IbAhkSendInit", "Int", send_type_v, "Int", 0, "Ptr", 0, "Int")
35+
SetWorkingDir(workding_dir)
36+
3237
if (result !== 0){
3338
error_text := [
39+
"InvalidArgument",
40+
"LibraryNotFound",
41+
"LibraryLoadFailed",
42+
"LibraryError",
43+
"DeviceCreateFailed",
3444
"DeviceNotFound",
35-
"DeviceOpeningFailed"
45+
"DeviceOpenFailed"
3646
]
3747
throw error_text[result]
3848
}
@@ -52,7 +62,7 @@ IbSendMode(mode){
5262
SendMode(ahk_mode)
5363
DllCall("IbAhkSend\IbAhkSendInputHook", "Int", 0)
5464
} else {
55-
throw "Invalid argument"
65+
throw "Invalid send mode"
5666
}
5767
}
5868

AhkDll.Ahk/v2/test/mode ahk.ahk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Send("notepad`n")
77
WinWaitActive("ahk_exe notepad.exe")
88
Send("Hello world+1")
99
Sleep(100)
10-
MouseClickDrag("Left", 0, 0, 150, 50)
10+
MouseClickDrag("Left", 5, 5, 150, 50)

AhkDll.Ahk/v2/test/type DD.ahk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Include "..\IbAhkSend.ahk"
2+
IbSendInit("DD") ;or IbSendInit("DD", 1, "C:\SomeDir\DD64.dll")
3+
#include "mode ahk.ahk"

AhkDll.Test/AhkDll.Test.cpp

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using namespace Send;
1010
#include <future>
1111
#include <queue>
1212
#include <IbWinCppLib/WinCppLib.hpp>
13+
#include "Vk.hpp"
1314

1415

1516
class Measure {
@@ -133,12 +134,15 @@ class KeyboardTest : public InitTest<TypeV>
133134
input_keys.pop();
134135
}
135136

136-
BOOST_TEST_MESSAGE(fmt::format("Duration: {}ns\n", t1));
137-
//SendInput: 1~3ms
137+
BOOST_TEST_MESSAGE("TestKeyboardLatency:");
138+
BOOST_TEST_MESSAGE(fmt::format(" Duration: {}ns", t1));
139+
//SendInput: 1000~3000us (1~3ms)
138140
//Logitech: 25~45us
139-
BOOST_TEST_MESSAGE(fmt::format("Latency: {}ns\n", t2));
141+
//DD: 70~110us
142+
BOOST_TEST_MESSAGE(fmt::format(" Latency: {}ns", t2));
140143
//SendInput: 100ns (0)
141144
//Logitech: 0.8~4ms
145+
//DD: 0.6~1ms
142146
}
143147
};
144148

@@ -164,9 +168,12 @@ class MouseTest : public InitTest<TypeV> {
164168
IbAhkSendInput(1, &input, sizeof INPUT);
165169
}
166170
uint64_t t = measure.end();
167-
BOOST_TEST_MESSAGE(fmt::format("Duration: {}ns\n", t / 10000));
171+
172+
BOOST_TEST_MESSAGE("TestMouseMove:");
173+
BOOST_TEST_MESSAGE(fmt::format(" Duration: {}ns", t / 10000));
168174
//SendInput: 200~800us
169175
//Logitech: 3~10us
176+
//DD: 500~1000us
170177
}
171178

172179
void TestMouseMoveLatency() {
@@ -188,12 +195,37 @@ class MouseTest : public InitTest<TypeV> {
188195
d = { p2.x - p1.x, p2.y - p1.y };
189196
} while (!(d.x >= 50 && d.y >= 50));
190197
uint64_t latency = measure.end();
191-
BOOST_TEST_MESSAGE(fmt::format("Latency: {}ns\n", latency));
192-
//SendInput: 3~200us
193-
//Logitech: 900~4000us (0.9~4ms)
198+
199+
BOOST_TEST_MESSAGE("TestMouseMoveLatency:");
200+
BOOST_TEST_MESSAGE(fmt::format(" Latency: {}ns", latency));
201+
//SendInput: 0.003~0.2ms (3~200us)
202+
//Logitech: 0.9~4ms
203+
//DD: 1.1ms~1.3ms
204+
}
205+
206+
void TestMouseAbsoluteMove() {
207+
POINT screen{ GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) };
208+
209+
INPUT input;
210+
input.type = INPUT_MOUSE;
211+
input.mi = {};
212+
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_MOVE_NOCOALESCE | MOUSEEVENTF_ABSOLUTE;
213+
input.mi.dx = 65536 * 100 / screen.x;
214+
input.mi.dy = 65536 * 100 / screen.y;
215+
IbAhkSendInput(1, &input, sizeof INPUT);
216+
217+
POINT p1, p2;
218+
GetCursorPos(&p1);
219+
Sleep(10);
220+
GetCursorPos(&p2);
221+
BOOST_CHECK((abs(p2.x - 100) <= 10 && abs(p2.y - 100) <= 100));
222+
223+
BOOST_TEST_MESSAGE("TestMouseAbsoluteMove:");
224+
BOOST_TEST_MESSAGE(fmt::format(" 0ms: ({}, {})", p1.x, p1.y));
225+
BOOST_TEST_MESSAGE(fmt::format(" 10ms: ({}, {})", p2.x, p2.y));
194226
}
195227

196-
void TestMouseMoveDistance() {
228+
void TestMouseRelativeMove() {
197229
POINT p1, p2, d1, d2;
198230
GetCursorPos(&p1);
199231

@@ -213,35 +245,69 @@ class MouseTest : public InitTest<TypeV> {
213245
d2 = { p2.x - p1.x, p2.y - p1.y };
214246
BOOST_CHECK((abs(d2.x - 100) <= 10 && abs(d2.y - 100) <= 100));
215247

216-
BOOST_TEST_MESSAGE(fmt::format("0ms: ({}, {})\n", d1.x, d1.y));
217-
BOOST_TEST_MESSAGE(fmt::format("10ms: ({}, {})\n", d2.x, d2.y));
248+
BOOST_TEST_MESSAGE("TestMouseRelativeMove:");
249+
BOOST_TEST_MESSAGE(fmt::format(" 0ms: ({}, {})", d1.x, d1.y));
250+
BOOST_TEST_MESSAGE(fmt::format(" 10ms: ({}, {})", d2.x, d2.y));
218251
}
219252
};
220253

221-
#define CODE_GENERATE_TEST_NAME(name, type) \
222-
BOOST_AUTO_TEST_SUITE(name) \
223-
BOOST_FIXTURE_TEST_SUITE(Keyboard, KeyboardTest<SendType::type>) \
254+
#define CODE_GENERATE_KEYBOARDTEST \
224255
using base = BOOST_AUTO_TEST_CASE_FIXTURE; \
225256
BOOST_AUTO_TEST_CASE(TestKeyboardLatency) { \
226257
base::TestKeyboardLatency(); \
227-
} \
228-
BOOST_AUTO_TEST_SUITE_END() \
229-
\
230-
BOOST_FIXTURE_TEST_SUITE(Mouse, MouseTest<SendType::type>) \
231-
using base = BOOST_AUTO_TEST_CASE_FIXTURE; \
258+
}
259+
260+
#define CODE_GENERATE_MOUSETEST \
261+
using base = BOOST_AUTO_TEST_CASE_FIXTURE; \
232262
BOOST_AUTO_TEST_CASE(TestMouseMove) { \
233263
base::TestMouseMove(); \
234264
} \
235265
BOOST_AUTO_TEST_CASE(TestMouseMoveLatency) { \
236266
base::TestMouseMoveLatency(); \
237267
} \
238-
BOOST_AUTO_TEST_CASE(TestMouseMoveDistance) { \
239-
base::TestMouseMoveDistance(); \
268+
BOOST_AUTO_TEST_CASE(TestMouseAbsoluteMove) { \
269+
base::TestMouseAbsoluteMove(); \
240270
} \
271+
BOOST_AUTO_TEST_CASE(TestMouseRelativeMove) { \
272+
base::TestMouseRelativeMove(); \
273+
}
274+
275+
#define CODE_GENERATE_TEST_NAME(name, type) \
276+
BOOST_AUTO_TEST_SUITE(name) \
277+
BOOST_FIXTURE_TEST_SUITE(Keyboard, KeyboardTest<SendType::type>) \
278+
CODE_GENERATE_KEYBOARDTEST \
279+
BOOST_AUTO_TEST_SUITE_END() \
280+
\
281+
BOOST_FIXTURE_TEST_SUITE(Mouse, MouseTest<SendType::type>) \
282+
CODE_GENERATE_MOUSETEST \
241283
BOOST_AUTO_TEST_SUITE_END() \
242284
BOOST_AUTO_TEST_SUITE_END()
243285
#define CODE_GENERATE_TEST(type) CODE_GENERATE_TEST_NAME(type, type)
244286

245287
CODE_GENERATE_TEST_NAME(SendInput_, SendInput)
246288
CODE_GENERATE_TEST(AnyDriver)
247-
CODE_GENERATE_TEST(Logitech)
289+
CODE_GENERATE_TEST(Logitech)
290+
291+
BOOST_AUTO_TEST_SUITE(DD)
292+
BOOST_FIXTURE_TEST_SUITE(Keyboard, KeyboardTest<SendType::DD>)
293+
BOOST_AUTO_TEST_CASE(TestDDCode) {
294+
HMODULE dd = GetModuleHandleW(L"DD94687.64.dll");
295+
if(!dd) dd = GetModuleHandleW(L"DD64.dll");
296+
if (!dd) dd = GetModuleHandleW(L"DDHID64.dll");
297+
BOOST_CHECK(dd);
298+
int (*DD_todc)(int vk) = ib::Addr(GetProcAddress(dd, "DD_todc"));
299+
BOOST_CHECK(DD_todc);
300+
301+
std::stringstream ss;
302+
for (WORD vk = 0; vk < 0x100; vk++)
303+
ss << vk << " " << vk_to_str(vk) << ": " << DD_todc(vk) << "\n";
304+
BOOST_TEST_MESSAGE(ss.str());
305+
}
306+
307+
CODE_GENERATE_KEYBOARDTEST
308+
BOOST_AUTO_TEST_SUITE_END()
309+
310+
BOOST_FIXTURE_TEST_SUITE(Mouse, MouseTest<SendType::DD>)
311+
CODE_GENERATE_MOUSETEST
312+
BOOST_AUTO_TEST_SUITE_END()
313+
BOOST_AUTO_TEST_SUITE_END()

AhkDll.Test/AhkDll.Test.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@
162162
<Project>{a8cc7e86-6e3f-4c18-bf47-f6aaf0afefa9}</Project>
163163
</ProjectReference>
164164
</ItemGroup>
165+
<ItemGroup>
166+
<ClInclude Include="Vk.hpp" />
167+
</ItemGroup>
165168
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
166169
<ImportGroup Label="ExtensionTargets">
167170
</ImportGroup>

AhkDll.Test/AhkDll.Test.vcxproj.filters

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
2121
</ItemGroup>
22+
<ItemGroup>
23+
<ClInclude Include="Vk.hpp">
24+
<Filter>Header Files</Filter>
25+
</ClInclude>
26+
</ItemGroup>
2227
</Project>

0 commit comments

Comments
 (0)