-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathUSBKeyboard.h
More file actions
130 lines (113 loc) · 3.14 KB
/
USBKeyboard.h
File metadata and controls
130 lines (113 loc) · 3.14 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
#ifndef __USBKeyboard_h__
#define __USBKeyboard_h__
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "hid_keys.h"
#define LED_NUMLOCK (1 << 0)
#define LED_CAPSLOCK (1 << 1)
#define LED_SCROLLLOCK (1 << 2)
class USBKeyboard {
public:
void init () {
// We will talk to atmega8u2 using 9600 bps
Serial.begin(9600);
}
void sendKeyStroke(byte keyStroke) {
sendKeyStroke(keyStroke, 0);
}
void sendKeyStroke(byte keyStroke, byte modifiers) {
uint8_t keyNone[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
Serial.write(modifiers); // Modifier Keys
Serial.write(0, 1); // Reserved
Serial.write(keyStroke); // Keycode 1
Serial.write(0, 1); // Keycode 2
Serial.write(0, 1); // Keycode 3
Serial.write(0, 1); // Keycode 4
Serial.write(0, 1); // Keycode 5
Serial.write(0, 1); // Keycode 6
Serial.write(keyNone, 8); // Release Key
}
uint8_t readLedStatus() {
uint8_t keyNone[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
uint8_t ledStatus;
Serial.write(keyNone, 8); // Release Key
while (Serial.available() > 0) {
ledStatus = Serial.read();
}
return ledStatus;
}
void print(char *chp)
{
uint8_t buf[8] = { 0 }; /* Keyboard report buffer */
while (*chp) {
if ((*chp >= 'a') && (*chp <= 'z')) {
buf[2] = *chp - 'a' + 4;
} else if ((*chp >= 'A') && (*chp <= 'Z')) {
buf[0] = MOD_SHIFT_LEFT; /* Caps */
buf[2] = *chp - 'A' + 4;
} else if ((*chp >= '1') && (*chp <= '9')) {
buf[2] = *chp - '1' + 30;
} else if (*chp == '0') {
buf[2] = KEY_0;
} else {
switch (*chp) {
case ' ':
buf[2] = KEY_SPACE; // Space
break;
case '-':
buf[2] = KEY_MINUS;
break;
case '=':
buf[2] = KEY_EQUALS;
break;
case '[':
buf[2] = KEY_LBRACKET;
break;
case ']':
buf[2] = KEY_RBRACKET;
break;
case '\\':
buf[2] = KEY_BACKSLASH;
break;
case ';':
buf[2] = KEY_SEMICOLON;
break;
case ':':
buf[0] = MOD_SHIFT_LEFT; /* Caps */
buf[2] = KEY_SEMICOLON;
break;
case '"':
buf[2] = KEY_QUOTE;
break;
case '~':
buf[2] = KEY_TILDE;
break;
case ',':
buf[2] = KEY_COMMA;
break;
case '/':
buf[2] = KEY_SLASH;
break;
case '@':
buf[0] = MOD_SHIFT_LEFT; /* Caps */
buf[2] = KEY_2;
break;
default:
/* Character not handled. To do: add rest of chars from HUT1_11.pdf */
buf[2] = KEY_PERIOD;// Period
break;
}
}
Serial.write(buf, 8); // Send keystroke
buf[0] = 0;
buf[2] = 0;
Serial.write(buf, 8); // Release key
chp++;
}
}
};
USBKeyboard Keyboard;
#endif // __USBKeyboard_h__