From: AW Date: 2002-12-23T09:41:19+09:00 Subject: Re: How to read a single character from DOS prompt in windows (without )? I used the sleep command simply because the script would unduce the CPU to run at 100% while waiting. Usano wrote: > Thanks AW. I have used the following... > > require "Win32API" > > print "Answer (Y/N): " > char = Win32API.new("crtdll", "_getch", [], 'L').Call > putc char > putc ?\n > if [?Y,?y].include? char > puts "You pressed 'Y'" > else > puts "You didn't press 'Y'" > end > > ....but I have a question about your use of kbhit() and sleep(). I > assume that while my code is waiting, Windows will still allocate time > slices for other programs that might be running at the same time. > Does using kbhit() and sleep() allow time to be given to other threads > that may be running concurrently in the same Ruby program as the > _getch()? > > Thank you for the help, > - Usano > > On Mon, 16 Dec 2002 20:18:58 GMT, AW > wrote: > > >This (or something like it) was posted a while ago by some helpfull > >person (I forget their name). Its a win32 version. > > > >require 'Win32API' > > > >kbhit = Win32API.new("crtdll", "_kbhit", [], 'L') > >getch = Win32API.new("crtdll", "_getch", [], 'L') > >loop{ > > puts " #{getch.Call.chr} pressed" if kbhit.Call != 0 > > sleep 0.1 > >} > > > >>Can anyone tell me how to get a single character from the command line > >>prompt without the user pressing ? This would be in response > >>to a Yes/No question or a number from a simple printed menu. > >> > >>I did notice something in my "The Ruby Way" book which talked about > >>the feature being in TK. Is there nothing in standard Ruby? > >> > >>Thanks, > >> - Usano >