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.
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 ( Google Code Archive - Long-term storage for Google Code Project Hosting. ) 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.
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
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
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