Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Non blocking keypress reading?

Reply
Thread Tools

Non blocking keypress reading?

 
 
Sard Aukary
Guest
Posts: n/a
 
      09-08-2006
On Windows XP is there a way to test that a certain key has been pressed
without blocking?

I’ve tried using curses but it blocks the current thread and any other
thread until a key is pressed.


I want to give the user 10 seconds so make a selection before a default
action is taken.

Here is the test code I’ve got;


require 'curses'
include Curses
init_screen
noecho
trap(0) { echo }

t1 = Time.new
Thread.new do

#example timer thread
while(true) do
x = Time.new - t1
h = 3+3
setpos(12,10)

addstr("#{x}")

end
end

setpos(1,5)
addstr("Please select your location")
setpos(3,5)
addstr(" 1 - Work")
setpos(4,5)
addstr(" 2 - Home")
setpos(6,5)
addstr("Esc - Exit")
while (c = getch) != ?\e do
# check what key is pressed and do stuff
end

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
William Crawford
Guest
Posts: n/a
 
      09-08-2006
Sard Aukary wrote:
> On Windows XP is there a way to test that a certain key has been pressed
> without blocking?


From my quick search, I didn't find a way. Maybe this will help though?

http://blade.nagaokaut.ac.jp/cgi-bin...ruby-core/4435

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      09-08-2006
On 9/8/06, Sard Aukary <(E-Mail Removed)> wrote:
> On Windows XP is there a way to test that a certain key has been pressed
> without blocking?
>
> I've tried using curses but it blocks the current thread and any other
> thread until a key is pressed.
>
>
> I want to give the user 10 seconds so make a selection before a default
> action is taken.
>
> Here is the test code I've got;
>
>
> require 'curses'
> include Curses
> init_screen
> noecho
> trap(0) { echo }
>
> t1 = Time.new
> Thread.new do
>
> #example timer thread
> while(true) do
> x = Time.new - t1
> h = 3+3
> setpos(12,10)
>
> addstr("#{x}")
>
> end
> end
>
> setpos(1,5)
> addstr("Please select your location")
> setpos(3,5)
> addstr(" 1 - Work")
> setpos(4,5)
> addstr(" 2 - Home")
> setpos(6,5)
> addstr("Esc - Exit")
> while (c = getch) != ?\e do
> # check what key is pressed and do stuff
> end


search the archive for kbhit

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      09-11-2006
On Friday 08 September 2006 20:42, Sard Aukary wrote:
> On Windows XP is there a way to test that a certain key has been pressed
> without blocking?
>
> I=E2=80=99ve tried using curses but it blocks the current thread and any =

other
> thread until a key is pressed.
>
>
> I want to give the user 10 seconds so make a selection before a default
> action is taken.


require 'timeout'
begin
=C2=A0 Timeout.timeout(10) do
=C2=A0 =C2=A0 key =3D gets
=C2=A0 end
rescue Timeout::Error
=C2=A0 key =3D default
end

>
> Here is the test code I=E2=80=99ve got;
>
>
> require 'curses'
> include Curses
> init_screen
> noecho
> trap(0) { echo }
>
> t1 =3D Time.new
> Thread.new do
>
> #example timer thread
> while(true) do
> x =3D Time.new - t1
> h =3D 3+3
> setpos(12,10)
>
> addstr("#{x}")
>
> end
> end
>
> setpos(1,5)
> addstr("Please select your location")
> setpos(3,5)
> addstr(" 1 - Work")
> setpos(4,5)
> addstr(" 2 - Home")
> setpos(6,5)
> addstr("Esc - Exit")
> while (c =3D getch) !=3D ?\e do
> # check what key is pressed and do stuff
> end


=2D-=20
Weez International Limited

East Roppongi Bldg 5F, 509
3-16-35 Roppongi, Minato-ku Tokyo #106-0032
Tel: 81-(0)3-3505-3881 Fax: 81-(0)3-3505-3883
E-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
Website: http://weez-int.com

 
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
Non blocking socket keep blocking on read ? Serge Savoie Ruby 4 10-01-2008 03:16 PM
Switching from Non-Blocking to Blocking IO Christian Java 5 12-02-2007 11:24 PM
Non-blocking and semi-blocking Sockets class. nukleus Java 14 01-22-2007 08:22 PM
Blocking and non blocking assignment in VHDL Hendra Gunawan VHDL 1 04-08-2004 06:03 AM
blocking i/o vs. non blocking i/o (performance) Andre Kelmanson C Programming 3 10-12-2003 02:09 PM



Advertisments