[R] detecting a key press

Ben Bolker bbolker at gmail.com
Sat Aug 14 04:46:47 CEST 2010


Martin Teicher <martin_teicher <at> hms.harvard.edu> writes:

> I'm relatively new to r.  I'd like to have a user respond by 
> pressing a 1 or a 2 and determining their choice and
> the time of response.  Previous postings have indicated that
> keyboard responses can be processed using
> scan and readline but both seem to wait for the user to also 
> press return.  Is there a way to detect the initial
> key press without requiring them to hit return?  

  Probably not -- probably depends a lot on platform and
interface too (i.e. in a command-line interface, interaction
with the keyboard is likely to be handled by the OS rather
than by R itself).

  If you're on Windows you could look at ?getGraphicsEvent
(which will see keyboard responses, not from the console, but
from the graphics window).

  Alternatively, you might be able to put something together
with the tcltk package ...



More information about the R-help mailing list