Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting single keystrokes

Tags:

r

Several times I have wanted to detect single keystrokes in R but have failed to find anything else than readline() or similar.

An example would be to do interactive plotting or data browsing and be able to change parameter values with the arrow keys and automatically update the plot. Of course I could use readline() and have the user input "u" then instead of up arrow but I don't find it very elegant.

Could it be done with a system() command reading stdin in some way?

EDIT: I have now been told elsewhere that stdin also wait for an enter-stroke before doing anything and that catching keystrokes immediately is system specific and tricky to accomplish. If anyone knows how to do it on ubuntu 10.10 or any other Linux/unix system I'd be glad to know.

like image 525
Backlin Avatar asked Jul 12 '11 10:07

Backlin


1 Answers

Very OS-dependent solution. First some C code in getkey3.c:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

void mygetch ( int *ch ) 
{
  struct termios oldt, newt;

  tcgetattr ( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
  *ch = getchar();
  tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
  return;
}

Compile for R with R CMD SHLIB getkey3.c

That produces getkey3.so. Start R.

 > dyn.load("getkey3.so")
 > .C("mygetch",as.integer(0))

then press a key, it should return a list with the first element as the integer value of the ASCII code of that key. Store it in an R variable if you want.

Works for me on Ubuntu, you're on your own for any other OSes.

Barry

like image 200
Spacedman Avatar answered Oct 07 '22 02:10

Spacedman