Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Listening for Keypress while Working

Reply
Thread Tools

Listening for Keypress while Working

 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-09-2004
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



 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-09-2004
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



 
Reply With Quote
 
 
 
 
Lennon Day-Reynolds
Guest
Posts: n/a
 
      09-09-2004
How about this:

--
require 'io/wait'

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

--
Lennon
rcoder.net


 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      09-09-2004
Hi,

From: "James Edward Gray II" <>
> 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>


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








 
Reply With Quote
 
Bill Kelly
Guest
Posts: n/a
 
      09-09-2004

From: "Bill Kelly" <>
> From: "James Edward Gray II" <>
> > 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




 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      09-09-2004
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out, what causes constant interruptions while listening using a BT headphone (WinXP)? Michael Moser Wireless Networking 0 06-25-2008 09:50 PM
Listening for keypress in the background Mathias Dahl Python 4 10-23-2005 12:26 PM
Listening for a certain keypress in the background Mathias Dahl Python 0 10-22-2005 11:31 AM
Interruptions in live news listening Realone Player listening Thaqalain Computer Support 6 07-16-2005 02:11 PM
Listening for a Keypress (Console, not GUI) brettk@gmail.com Python 2 06-24-2005 12:02 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57