Keyboard input: one character

hi to all!
i've seen this question posted many times without a single definitive answer. what i'm trying to get is a single character from the keyboard in a multiplatform solution: i hit the key "h" and rubu knows "h" has been pressed. i need it to detect the imput for a game i'm working on: insertin the key-detection into a loop would actually make the player interact with the game.

i've come across the library highline ( http://highline.rubyforge.org/ ). the simplest program

require "highline/system_extensions"
include HighLine::SystemExtensions
#print "Enter one character: "
while true
   char = get_character
   if char != nil
     puts char.chr
   end
end

only works to a certain extend. it puts out the received character only after the end of the program, not during its execution, for some strange reason. moreover it blocks the execution of the program while integrated into a gosu ( http://code.google.com/p/gosu/ ) framework.

on the other hand, any attempt on using curses results, don't know why, in the error:

LINES value must be >= 2 and <= -2616: got 1
initscr(): LINES=1 COLS=1: too small.

anyone can help?

gu wrote:

i've come across the library highline ( http://highline.rubyforge.org/
). the simplest program

require "highline/system_extensions"
include HighLine::SystemExtensions
#print "Enter one character: "
while true
   char = get_character
   if char != nil
     puts char.chr
   end
end

info = gets.chomp
  if info != nil
puts info
end

or more elaborately you would use it like the following:

info = gets.chomp
if info != nil
  puts "You Entered the Character: #{info}"
end

Hoped this helped.

···

--
Posted via http://www.ruby-forum.com/\.

Michael Linfield ha scritto:

info = gets.chomp
  if info != nil
puts info
end

hi and thanks for the reply! well.. the following code

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

actually doesn't outputs a thing..

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

actually doesn't outputs a thing..

lol. i didnt mean for it to be used in a while loop.

···

--
Posted via http://www.ruby-forum.com/\.

Michael Linfield wrote:

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

actually doesn't outputs a thing..

lol. i didnt mean for it to be used in a while loop.

And i actually just got home and tested that while loop, it works...ur
not doing this in IRB right?

testprog.rb

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

ruby testprog.rb

output:
h
h
i
i
p
p
#whatever letter you enter it will respond with that letter that you
entered.

···

--
Posted via http://www.ruby-forum.com/\.

Michael Linfield ha scritto:

Michael Linfield wrote:

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

actually doesn't outputs a thing..

lol. i didnt mean for it to be used in a while loop.

And i actually just got home and tested that while loop, it works...ur not doing this in IRB right?

testprog.rb

while true
   info = gets.chomp
   if info != nil
       puts info
   end
end

ruby testprog.rb

output:
h
i
p
#whatever letter you enter it will respond with that letter that you entered.

no, i'm not using irb. maybe there's an os compatibility problem going on? i'm testin it in windows xp sp2..

Hi,

I think gu means to get one character without pressing enter
afterwards. Just like "readkey" in Pascal. As I know this is actually
only possible with Ruby on MS Windows with

(Win32API.new("crtdll", "_getch", , "L").Call).chr

Bye

···

On Sep 1, 11:01 am, Michael Linfield <globyy3...@hotmail.com> wrote:

Michael Linfield wrote:

>> while true
>> info = gets.chomp
>> if info != nil
>> puts info
>> end
>> end

>> actually doesn't outputs a thing..

> lol. i didnt mean for it to be used in a while loop.

And i actually just got home and tested that while loop, it works...ur
not doing this in IRB right?

testprog.rb

>> while true
>> info = gets.chomp
>> if info != nil
>> puts info
>> end
>> end

ruby testprog.rb

output:
h
h
i
i
p
p
#whatever letter you enter it will respond with that letter that you
entered.
--
Posted viahttp://www.ruby-forum.com/.

Well, to help you answer your original question.... Are you using an IDE? A problem that often occurs with Windows IDEs is that it will ask all of the gets() and then puts() them out. In other words, you may have a syncing issue going on.

STDOUT.sync = true

But, in the words of Avdi Grimm (sic), don't make a habit out of it. Synchronous output is a great way to slow down your code.

aRi
-------------------------------------------|
Nietzsche is my copilot

···

On Sep 1, 2007, at 5:20 AM, gu wrote:

no, i'm not using irb. maybe there's an os compatibility problem going on? i'm testin it in windows xp sp2..

Ari Brown wrote:

···

On Sep 1, 2007, at 5:20 AM, gu wrote:

no, i'm not using irb. maybe there's an os compatibility problem
going on? i'm testin it in windows xp sp2..

But, in the words of Avdi Grimm (sic), don't make a habit out of it.
Synchronous output is a great way to slow down your code.

aRi

Some advise: Download Ubuntu...or Debian..whatever suits you...install
it on an extra machine...solve about 99% of ur problems. :wink:
--
Posted via http://www.ruby-forum.com/\.