1
0
mirror of https://github.com/gdsports/keymapper synced 2024-11-30 21:22:16 -05:00

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

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,26 +100,50 @@ void KbdRptParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
if (!HandleReservedKeystrokes(hid, buf)) if (!HandleReservedKeystrokes(hid, buf))
{ {
specialKeyLatchReleased = true;
// remap all keys according to the existing keymap // remap all keys according to the existing keymap
for (i=2; i<8; i++) for (i=2; i<8; i++)
{ {
// handle special case of Shift-CAPSLOCK to be ignored by the remapper // handle special case of Shift-CAPSLOCK to be ignored by the remapper
if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22) if (buf[i] == KEY_CAPS_LOCK && buf[0] & 0x22)
KeyBuffer[i] = buf[i]; {
KeyBuffer[i] = KEY_CAPS_LOCK;
LatchKey(KEY_CAPS_LOCK);
}
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)
{
// if it was a special key of shift-CAPS, then only allow mapping if the key has been released at least once
if (buf[i] != specialKeyLatch)
KeyBuffer[i] = pgm_read_byte(Keymap[CurrentLayout]+buf[i]-4); KeyBuffer[i] = pgm_read_byte(Keymap[CurrentLayout]+buf[i]-4);
else // key is not released yet. do not allow mapping
{
// Serial.println("key is not released");
KeyBuffer[i] = 0;
specialKeyLatchReleased = false;
}
}
else else
KeyBuffer[i] = buf[i]; KeyBuffer[i] = buf[i];
} }
// check locking keys // check locking keys
HandleLockingKeys(hid, KeyBuffer[i]); HandleLockingKeys(hid, KeyBuffer[i]);
} }
// reset latch if key is released
if (specialKeyLatchReleased)
{
// Serial.println("latch is released");
specialKeyLatch = 0;
}
// send out key press // send out key press
SendKeysToHost (KeyBuffer); SendKeysToHost (KeyBuffer);
@ -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;
@ -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");
}