aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/HID.cpp
diff options
context:
space:
mode:
authorZach Eveland <zeveland@blacklabel-development.com>2012-03-03 22:54:45 -0500
committerZach Eveland <zeveland@blacklabel-development.com>2012-03-03 22:54:45 -0500
commitfbea67532a03cbcb35c47209f902797c80c499f7 (patch)
tree00c32f51c060cc0105cedd8ee504802a45f6eb62 /cores/arduino/HID.cpp
parent74863e4bdfdfc162bf8c349afc5529768c74300f (diff)
added Keyboard methods press(), release(), and releaseAll()
Changes mean that a single, persistent key report must be used so keys can be added or removed. Also reimplemented type() using the new methods.
Diffstat (limited to 'cores/arduino/HID.cpp')
-rw-r--r--cores/arduino/HID.cpp98
1 files changed, 70 insertions, 28 deletions
diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp
index b60b3c0..9904993 100644
--- a/cores/arduino/HID.cpp
+++ b/cores/arduino/HID.cpp
@@ -253,7 +253,7 @@ bool Mouse_::isPressed(uint8_t b)
//================================================================================
// Keyboard
-Keyboard_::Keyboard_() : _keyMap(0)
+Keyboard_::Keyboard_() : _keyMap(0)
{
}
@@ -406,38 +406,80 @@ const uint8_t _asciimap[128] =
uint8_t USBPutChar(uint8_t c);
-size_t Keyboard_::type(uint8_t c)
+size_t Keyboard_::press(uint8_t k)
{
- // Keydown
- {
- KeyReport keys = {0};
- if (_keyMap)
- _keyMap->charToKey(c,&keys);
- else
- {
- if (c >= 128) {
- setWriteError();
- return 0;
- }
- c = pgm_read_byte(_asciimap + c);
- if (!c) {
- setWriteError();
- return 0;
- }
- if (c & 0x80)
- {
- keys.modifiers |= KEY_MODIFIER_LEFT_SHIFT;
- c &= 0x7F;
+ uint8_t i;
+ k = pgm_read_byte(_asciimap + k);
+ if (!k) {
+ setWriteError();
+ return 0;
+ }
+ if (k & 0x80) {
+ _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT;
+ k &= 0x7F;
+ }
+ if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
+ _keyReport.keys[2] != k && _keyReport.keys[3] != k &&
+ _keyReport.keys[4] != k && _keyReport.keys[5] != k) {
+
+ for (i=0; i<6; i++) {
+ if (_keyReport.keys[i] == 0x00) {
+ _keyReport.keys[i] = k;
+ break;
}
- keys.keys[0] = c;
}
- sendReport(&keys);
+ if (i == 6) {
+ setWriteError();
+ return 0;
+ }
}
- // Keyup
- {
- KeyReport keys = {0};
- sendReport(&keys);
+ sendReport(&_keyReport);
+ return 1;
+}
+
+size_t Keyboard_::release(uint8_t k)
+{
+ uint8_t i;
+ k = pgm_read_byte(_asciimap + k);
+ if (!k) {
+ return 0;
}
+ if (k & 0x80) {
+ _keyReport.modifiers |= KEY_MODIFIER_LEFT_SHIFT;
+ k &= 0x7F;
+ }
+ for (i=0; i<6; i++) {
+ if (_keyReport.keys[i] == k) {
+ _keyReport.keys[i] = 0x00;
+ break;
+ }
+ }
+ if (i == 6) {
+ return 0;
+ }
+ sendReport(&_keyReport);
+ return 1;
+}
+
+void Keyboard_::releaseAll(void)
+{
+ _keyReport.keys[0] = 0;
+ _keyReport.keys[1] = 0;
+ _keyReport.keys[2] = 0;
+ _keyReport.keys[3] = 0;
+ _keyReport.keys[4] = 0;
+ _keyReport.keys[5] = 0;
+ _keyReport.modifiers = 0;
+ sendReport(&_keyReport);
+}
+
+size_t Keyboard_::type(uint8_t c)
+{
+ releaseAll();
+ // Keydown
+ press(c);
+ // Keyup
+ releaseAll();
return 1;
}