I am new to using Ruby and I am writing my first real programs and I have something I was wonndering about:
I have a menu that I would like users to respond to simply by entering a single character. I have tried both STDIN.getc and gets, but both require a carriage return. Is there a command I haven't found that will actively read STDIN to process a single character as soon as it is entered?
Hi there,
I am new to using Ruby and I am writing my first real programs and I have something I was wonndering about:
I have a menu that I would like users to respond to simply by entering a single character. I have tried both STDIN.getc and gets, but both require a carriage return. Is there a command I haven't found that will actively read STDIN to process a single character as soon as it is entered?
Thanks in advance,
Keith
This is a common question. You need a library (gem) that provides this function. I have heard that highline, ncurses, and termios have this. In ncurses, it's called "getch", but I don't know about the others.
if RUBY_PLATFORM =~ /win32/
require 'Win32API'
Kbhit = Win32API.new("msvcrt", "_kbhit", , 'I')
Getch = Win32API.new("msvcrt", "_getch", , 'I')
def getkey
sleep 0.01
return nil if Kbhit.call.zero?
c = Getch.call
c = Getch.call + 256 if c.zero? || c == 0xE0
c
end
else
def getkey
select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
end
end
5.times{
begin end until key = getkey
print key.chr
}
···
On Aug 25, 3:05 pm, "Barr, Keith" <keith.b...@lmco.com> wrote:
Hi there,
I am new to using Ruby and I am writing my first real
programs and I have something I was wonndering about:
I have a menu that I would like users to respond to
simply by entering a single character. I have tried
both STDIN.getc and gets, but both require a carriage
return. Is there a command I haven't found that will
actively read STDIN to process a single character as
soon as it is entered?
Hi there,
I am new to using Ruby and I am writing my first real programs and I have something I was wonndering about:
I have a menu that I would like users to respond to simply by entering a single character. I have tried both STDIN.getc and gets, but both require a carriage return. Is there a command I haven't found that will actively read STDIN to process a single character as soon as it is entered?
Thanks in advance,
Keith
This is a common question. You need a library (gem) that provides this function. I have heard that highline, ncurses, and termios have this. In ncurses, it's called "getch", but I don't know about the others.
Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
On Aug 25, 3:05 pm, "Barr, Keith" <keith.b...@lmco.com> wrote:
> I have a menu that I would like users to respond to
> simply by entering a single character.
def getkey
select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
end
Why `c=´? Why twice?
It doesn't work here; it still waits for the enter key.
I doubt whether there is any other way than using termios.
Am Sonntag, 26. Aug 2007, 10:05:07 +0900 schrieb Wai Tsang:
Bertram Scharpf wrote:
> Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
>> On Aug 25, 3:05 pm, "Barr, Keith" <keith.b...@lmco.com> wrote:
>>
>> select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
> Why `c=´? Why twice?
It means if select( [$stdin], nil, nil, 0.01 ) return true,
then c = $stdin.getc; otherwise c = nil.
The variable c will not be used and the assignment is
mentioned twice. Besides that it is questionable whether an
assignment in a ?: expression will parse how the author
intended.
That the function will return `true' is just a lie.
Bertram, no need for inflamatory statements. Calling something 'just a lie' implies that the person is maliciously trying to spread false information. It might be more appropriate to simply say that it is a mistake and explain why.
···
On Aug 26, 2007, at 9:32 AM, Bertram Scharpf wrote:
Hi,
Am Sonntag, 26. Aug 2007, 10:05:07 +0900 schrieb Wai Tsang:
Bertram Scharpf wrote:
Am Sonntag, 26. Aug 2007, 07:00:06 +0900 schrieb William James:
On Aug 25, 3:05 pm, "Barr, Keith" <keith.b...@lmco.com> wrote:
select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil
Why `c=´? Why twice?
It means if select( [$stdin], nil, nil, 0.01 ) return true,
then c = $stdin.getc; otherwise c = nil.
The variable c will not be used and the assignment is
mentioned twice. Besides that it is questionable whether an
assignment in a ?: expression will parse how the author
intended.
That the function will return `true' is just a lie.