loop until keypress (Windows XP)

Discussion in 'Python' started by placid, Aug 10, 2006.

  1. placid

    placid Guest

    Hi all,


    Im using the cmd module and i have command that loops and keeps on
    printing text, what i want to be able to do is loop until the user
    presses a particular key, say Q/q ? I tried the following code;


    line = raw_input ("press q to abort")
    while line[0] != "q":
    """ keep printing text """
    line = raw_input ("press q to abort")

    but raw_input blocks for input until the newline char. So i then tried
    the following code


    import sys

    chr = sys.stdin.read(1)
    while chr != "q":
    """ keep printing text """
    chr = sys.stdin.read(1)

    but again this blocks too.

    is there a way to do this, wait for user input but dont block? I could
    use a thread that just does the previous code block but i already have
    three Thread classes, its just getting too complex with threads!

    Cheers
     
    placid, Aug 10, 2006
    #1
    1. Advertising

  2. placid wrote:
    > is there a way to do this, wait for user input but dont block?


    Hi,

    The msvcrt module should do what you want. Here is a sample:

    import msvcrt

    chr = 0
    while chr != 'q':
    """ keep printing text """
    if msvcrt.kbhit():
    chr = msvcrt.getch()


    Keep in mind that this will only work on windows.

    -Farshid
     
    Farshid Lashkari, Aug 10, 2006
    #2
    1. Advertising

  3. At Thursday 10/8/2006 02:19, placid wrote:

    >chr = sys.stdin.read(1)
    >while chr != "q":
    > """ keep printing text """
    > chr = sys.stdin.read(1)
    >
    >but again this blocks too.
    >
    >is there a way to do this, wait for user input but dont block? I could
    >use a thread that just does the previous code block but i already have
    >three Thread classes, its just getting too complex with threads!


    If your script only needs to be run on Windows -as the subject
    suggests- you can use the msvcrt module:

    from msvcrt import kbhit,getch

    stop = False
    while not stop:
    print "Hello world!"
    if kbhit(): stop = getch()=='q'

    kbhit() is used to detect when a keypress is waiting, so the next
    getch() will not block.



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Aug 10, 2006
    #3
  4. placid

    Andy Terrel Guest

    If you did want a linux version you could just make people send a
    KeyboardInterupt.


    try:
    print "Press ^C to stop"
    loop
    except KeyboardInterrupt:
    some stop action or just pass
     
    Andy Terrel, Aug 10, 2006
    #4
  5. placid

    placid Guest

    Gabriel Genellina wrote:
    > At Thursday 10/8/2006 02:19, placid wrote:
    >
    > >chr = sys.stdin.read(1)
    > >while chr != "q":
    > > """ keep printing text """
    > > chr = sys.stdin.read(1)
    > >
    > >but again this blocks too.
    > >
    > >is there a way to do this, wait for user input but dont block? I could
    > >use a thread that just does the previous code block but i already have
    > >three Thread classes, its just getting too complex with threads!

    >
    > If your script only needs to be run on Windows -as the subject
    > suggests- you can use the msvcrt module:
    >
    > from msvcrt import kbhit,getch
    >
    > stop = False
    > while not stop:
    > print "Hello world!"
    > if kbhit(): stop = getch()=='q'
    >
    > kbhit() is used to detect when a keypress is waiting, so the next
    > getch() will not block.


    Thanks for the solution ive got it working. You were correct, the
    script needs to run only on Windows.

    Cheers (and thanks all for the replies)
     
    placid, Aug 10, 2006
    #5
  6. Am Wed, 09 Aug 2006 22:19:24 -0700 schrieb placid:

    > Hi all,
    >
    >
    > Im using the cmd module and i have command that loops and keeps on
    > printing text, what i want to be able to do is loop until the user
    > presses a particular key, say Q/q ? I tried the following code;
    >


    There is a portable getch() implementation:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892

    It does not loop, it waits until the key is pressed. I hope that is what
    you want.
     
    Thomas Guettler, Aug 10, 2006
    #6
  7. placid

    placid Guest

    Thomas Guettler wrote:
    > Am Wed, 09 Aug 2006 22:19:24 -0700 schrieb placid:
    >
    > > Hi all,
    > >
    > >
    > > Im using the cmd module and i have command that loops and keeps on
    > > printing text, what i want to be able to do is loop until the user
    > > presses a particular key, say Q/q ? I tried the following code;
    > >

    >
    > There is a portable getch() implementation:
    >
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
    >
    > It does not loop, it waits until the key is pressed. I hope that is what
    > you want.



    i want a read operation that doesn't block and im not concerned if my
    program is Windows only, which is. (im using wmi module too).

    msvcrt.kbhit() and msvcrt.getch() is what i need!


    Cheers
     
    placid, Aug 11, 2006
    #7
    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. Remi Villatel

    Loop until condition is true

    Remi Villatel, Jun 18, 2005, in forum: Python
    Replies:
    40
    Views:
    1,127
    Mike Meyer
    Jun 23, 2005
  2. yogi_bear_79
    Replies:
    2
    Views:
    256
    yogi_bear_79
    Mar 11, 2008
  3. eblume
    Replies:
    3
    Views:
    217
    Peter Otten
    Jan 12, 2011
  4. Peter Szinek

    Do something until keypress

    Peter Szinek, Oct 26, 2006, in forum: Ruby
    Replies:
    1
    Views:
    269
    Jan Svitok
    Oct 26, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page