Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listening for Keypress while Working

28 views
Skip to first unread message

James Edward Gray II

unread,
Sep 9, 2004, 3:23:38 PM9/9/04
to
I have this long running process which shows output as it goes. I need
to listen for a key press, but keep writing out my results. What's the
best way to go about this, select() over STDIN in my processing loop?
Any tips appreciated. Thanks.

James Edward Gray II

James Edward Gray II

unread,
Sep 9, 2004, 4:21:55 PM9/9/04
to
On Sep 9, 2004, at 2:23 PM, James Edward Gray II wrote:

> What's the best way to go about this, select() over STDIN in my
> processing loop?

Hmm, I don't appear to be able to select() over STDIN. Now I'm really
in need of a good idea... <laughs>

James Edward Gray II

Lennon Day-Reynolds

unread,
Sep 9, 2004, 4:32:15 PM9/9/04
to
How about this:

--
require 'io/wait'

loop do
# my processing stuff
if STDIN.ready?
# handle input
end
end

--
Lennon
rcoder.net


Bill Kelly

unread,
Sep 9, 2004, 4:42:40 PM9/9/04
to
Hi,

Which operating system?

On win32... I haven't tried it... they seem to have
provided _kbhit() in <conio.h>... Apparently implemented
in terms of win32 PeekConsoleInput()...

I dunno if it really works...? :) But you might be able
to try calling it using ruby/dl...

On *nix, I think if you put the tty into raw mode you should
be able to select on stdin and detect unbuffered keypresses.


Sorry I can't be of more help . .

Regards,

Bill


Bill Kelly

unread,
Sep 9, 2004, 4:54:17 PM9/9/04
to

From: "Bill Kelly" <bi...@cts.com>

> From: "James Edward Gray II" <ja...@grayproductions.net>
> > On Sep 9, 2004, at 2:23 PM, James Edward Gray II wrote:
> >
> > > What's the best way to go about this, select() over STDIN in my
> > > processing loop?
> >
> > Hmm, I don't appear to be able to select() over STDIN. Now I'm really
> > in need of a good idea... <laughs>
>
> On win32... I haven't tried it... they seem to have
> provided _kbhit() in <conio.h>... Apparently implemented
> in terms of win32 PeekConsoleInput()...

Wow it actually seems to work (!)

>> require 'dl'
=> true
>> kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
=> #<Win32API:0x2b84670>
>> kbhit.call
=> 0
>> 10.times { puts kbhit.call; sleep 1 }
0
0
1 # I hit spacebar here....
1
1
1
1
1
1
1


HTH,

Regards,

Bill


James Edward Gray II

unread,
Sep 9, 2004, 5:07:14 PM9/9/04
to
On Sep 9, 2004, at 3:42 PM, Bill Kelly wrote:

> On *nix, I think if you put the tty into raw mode you should
> be able to select on stdin and detect unbuffered keypresses.

That was the trick I needed. Thank you.

Also thanks to Lennon Day-Reynolds, who made it look good. ;)

James Edward Gray II

0 new messages