Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 11 months ago.
up and down errow keys detection with mbed ?
how can i detect the up an down arrow keys with mbed serially connected with a pc?
1 Answer
11 years, 11 months ago.
Took me a while to figure this out - so I'll save you the trouble!
This code snippet should get you started...
#include "mbed.h" Serial term (USBTX,USBRX); char Key; int main() { while(1) { Key=term.getc(); //Get the keypad pressed if(Key==0x1B) { //If the keypress was an arrow key Key = term.getc(); //Check again! if (Key == 0x5B) { Key = term.getc(); switch(Key) { //Check to see which arrow key... case 0x41: //It was the UP arrow key... term.printf("\n\r UP!"); break; case 0x42: //It was the DOWN arrow key... term.printf("\n\r DOWN!"); break; case 0x43: //It was the RIGHT arrow key... term.printf("\n\r RIGHT!"); break; case 0x44: //It was the LEFT arrow key... term.printf("\n\r LEFT!"); break; } } } } }
You have to check 3 times to find out which arrow key was pressed. The 1st two times tells you that it was an arrow key and the 3rd time says which one. Strange???
Looks like a VT100 or ANSI escape code. Its a property of the terminal software though not the computer.
I'm guessing the serial terminal is in VT100 mode, and most special keys send an escape character (0x1B) followed by a sequence indicating what the key was.
The second character is "[" , I think there can be an optional digit indicating number of times to move, and the last is an uppercase letter. The information on the web is confusing.
posted by 11 Oct 2013