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.

Michael L. 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.

Michael L. 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.

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…

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 G. (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

Michael L. ha scritto:

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

Ari B. 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 G. (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:

On Sep 1, 11:01 am, Michael L. [email protected] wrote:

   puts info

p
p
#whatever letter you enter it will respond with that letter that you
entered.

Posted viahttp://www.ruby-forum.com/.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs