Additional handling of special keys

Prevent special keys from being handled until they have been released.
This hopefully fixes the weird repeating key problems.
This commit is contained in:
weigee 2013-02-05 02:19:14 +08:00
parent 27bfccb1a0
commit 2a7bd24b63
1 changed files with 73 additions and 29 deletions

View File

@ -26,7 +26,7 @@
bool HandleReservedKeystrokes(HID *hid, uint8_t *buf); bool HandleReservedKeystrokes(HID *hid, uint8_t *buf);
inline void SendKeysToHost (uint8_t *buf); inline void SendKeysToHost (uint8_t *buf);
void play_word_game(void); void play_word_game(void);
inline void LatchKey (uint8_t keyToLatch);
// variable definitions // variable definitions
@ -62,9 +62,12 @@ const uint8_t *Keymap[] =
// global variables // global variables
//uint32_t ledBlinkTime = millis(); //uint32_t ledBlinkTime = millis();
//uint16_t ledBlinkDelay = 500; //uint16_t ledBlinkDelay = 500;
//bool ledStatus = false;
KeyboardLayout CurrentLayout = qwerty; KeyboardLayout CurrentLayout = qwerty;
uint8_t KeyBuffer[8] = {0,0,0,0,0,0,0,0}; uint8_t KeyBuffer[8] = {0,0,0,0,0,0,0,0};
uint8_t specialKeyLatch=0;
bool specialKeyLatchReleased = false;
class KbdRptParser : public KeyboardReportParser class KbdRptParser : public KeyboardReportParser
{ {
@ -97,36 +100,60 @@ void KbdRptParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
if (!HandleReservedKeystrokes(hid, buf)) if (!HandleReservedKeystrokes(hid, buf))
{ {
// remap all keys according to the existing keymap specialKeyLatchReleased = true;
for (i=2; i<8; i++)
// remap all keys according to the existing keymap
for (i=2; i<8; i++)
{
// handle special case of Shift-CAPSLOCK to be ignored by the remapper
if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22)
{ {
// handle special case of Shift-CAPSLOCK to be ignored by the remapper KeyBuffer[i] = KEY_CAPS_LOCK;
if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22) LatchKey(KEY_CAPS_LOCK);
KeyBuffer[i] = buf[i]; }
else else
{ {
// print the key based on the current layout // print the key based on the current layout
if (buf[i]>=4 && buf[i] <= 57) // transpose of 4 becoz our array starts from 0 but A is 4 if (buf[i]>=4 && buf[i] <= 57) // transpose of 4 becoz our array starts from 0 but A is 4
// limit check to 57, which is the last mappable key (CAPSLOCK) // limit check to 57, which is the last mappable key (CAPSLOCK)
KeyBuffer[i] = pgm_read_byte(Keymap[CurrentLayout]+buf[i]-4); {
else // if it was a special key of shift-CAPS, then only allow mapping if the key has been released at least once
KeyBuffer[i] = buf[i]; if (buf[i] != specialKeyLatch)
} KeyBuffer[i] = pgm_read_byte(Keymap[CurrentLayout]+buf[i]-4);
else // key is not released yet. do not allow mapping
// check locking keys {
HandleLockingKeys(hid, KeyBuffer[i]); // Serial.println("key is not released");
KeyBuffer[i] = 0;
specialKeyLatchReleased = false;
}
}
else
KeyBuffer[i] = buf[i];
} }
// send out key press
SendKeysToHost (KeyBuffer);
// for (uint8_t i=0; i<8; i++) // check locking keys
// { HandleLockingKeys(hid, KeyBuffer[i]);
// PrintHex(KeyBuffer[i]); }
// Serial.print(" ");
// } // reset latch if key is released
// Serial.println(""); if (specialKeyLatchReleased)
// Serial.println(""); {
// Serial.println("latch is released");
specialKeyLatch = 0;
}
// send out key press
SendKeysToHost (KeyBuffer);
// for (uint8_t i=0; i<8; i++)
// {
// PrintHex(KeyBuffer[i]);
// Serial.print(" ");
// }
// Serial.println("");
// Serial.println("");
} }
@ -156,37 +183,45 @@ bool HandleReservedKeystrokes(HID *hid, uint8_t *buf) // return true if it is a
case 0x27: // 0 case 0x27: // 0
CurrentLayout = qwerty; CurrentLayout = qwerty;
digitalWrite(modeLED, LOW); digitalWrite(modeLED, LOW);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x1e: // 1 case 0x1e: // 1
CurrentLayout = tarmak1; CurrentLayout = tarmak1;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x1f: // 2 case 0x1f: // 2
CurrentLayout = tarmak2; CurrentLayout = tarmak2;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x20: // 3 case 0x20: // 3
CurrentLayout = tarmak3; CurrentLayout = tarmak3;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x21: // 4 case 0x21: // 4
CurrentLayout = tarmak4; CurrentLayout = tarmak4;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x22: // 5 case 0x22: // 5
CurrentLayout = colemak; CurrentLayout = colemak;
digitalWrite(modeLED, HIGH); digitalWrite(modeLED, HIGH);
LatchKey(buf[keyPosition]);
return true; return true;
case 0x2c: // space bar case 0x2c: // space bar
play_word_game(); play_word_game();
LatchKey(buf[keyPosition]);
return true; return true;
} }
} }
return false; return false;
@ -196,7 +231,7 @@ bool HandleReservedKeystrokes(HID *hid, uint8_t *buf) // return true if it is a
inline void SendKeysToHost (uint8_t *buf) inline void SendKeysToHost (uint8_t *buf)
{ {
#ifdef TEENSY #ifdef TEENSY
Keyboard.set_modifier(buf[0]); Keyboard.set_modifier(buf[0]);
Keyboard.set_key1(buf[2]); Keyboard.set_key1(buf[2]);
Keyboard.set_key2(buf[3]); Keyboard.set_key2(buf[3]);
Keyboard.set_key3(buf[4]); Keyboard.set_key3(buf[4]);
@ -211,6 +246,15 @@ inline void SendKeysToHost (uint8_t *buf)
} }
inline void LatchKey (uint8_t keyToLatch)
{
specialKeyLatch = keyToLatch;
specialKeyLatchReleased = false;
// Serial.print(keyToLatch);
// Serial.println(" is latched");
}