Equivalent of pascal's keypressed?

Hi,

I’m looking for an equivalent to pascal’s old keypressed.
I’m trying to execute a block till the user presses a key, something like :

while (!keypressed)
print "."
end

Is there any such functionality ?

I’d also like to know if there is a way to trap the user pressing Ctrl-C
and do some stuff before closing ?

Thank you very much,
philip

I’d also like to know if there is a way to trap the
user pressing Ctrl-C
and do some stuff before closing ?

you should use something like

trap “SIGINT” { block_to_do (some, stuff)}

^C in unix sends SIGINT , tipically.

You could even try
at_exit { endBlock }

In this way you’ll have endBlock anyway.
I suppose that trap SIGINT won’t work in dos, but you
could do something like this to check if the program
actually ended or had been interrupted

#start

#good end
$ended=TRUE

at_exit {
unless $ended
<interrupted, exception…>
end
}

···

Yahoo! Musica: notizie, recensioni, classifiche, speciali multimediali
http://it.yahoo.com/mail_it/foot/?http://it.music.yahoo.com/

Hi,

···

----- Original Message -----
From: “Philip Mateescu” pmateescu@novosadhayes.com
Newsgroups: comp.lang.ruby
Sent: Monday, September 09, 2002 9:41 AM
Subject: Equivalent of pascal’s keypressed ?

Hi,

I’m looking for an equivalent to pascal’s old keypressed.
I’m trying to execute a block till the user presses a key, something like
:

while (!keypressed)
print "."
end

Is there any such functionality ?

I’d also like to know if there is a way to trap the user pressing Ctrl-C
and do some stuff before closing ?

Thank you very much,
philip

Is the following what you are looking for?

require ‘Win32API’

kbhit = Win32API.new(“crtdll”, “_kbhit”, [], ‘L’)
getch = Win32API.new(“crtdll”, “_getch”, [], ‘L’)

trap(“INT”) do
puts "ouch"
exit
end

while kbhit.Call==0
print "."
end
puts “#{getch.Call.chr} pressed”

Park Heesob

If we implemented a cross-platform “keypressed”, might there be
any chance of it getting into the builtin library?

A frequent beginner C/C++ FAQ is the one above. It seems like a
high-level language such as ruby should be able to give a good
solution to it, rather than a lengthy answer involving Win32 API
or Termios witchery.

  • Ryan King
···

On 2002.09.09, Philip Mateescu pmateescu@novosadhayes.com wrote:

Hi,

I’m looking for an equivalent to pascal’s old keypressed.
I’m trying to execute a block till the user presses a key, something like :

while (!keypressed)
print "."
end

Is there any such functionality ?

Hi,

···

At Mon, 9 Sep 2002 13:49:58 +0900, Park Heesob wrote:

kbhit = Win32API.new(“crtdll”, “_kbhit”, [], ‘L’)
getch = Win32API.new(“crtdll”, “_getch”, [], ‘L’)

Why “crtdll” rather than “msvcrt”?


Nobu Nakada

I’m looking for an equivalent to pascal’s old keypressed.
I’m trying to execute a block till the user presses a key, something like :

while (!keypressed)
print "."
end

Is there any such functionality ?

If we implemented a cross-platform “keypressed”, might there be
any chance of it getting into the builtin library?

A frequent beginner C/C++ FAQ is the one above. It seems like a
high-level language such as ruby should be able to give a good
solution to it, rather than a lengthy answer involving Win32 API
or Termios witchery.

select[[$stdin], nil, nil]

if key_pressed() does not allow mixing with select(), it lost its efficiency.

disclaimer: unlikely to work on Win*

±-- Kero ------------------------------ kero@chello.nl —+

Don’t split your mentality without thinking twice |
Proud like a God – Guano Apes |
±-- M38c ---------- http://httpd.chello.nl/k.vangelder —+

Hi,
nobu.nokada@softhome.net wrote in message
news:200209090814.g898E3Q08380@sharui.nakada.kanuma.tochigi.jp

Hi,

kbhit = Win32API.new(“crtdll”, “_kbhit”, [], ‘L’)
getch = Win32API.new(“crtdll”, “_getch”, [], ‘L’)

Why “crtdll” rather than “msvcrt”?


Nobu Nakada

I don’t know.
Just quote from the Ruby source code (/ext/Win32API/getch.rb).

Park Heesob

···

At Mon, 9 Sep 2002 13:49:58 +0900, > Park Heesob wrote:

^ There’s the problem. Surely something could be worked out for
Windows, too. However, expecting individual programmers to
reinvent those wheels is a violation of the DRY principle.

I think it would be handy to have key_pressed, or preferably
"getch", as a part of the standard library.

I could be wrong, though.

  • Ryan King
···

On 2002.09.11, Kero van Gelder kero@chello.nl wrote:

If we implemented a cross-platform “keypressed”, might there be
any chance of it getting into the builtin library?

A frequent beginner C/C++ FAQ is the one above. It seems like a
high-level language such as ruby should be able to give a good
solution to it, rather than a lengthy answer involving Win32 API
or Termios witchery.

select[[$stdin], nil, nil]

if key_pressed() does not allow mixing with select(), it lost its efficiency.

disclaimer: unlikely to work on Win*

Hi,

···

At Mon, 9 Sep 2002 17:50:46 +0900, Park Heesob wrote:

kbhit = Win32API.new(“crtdll”, “_kbhit”, [], ‘L’)
getch = Win32API.new(“crtdll”, “_getch”, [], ‘L’)

Why “crtdll” rather than “msvcrt”?


Nobu Nakada

I don’t know.
Just quote from the Ruby source code (/ext/Win32API/getch.rb).

Hmmm, it’s too old. Now mswin32/mingw32 version use msvcrt.dll
by default, and Termios should be better under cygwin.


Nobu Nakada

If we implemented a cross-platform “keypressed”, might there be
any chance of it getting into the builtin library?

A frequent beginner C/C++ FAQ is the one above. It seems like a
high-level language such as ruby should be able to give a good
solution to it, rather than a lengthy answer involving Win32 API
or Termios witchery.

select[[$stdin], nil, nil]

if key_pressed() does not allow mixing with select(), it lost its
efficiency.

disclaimer: unlikely to work on Win*

^ There’s the problem. Surely something could be worked out for
Windows, too. However, expecting individual programmers to
reinvent those wheels is a violation of the DRY principle.

I think it would be handy to have key_pressed, or preferably
"getch", as a part of the standard library.

I could be wrong, though.

I think I’m generally in favor of this kind of thing,
but I’d like it to be isolated in some kind of “OS-
sensitive” library.

It’s likely to be implemented differently on every
different OS, not just Win and *nix.

I’m not sure how much of that it’s appropriate to reveal
to the coder and how much to conceal.

The old fork() that worked under Cygwin is a case in point.
Should we have a fork() on Win32 that perhaps calls
CreateProcess? Or should we just not even pretend that
Win is anything like *nix? Many of the file operations
work transparently between the two. Where do you draw
the line?

Hal

···

----- Original Message -----
From: “Ryan King” rking@panoptic.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, September 10, 2002 4:14 PM
Subject: Re: Equivalent of pascal’s keypressed ?

On 2002.09.11, Kero van Gelder kero@chello.nl wrote: