How to read a single character from DOS prompt in windows (without <Enter>)?

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

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

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 sturmpanzer@metacrawler.com 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

Hmmmm. This worked ok in my little test file, but the character is
still in a buffer somewhere. In my program, I hit ‘n’ to not do
something, then ‘puts’ to the command line with no problem – then
when I do a ‘gets’ the ‘n’ prints out. What the heck?

  • Usano

require ‘Win32API’

print "Question? (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 pressed something other than ‘Y’"
end
gets # this prints the ‘y’ or ‘n’ !!!

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 sturmpanzer@metacrawler.com > 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

Im not understanding what the problem is? I tried the script and seems
to work like its suppost to. maybe your using doskey?

AW

Usano wrote:

···

Hmmmm. This worked ok in my little test file, but the character is
still in a buffer somewhere. In my program, I hit ‘n’ to not do
something, then ‘puts’ to the command line with no problem – then
when I do a ‘gets’ the ‘n’ prints out. What the heck?

  • Usano

require ‘Win32API’

print "Question? (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 pressed something other than ‘Y’”
end
gets # this prints the ‘y’ or ‘n’ !!!