forked from arduino-libraries/Keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoKeyboard.cpp
More file actions
133 lines (109 loc) · 3.94 KB
/
Copy pathArduinoKeyboard.cpp
File metadata and controls
133 lines (109 loc) · 3.94 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
/*
Keyboard.cpp
Copyright (c) 2015, Arduino LLC
Original code (pre-library): Copyright (c) 2011, Peter Barrett
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ArduinoKeyboard.hpp"
#ifdef _USING_HID
#define MODIFIER_MASK(mod_kc) (1 << (mod_kc & 0x0F))
static const uint8_t _hidReportDescriptor[] PROGMEM = {
// Keyboard
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x02, // REPORT_ID (2)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x73, // LOGICAL_MAXIMUM (115)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x73, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0, // END_COLLECTION
};
ArduinoKeyboard::ArduinoKeyboard() {
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
HID().AppendDescriptor(&node);
releaseAll();
}
void ArduinoKeyboard::press(uint8_t keycode) {
// If keycode >= E0 then it's a modifier key.
if (keycode >= 0xE0) {
// Create bitmask from the modifier keycode to set the corresponding bit in the modifier
// byte.
uint8_t bitmask = MODIFIER_MASK(keycode);
_report.modifiers |= bitmask;
return;
}
// Check if the key is already pressed so that we don't send the same keycode multiple times in
// the same report.
for (int i = 0; i < 6; i++) {
if (_report.keys[i] == keycode) {
return;
}
}
// Place this keycode in the report in place of the first empty keycode.
for (int i = 0; i < 6; i++) {
if (_report.keys[i] == HID_KEY_NONE) {
_report.keys[i] = keycode;
return;
}
}
}
void ArduinoKeyboard::release(uint8_t keycode) {
// If keycode >= E0 then it's a modifier key.
if (keycode >= 0xE0) {
// Create bitmask from the modifier keycode to unset the corresponding bit in the modifier
// byte.
uint8_t bitmask = ~MODIFIER_MASK(keycode);
_report.modifiers &= bitmask;
return;
}
// Loop through keycodes in report. If we find the specified key code, we clear it.
for (int i = 0; i < 6; i++) {
if (_report.keys[i] == keycode) {
_report.keys[i] = HID_KEY_NONE;
}
}
}
void ArduinoKeyboard::setPressed(uint8_t keycode, bool pressed) {
if (pressed) {
press(keycode);
} else {
release(keycode);
}
}
void ArduinoKeyboard::releaseAll() {
for (int i = 0; i < 6; i++) {
_report.keys[i] = 0;
}
_report.modifiers = 0;
}
void ArduinoKeyboard::sendReport() {
HID().SendReport(2, &_report, sizeof(KeyReport));
}
ArduinoKeyboard _keyboard;
#endif