[R-gui] Ask for the State of a Keyboard Key

Greg Snow Greg.Snow at imail.org
Thu Jun 24 20:56:00 CEST 2010


Using Tk you can use the tkbind function to bind a keypress and/or keyrelease to run a function.  The usual mode is to have that function do whatever you plan, but you could have the keypress function update a list of keys currently pressed and the keyrelease function clear that key from the list, then your isKeyPressed function could just check to see if that key is in the list of currently pressed keys.

The playSudoku function in the sudoku package is one example of binding keys, you can look at the code for an example.

Note:  I think that the argument to the function you create/call is important in the Tk bindings.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-sig-gui-bounces at stat.math.ethz.ch [mailto:r-sig-gui-
> bounces at stat.math.ethz.ch] On Behalf Of Adrian Waddell
> Sent: Thursday, June 24, 2010 12:18 PM
> To: r-sig-gui at stat.math.ethz.ch
> Subject: [R-gui] Ask for the State of a Keyboard Key
> 
> Dear GUI list,
> 
> Is it possible in R to check whether a certain key is pressed or not.
> 
> For example
> 
> if(isKeyPressed("a")) {
>    cat("a is pressed\n")
> }else {
>    cat("a is not pressed\n")
> }
> 
> I would like to use this function within tk events. For example when a
> '<Button-1>' event calls foo(), I would like to know within foo()
> whether the Shift key is pressed or not.
> 
> I know that I could generate events when a key is pressed or released
> (with focus on a tk toplevel window). However I would prefer a function
> like isKeyPressed().
> 
> Sincerely,
> 
> Adrian Waddell
> 
> _______________________________________________
> R-SIG-GUI mailing list
> R-SIG-GUI at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-gui



More information about the R-SIG-GUI mailing list