Listening for Keypress while Working

  • Thread starter James Edward Gray II
  • Start date
J

James Edward Gray II

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
 
J

James Edward Gray II

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
 
L

Lennon Day-Reynolds

How about this:

--
require 'io/wait'

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

Bill Kelly

Hi,

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

Bill Kelly

From: "Bill Kelly said:
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')
=> # said:
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
 
J

James Edward Gray II

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top