Retrieve simultaneous keypresses from terminal

21 Aug 2011

Hello, i am trying to capture more simultaneously pressed keys on pc that are retrieved from terminal via pc.getc() or pc.scanf(). But without any acceptable results. My target is to get A=keypressed1 and B=0 and C=0 each cycle if I hold down one key. Or A=keypressed1 and B=keypressed2 and C=0 each cycle if I hold down 2 keys.... Can anybody point me to right idea?

Here is code I am trying to use. Please notice I am C++ newbie and I'm not sure if my code is right.

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

unsigned char keybuffer [3];

void naplnbuffer (){


if (pc.readable()) {

keybuffer [0] = pc.getc();;
}
else {
keybuffer [0] = 0;
}


if (pc.readable()) {
pc.getc();
keybuffer [1] = pc.getc();
}
else {
keybuffer [1] = 0;
}
if (pc.readable()) {
pc.getc();
keybuffer [2] = pc.getc();
}
else {
keybuffer [2] = 0;
}
}
void vypisbuffer (unsigned char prvni, unsigned char druhy, unsigned char treti){
pc.printf("A%2x ",prvni);
pc.printf("B%2x ",druhy);
pc.printf("C%2x ",treti);
}
int main() {

pc.baud(921600);
while(1){
naplnbuffer();
vypisbuffer(keybuffer[0], keybuffer[1], keybuffer[2]);
wait (0.01);

}}

And second using pc.scanf().

#include "mbed.h"

DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

unsigned char keybuffer [3];

void naplnbuffer (){
if (pc.readable()) {

pc.scanf("%1s", &keybuffer[0]);
}
else {keybuffer[0] = 0;}

if (pc.readable()) {

pc.scanf("%1s", &keybuffer[1]);
}
else {keybuffer[1] = 0;}
if (pc.readable()) {

pc.scanf("%1s", &keybuffer[2]);
}
else {keybuffer[2] = 0;}


}
void vypisbuffer (unsigned char prvni, unsigned char druhy, unsigned char treti){
pc.printf("A%2x ",prvni);
pc.printf("B%2x ",druhy);
pc.printf("C%2x ",treti);
}
int main() {

pc.baud(921600);
while(1){
naplnbuffer();
vypisbuffer(keybuffer[0], keybuffer[1], keybuffer[2]);
wait (0.01);

}}
21 Aug 2011

Assuming you are using a standard terminal emulation program at the PC end, then it can't be done. Think of the serial connection in terms of sending characters, rather than keypresses.

If you need to detect multiple keyhits, you would need to write some custom software at the PC end (if you still need to use the serial connection), or maybe interface directly to a keyboard to enable you to read the key scan codes.