How do I wait for a single character input without terminating "Enter"? Following Brian Ripley on http://markmail.org/message/ptmbkhdfnpnf5zcd "In general R does not have a character-by-character interface with a keyboard." Dieter
-- View this message in context: http://n4.nabble.com/Single-character-input-without-Enter-tp1564153p1567054.html Sent from the R help mailing list archive at Nabble.com.
Dieter Menne wrote:> > How do I wait for a single character input without terminating "Enter"? >In case someone needs a solution for Windows, here it is. Compiled Dll from http://www.menne-biomed.de/download/keystate.zip Dieter #dyn.unload("keystate.dll") dyn.load("keystate.dll") AsyncKeyState = function(vkey){ state = 0 .C("AsyncKeyState",as.integer(state),as.integer(vkey))[[1]] } ShiftKeyState = function(){ state = 0 .C("ShiftKeyState",as.integer(state))[[1]] } ControlKeyState = function(){ state = 0 .C("ControlKeyState",as.integer(state))[[1]] } PressedKey = function(){ state = 0 .C("PressedKey",as.integer(state))[[1]] } # while(TRUE){ # Sys.sleep(1) # cat(PressedKey(),"\n") #} // C Program #include <windows.h> #include <R.h> //#include <Rdefines.h> //#include <R_ext/Error.h> #define DLLIMPORT __declspec (dllexport) #define VK_A 0x41 #define VK_Z 0x5A DLLIMPORT void AsyncKeyState(int *state,int* vKey) { state[0] = GetAsyncKeyState(*vKey) <0; } DLLIMPORT void ControlKeyState(int *state) { state[0] = GetAsyncKeyState(VK_CONTROL) <0; } DLLIMPORT void ShiftKeyState(int *state) { state[0] = GetAsyncKeyState(VK_SHIFT) < 0 ; } DLLIMPORT void PressedKey(int *state) { byte keyState[256]; GetKeyboardState(keyState); for (*state=VK_A; (*state) <= VK_Z; (*state)++) if (keyState[*state] & 0x80) return ; *state = 0; } -- View this message in context: http://n4.nabble.com/Single-character-input-without-Enter-tp1564153p1567059.html Sent from the R help mailing list archive at Nabble.com.
Thanks. If you could wrap it up in a package, provide an Rd help page with an example, and make it available on for instance CRAN, that would be even better. Then one day maybe someone else contribute with a unix version etc. Make sure to credit your sources and get the licenses right. /Henrik On Wed, Feb 24, 2010 at 8:56 AM, Dieter Menne <dieter.menne at menne-biomed.de> wrote:> > > Dieter Menne wrote: >> >> How do I wait for a single character input without terminating "Enter"? >> > > > In case someone needs a solution for Windows, here it is. Compiled Dll from > > http://www.menne-biomed.de/download/keystate.zip > > Dieter > > #dyn.unload("keystate.dll") > dyn.load("keystate.dll") > > AsyncKeyState = function(vkey){ > ?state = 0 > ?.C("AsyncKeyState",as.integer(state),as.integer(vkey))[[1]] > } > > ShiftKeyState = function(){ > ?state = 0 > ?.C("ShiftKeyState",as.integer(state))[[1]] > } > > ControlKeyState = function(){ > ?state = 0 > ?.C("ControlKeyState",as.integer(state))[[1]] > } > > PressedKey = function(){ > ?state = 0 > ?.C("PressedKey",as.integer(state))[[1]] > } > > > # while(TRUE){ > # ? Sys.sleep(1) > # ? cat(PressedKey(),"\n") > #} > > // C Program > #include <windows.h> > #include <R.h> > //#include <Rdefines.h> > //#include <R_ext/Error.h> > > #define DLLIMPORT __declspec (dllexport) > #define VK_A 0x41 > #define VK_Z 0x5A > > DLLIMPORT void AsyncKeyState(int *state,int* vKey) { > ? state[0] = GetAsyncKeyState(*vKey) <0; > } > > DLLIMPORT void ControlKeyState(int *state) { > ? state[0] = GetAsyncKeyState(VK_CONTROL) <0; > } > > DLLIMPORT void ShiftKeyState(int *state) { > ?state[0] = GetAsyncKeyState(VK_SHIFT) < 0 ; > } > > DLLIMPORT void PressedKey(int *state) { > ?byte keyState[256]; > ?GetKeyboardState(keyState); > ?for (*state=VK_A; (*state) <= VK_Z; (*state)++) > ? ?if (keyState[*state] & 0x80) > ? ? ? ?return ; > ?*state = 0; > } > > > -- > View this message in context: http://n4.nabble.com/Single-character-input-without-Enter-tp1564153p1567059.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
The playSudoku function in the sudoku package has 2 examples/methods of responding to single key strokes, whether those methods will work for your application or not depends on what you are trying to do. Did I just use the playSudoku function as a serious answer to a legitimate question? Dang, I may need to find a new example of the most useless function that actually made it onto CRAN. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dieter Menne > Sent: Monday, February 22, 2010 2:06 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Single character input without Enter > > How do I wait for a single character input without terminating "Enter"? > > Following Brian Ripley on > > http://markmail.org/message/ptmbkhdfnpnf5zcd > > "In general R does not have a character-by-character interface with a > keyboard." > > Dieter > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Greg Snow-2 wrote:> > The playSudoku function in the sudoku package has 2 examples/methods of > responding to single key strokes, whether those methods will work for your > application or not depends on what you are trying to do. > >Thanks, Greg, it boils down to the rather basic getGraphicsEvent(prompt = "Waiting for input", onMouseDown = NULL, onMouseMove = NULL, onMouseUp = NULL, onKeybd = NULL) in grDevices; so no tclk needed. Strange that nobody told me.... Dieter -- View this message in context: http://n4.nabble.com/Single-character-input-without-Enter-tp1564153p1569460.html Sent from the R help mailing list archive at Nabble.com.
The getGraphicsEvent function only works on windows (at least last time I checked), so if you are working only on windows, you can use that. If you want something that works cross platform, then the tcltk solution is a possibility. Your original question did not indicate if you would be willing to have this working through a graph or not, so I was not sure if this was a good solution or not. I would have told you this on Monday, but I was out of town the first part of the week and not reading r-help. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dieter Menne > Sent: Thursday, February 25, 2010 11:29 AM > To: r-help at r-project.org > Subject: Re: [R] Single character input without Enter > > > > Greg Snow-2 wrote: > > > > The playSudoku function in the sudoku package has 2 examples/methods > of > > responding to single key strokes, whether those methods will work for > your > > application or not depends on what you are trying to do. > > > > > > Thanks, Greg, it boils down to the rather basic > > getGraphicsEvent(prompt = "Waiting for input", > onMouseDown = NULL, onMouseMove = NULL, > onMouseUp = NULL, onKeybd = NULL) > > in grDevices; so no tclk needed. Strange that nobody told me.... > > Dieter > > > > > -- > View this message in context: http://n4.nabble.com/Single-character- > input-without-Enter-tp1564153p1569460.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.