Non blocking keypress reading?

Discussion in 'Ruby' started by Sard Aukary, Sep 8, 2006.

  1. Sard Aukary

    Sard Aukary Guest

    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/.
     
    Sard Aukary, Sep 8, 2006
    #1
    1. Advertising

  2. William Crawford, Sep 8, 2006
    #2
    1. Advertising

  3. Sard Aukary

    Jan Svitok Guest

    On 9/8/06, Sard Aukary <> 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
     
    Jan Svitok, Sep 8, 2006
    #3
  4. 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:
    Website: http://weez-int.com
     
    Michael Fellinger, Sep 11, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    12,756
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    960
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    851
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    748
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    283
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page