Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASCII control codes #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions src/hidboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ uint8_t KeyboardReportParser::HandleLockingKeys(HID *hid, uint8_t key)
const uint8_t KeyboardReportParser::numKeys[] = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' };
const uint8_t KeyboardReportParser::symKeysUp[] = { '_', '+', '{', '}', '|', '~', ':', '"', '~', '<', '>', '?' };
const uint8_t KeyboardReportParser::symKeysLo[] = { '-', '=', '[', ']', '\\', ' ', ';', '\'', '`', ',', '.', '/' };
const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x13 };
const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x0D };

/**
* \brief Manage keyboard OEM to ASCII conversion.
Expand All @@ -142,13 +142,34 @@ const uint8_t KeyboardReportParser::padKeys[] = { '/', '*', '-', '+', 0x13 };
*
* \return Keyboard corresponding ASCII value on success, 0 otherwise.
*/

/* Key modifier bit map.
* 0 LEFT CTRL
* 1 LEFT SHIFT
* 2 LEFT ALT
* 3 LEFT GUI
* 4 RIGHT CTRL
* 5 RIGHT SHIFT
* 6 RIGHT ALT
* 7 RIGHT GUI
*
* shift = (mod & 0x22) means shift is non-zero
* if either or both shift keys are active
* ctrl = (mod & 0x11) means ctrl is non-zero
* if either or both control keys are active
*/

uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key)
{
uint8_t shift = (mod & 0x22);
uint8_t ctrl = (mod & 0x11);

// [a-z]
if (key > 0x03 && key < 0x1e)
{
// [^a-^z]
if (ctrl) return (key - 3);

// Upper case letters
if ( (kbdLockingKeys.kbdLeds.bmCapsLock == 0 && (mod & 2)) ||
(kbdLockingKeys.kbdLeds.bmCapsLock == 1 && (mod & 2) == 0) )
Expand All @@ -162,7 +183,17 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key)
else if (key > 0x1d && key < 0x27)
{
if (shift)
return (numKeys[key - 0x1e]);
{
if (ctrl)
{
if (key == 0x23)
return (0x1E); /* RS ^^ */
else
return (0x00);
}
else
return (numKeys[key - 0x1e]);
}
else
return (key - 0x1e + '1');
}
Expand All @@ -173,15 +204,45 @@ uint8_t KeyboardReportParser::OemToAscii(uint8_t mod, uint8_t key)
return (key - 0x59 + '1');
}
else if (key > 0x2c && key < 0x39)
return ((shift) ? symKeysUp[key-0x2d] : symKeysLo[key-0x2d]);
{
if (shift)
{
if (ctrl)
{
if (key == 0x2d)
return (0x1F); /* US ^_ */
else
return (0x00);
}
else
return symKeysUp[key-0x2d];
}
else
{
if (ctrl)
switch (key)
{
case 0x2F: return 0x1B; /* ESC ^[ */
case 0x31: return 0x1C; /* FS ^\ */
case 0x30: return 0x1D; /* GS ^] */
default: return 0x00;
}
else
return symKeysLo[key-0x2d];
}
}
else if (key > 0x53 && key < 0x59)
return padKeys[key - 0x54];
else
{
switch (key)
{
case KEY_SPACE: return (0x20);
case KEY_ENTER: return (0x13);
case KEY_ENTER: return (0x0D);
case KEY_ESCAPE: return (0x1B);
case KEY_DELETE: return (0x08);
case KEY_DELETE_FORWARD: return (0x7F); // ASCII DEL
case KEY_TAB: return (0x09);
case KEY_ZERO: return ((shift) ? ')' : '0');
case KEY_ZERO2: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '0' : 0);
case KEY_PERIOD: return ((kbdLockingKeys.kbdLeds.bmNumLock == 1) ? '.' : 0);
Expand Down
4 changes: 4 additions & 0 deletions src/hidboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ e-mail : [email protected]
#define KEY_ZERO 0x27
#define KEY_ZERO2 0x62
#define KEY_ENTER 0x28
#define KEY_ESCAPE 0x29
#define KEY_DELETE 0x2A // Backspace
#define KEY_DELETE_FORWARD 0x4C // Delete
#define KEY_TAB 0x2B
#define KEY_PERIOD 0x63

/**
Expand Down