UP | HOME

Arduino's KEY3 and space character

During the development of my custom keyboard firmware, I jumped into a foolish issue that the keyboard could not send any event for number 3, which has the value of 0x20 or KEY_3.

The code that send keyboard events look like this, I read the key code to determine what is the corresponding key character, empty characters will be represented as a space.

char map[r][c] = { ' ', 'a', 'b', 'c', ' ', ...  };
char c = readEvent(key, map);
if (c != ' ') {
  sendKeyEvent(c);
}

Turned out, 0x20 is also the value of the space character. So it falls into the excluded case.

To fix this problem, we just need to stop using spaces as empty characters, instead, use a NULL character 0x00. So we don't have the conflict anymore.

The code become:

#define NULL_KEY 0x00
char map[r][c] = { NULL_KEY, 'a', 'b', 'c', NULL_KEY, ...  };
char c = readEvent(key, map);
if (c != NULL_KEY) {
  sendKeyEvent(c);
}

Date: 2019-02-06 Wed 00:00

Author: Huy Tran

Created: 2019-04-22 Mon 14:40