[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

keyboard input: one character

Gustavo DiPietro

9/1/2007 7:16:00 AM

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.ruby...
). 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.c... ) 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?
8 Answers

Michael Linfield

9/1/2007 8:40:00 AM

0

gu wrote:

> i've come across the library highline ( http://highline.ruby...
> ). 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-....

Gustavo DiPietro

9/1/2007 8:46:00 AM

0

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

Michael Linfield

9/1/2007 8:58:00 AM

0


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

Michael Linfield

9/1/2007 9:02:00 AM

0

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

Gustavo DiPietro

9/1/2007 9:17:00 AM

0

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
> h
> i
> i
> p
> 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..

Ari Brown

9/1/2007 4:31:00 PM

0


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 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



Michael Linfield

9/1/2007 7:39:00 PM

0

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. ;)
--
Posted via http://www.ruby-....

eckhardt.f

9/23/2007 7:23:00 PM

0

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

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