Repository navigation
Expand file tree
/
Copy path08_Keyboard_Minimal.cpp
More file actions
54 lines (53 loc) · 2.2 KB
/
Copy path08_Keyboard_Minimal.cpp
File metadata and controls
54 lines (53 loc) · 2.2 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
#include "InputBridge.h"
#include <iostream>
#include <system_error>
using namespace InputBridge;
int main() try {
std::cout
<< "Minimal example: capture inputs, change H to J, then forward them.\n"
"Run one example at a time as administrator. Close this window or press Ctrl+C to stop.\n"
<< std::flush;
// Check installation and initialize supported devices before capturing.
Status();
const auto init = Initialize();
if (init.reboot_required) {
std::cerr << "Restart Windows, then run this example again.\n";
return ERROR_SUCCESS_REBOOT_REQUIRED;
}
if (!init.ready) {
std::cerr << "Initialization failed: " << init.error_detail
<< ". Windows error: " << init.error
<< ". Restart required: " << init.reboot_required << "\n";
return 1;
}
DWORD consoleMode = 0; // Console only: prevent text selection from blocking console writes.
if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &consoleMode)) {
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
(consoleMode | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
}
for (;;) {
// Receive: wait for input without polling.
auto inputs = Read(INFINITE);
for (auto& input : inputs) {
if (auto* key = input.Get<KeyboardEvent>()) {
if (!(key->state.flags & (KeyE0 | KeyE1)) && key->state.make_code == 0x23) {
key->state.make_code =
0x24; // H -> J; preserve the press/release flag and device.
}
}
}
// Forward: send all unblocked inputs, including other device types.
const auto sent = Write(inputs);
if (!sent) {
throw std::system_error(sent.error, std::system_category(), "Write");
}
}
} catch (const std::system_error& error) {
std::cerr
<< "Driver error: " << error.what() << ". Windows error: " << error.code().value()
<< ". Install this edition with InputBridgeSetup.exe, restart if requested, and run as administrator.\n";
return 1;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}