Non blocking keypress reading?

S

Sard Aukary

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
 
J

Jan Svitok

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
 
M

Michael Fellinger

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: (e-mail address removed)
Website: http://weez-int.com
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top