好的,这是我得到的代码,它告诉我按了哪个键。
#define CLOCK 6 //D-
#define DATA 7 //D+
const char keymap[][2] = {
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, //0
{0,0}, {0,0}, {0,0}, {0,0}, {'`','~'}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, //10
{0,0}, {'q','Q'}, {'1','!'}, {0,0}, {0,0}, {0,0}, {'z','Z'}, {'s','S'}, {'a','A'}, {'w','W'}, //20
{'2','@'}, {0,0}, {0,0}, {'c','C'}, {'x','X'}, {'d','D'}, {'e','E'}, {'4','$'}, {'3','#'}, {0,0}, //30
{0,0}, {' ',' '}, {'v','V'}, {'f','F'}, {'t','T'}, {'r','R'}, {'5','%'}, {0,0}, {0,0}, {'n','N'}, //40
{'b','B'}, {'h','H'}, {'g','G'}, {'y','Y'}, {'6','^'}, {0,0}, {0,0}, {0,0}, {'m','M'}, {'j','J'}, //50
{'u','U'}, {'7','&'}, {'8','*'}, {0,0}, {0,0}, {',','<'}, {'k','K'}, {'i','I'}, {'o','O'}, {'0',')'}, //60
{'9','('}, {0,0}, {0,0}, {'.','>'}, {'/','?'}, {'l','L'}, {';',':'}, {'p','P'}, {'-','_'}, {0,0}, //70
{0,0}, {0,0}, {'\'','\"'}, {0,0}, {'[','{'}, {'=','+'}, {0,0}, {0,0}, {0,0}, {0,0}, //80
{0,0}, {']','}'}, {0,0}, {'\\','|'}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, //90
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {'1','!'}, {0,0}, {'4','$'}, {'7','&'}, {0,0}, //100
{0,0}, {0,0}, {'0',')'}, {'.','>'}, {'2','@'}, {'5','%'}, {'6','^'}, {'8','*'}, {0,0}, {0,0}, //110
{0,0}, {'+','+'}, {'3','#'}, {'-','_'},{'*','*'}, {'9','('}, {0,0}, {0,0}, {0,0}, {0,0}, //120
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, //130
};
void setup() {
Serial.begin(9600);
pinMode(CLOCK, INPUT_PULLUP);
pinMode(DATA, INPUT_PULLUP);
}
uint8_t lastscan; // doesn't work
unsigned short detectKey(){
unsigned short scanval = 0;
for(int i = 0; i<11; i++)
{
while(digitalRead(CLOCK));
scanval |= digitalRead(DATA) << i;
while(!digitalRead(CLOCK));
}
scanval >>= 1;
scanval &= 0xFF;
return scanval;
}
void loop() {
bool shiftPressed= true //when the new code I need detects the shift held down;
if(lastscan != 0xF0 && detectKey() != 0xF0)
keymap[detectKey()][shiftPressed];
lastscan = detectKey(); //this doesn't work to save the scancode to a variable
/* switch(detectKey())
{
case 0x5A: //Enter
Serial.println("Enter");
break;
case 0x66: //Backspace
break;
case 0x12: //left shift
while(detectKey() != 0xF0){
shiftPressed = true;
}
shiftPressed = false;
}
*/
}
下面是我使用键盘时的Arduino串行监视器:
16:47:56.832 -> 1C //hit "a"
16:47:56.924 -> F0 //let go
16:47:56.924 -> 1C
16:47:57.948 -> 1B //hit "s"
16:47:58.119 -> F0 //let go
16:47:58.119 -> 1B
16:48:02.119 -> 23 //held down "d"
16:48:02.722 -> 23
16:48:02.758 -> 23
16:48:02.758 -> F0 //let go
16:48:02.758 -> 23
如你所见,我想使用F0来告诉系统shift被按住。shift的扫描码是12,所以shift看起来像:
12
F0
12
所以我想从一个单独的键盘Map数组中选择大写字母,同时按下另一个键,然后我想将大写字母打印为串行。
1条答案
按热度按时间8oomwypt1#
您可以为字符使用多维数组:
然后,要访问该字符,需要为第二个选项
keymap[detectKey()][shiftPressed]
传入一个布尔值