Alternative to raw_input ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BOOGIEMAN

    #1

    Alternative to raw_input ?

    I need something like "Press any key to continue" code for my program.
    Currently I use : raw_input("Pres s Enter to continue ") but it's lame.
  • Simon Brunning

    #2
    Re: Alternative to raw_input ?

    On Fri, 11 Feb 2005 17:26:04 +0100, BOOGIEMAN <BOOGIEMANPN@ya hoo.com> wrote:[color=blue]
    > I need something like "Press any key to continue" code for my program.
    > Currently I use : raw_input("Pres s Enter to continue ") but it's lame.[/color]

    Err, why?

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • BOOGIEMAN

      #3
      Re: Alternative to raw_input ?

      On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
      [color=blue]
      > Err, why?[/color]

      It looks to ugly this way. I want to press
      any key without ENTER to continue

      Comment

      • Grant Edwards

        #4
        Re: Alternative to raw_input ?

        On 2005-02-11, BOOGIEMAN <BOOGIEMANPN@YA HOO.COM> wrote:[color=blue]
        > On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
        >[color=green]
        >> Err, why?[/color]
        >
        > It looks to ugly this way. I want to press
        > any key without ENTER to continue[/color]

        Like somebody already said: use the WConio module.

        Somebody already posted a link.

        I suggest you go look at it.

        --
        Grant Edwards grante Yow! ... the MYSTERIANS
        at are in here with my
        visi.com CORDUROY SOAP DISH!!

        Comment

        • den

          #5
          Re: Alternative to raw_input ?

          BOOGIEMAN wrote:[color=blue]
          > On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
          >
          >[color=green]
          >>Err, why?[/color]
          >
          >
          > It looks to ugly this way. I want to press
          > any key without ENTER to continue[/color]

          Did you try this:

          import msvcrt
          msvcrt.getch()

          Comment

          • John Lenton

            #6
            Re: Alternative to raw_input ?

            On Fri, Feb 11, 2005 at 05:37:19PM +0100, BOOGIEMAN wrote:[color=blue]
            > On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
            > [color=green]
            > > Err, why?[/color]
            >
            > It looks to ugly this way. I want to press
            > any key without ENTER to continue[/color]

            read the documentation on readline.

            Hmm! it says "Availabili ty: Unix". Any particular reason? readline
            should be fine on OSX and Win32

            --
            John Lenton (john@grulic.or g.ar) -- Random fortune:
            sugar daddy, n.:
            A man who can afford to raise cain.

            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.0 (GNU/Linux)

            iD8DBQFCDOLjgPq u395ykGsRAig6AJ 9extU/bMx7GbBs1DGrWKk fAx8bhACgiE5w
            jhda3sNi9vgHa7+ x2vau4Z8=
            =qbJW
            -----END PGP SIGNATURE-----

            Comment

            • Skip Montanaro

              #7
              Re: Alternative to raw_input ?

              [color=blue]
              > Err, why?[/color]
              [color=blue][color=green]
              >> It looks to ugly this way. I want to press
              >> any key without ENTER to continue[/color][/color]

              How about modifying it to

              raw_input("Pres s ENTER to continue ")

              Skip

              Comment

              • BOOGIEMAN

                #8
                Re: Alternative to raw_input ?

                On Fri, 11 Feb 2005 18:00:08 +0100, den wrote:
                [color=blue]
                > Did you try this:
                >
                > import msvcrt
                > msvcrt.getch()[/color]

                Yes, that's what I need. Thank you.
                BTW, sometimes program continues
                without me pressing any button, why ?

                Comment

                • Peter Hansen

                  #9
                  Re: Alternative to raw_input ?

                  BOOGIEMAN wrote:[color=blue]
                  > On Fri, 11 Feb 2005 18:00:08 +0100, den wrote:
                  >
                  >[color=green]
                  >>Did you try this:
                  >>
                  >>import msvcrt
                  >>msvcrt.getch( )[/color]
                  >
                  >
                  > Yes, that's what I need. Thank you.
                  > BTW, sometimes program continues
                  > without me pressing any button, why ?[/color]

                  Probably because you had already hit a key earlier, and
                  it was still in the keyboard queue.

                  You can flush it first:

                  print prompt
                  while msvcrt.kbhit():
                  msvcrt.getch()
                  msvcrt.getch()

                  Pack that up in a subroutine and call it when
                  you need to pause and continue only when the user
                  has seen your prompt. Any previous keystrokes
                  will be discarded by the loop, then it will wait
                  for a new one to be hit.


                  -Peter

                  Comment

                  • Michael Hoffman

                    #10
                    Re: Alternative to raw_input ?

                    Skip Montanaro wrote:
                    [color=blue]
                    > How about modifying it to
                    >
                    > raw_input("Pres s ENTER to continue ")[/color]

                    You want him to just capitalize ENTER in the current message?
                    --
                    Michael Hoffman

                    Comment

                    • BOOGIEMAN

                      #11
                      Re: Alternative to raw_input ?

                      On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
                      [color=blue]
                      > print prompt
                      > while msvcrt.kbhit():
                      > msvcrt.getch()
                      > msvcrt.getch()[/color]

                      Thanks, it works but without line "print prompt" and also
                      I'm not sure if I should put this function :

                      def cekaj():
                      while msvcrt.kbhit():
                      msvcrt.getch()
                      msvcrt.getch()

                      #Or this one, which sounds more logical according to help
                      #kbhit() - Return true if a keypress is waiting to be read.

                      def cekaj():
                      msvcrt.getch()
                      while msvcrt.kbhit():
                      msvcrt.getch()

                      It works both ways, not sure which one is right

                      Comment

                      • Nick Coghlan

                        #12
                        Re: Alternative to raw_input ?

                        BOOGIEMAN wrote:[color=blue]
                        > On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
                        >
                        >[color=green]
                        >>print prompt
                        >>while msvcrt.kbhit():
                        >> msvcrt.getch()
                        >>msvcrt.getch( )[/color]
                        >
                        >
                        > Thanks, it works but without line "print prompt" and also
                        > I'm not sure if I should put this function :
                        >
                        > def cekaj():
                        > while msvcrt.kbhit():
                        > msvcrt.getch()
                        > msvcrt.getch()
                        >
                        > #Or this one, which sounds more logical according to help
                        > #kbhit() - Return true if a keypress is waiting to be read.
                        >
                        > def cekaj():
                        > msvcrt.getch()
                        > while msvcrt.kbhit():
                        > msvcrt.getch()
                        >
                        > It works both ways, not sure which one is right[/color]

                        Try this:

                        print "Hit a key!"
                        cekaj()
                        print "Nap time!"
                        time.sleep(15)
                        print "Hit another key!"
                        cekaj()

                        with the two different implementations , and see what happens if you hit a key
                        when the 'Nap Time!' prompt appears.

                        Cheers,
                        Nick.

                        P.S. You probably actually want an implementation that looks like:

                        def cekaj(prompt="P ress a key to continue"):
                        while msvcrt.kbhit():
                        msvcrt.getch()
                        if prompt is not None:
                        print prompt
                        msvcrt.getch()

                        And the sample program would look like:
                        cekaj("Hit a key!")
                        print "Nap time!"
                        time.sleep(15)
                        cekaj("Hit another key!")

                        --
                        Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
                        ---------------------------------------------------------------

                        Comment

                        • Peter Hansen

                          #13
                          Re: Alternative to raw_input ?

                          BOOGIEMAN wrote:[color=blue]
                          > On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
                          >[color=green]
                          >>print prompt
                          >>while msvcrt.kbhit():
                          >> msvcrt.getch()
                          >>msvcrt.getch( )[/color]
                          >
                          >
                          > Thanks, it works but without line "print prompt" and also[/color]

                          That was intended to be a hint that you might need
                          to print a prompt to the user (maybe a string contained
                          in a variable named "prompt", for example ;-), or the
                          program might halt without apparent reason.
                          [color=blue]
                          > I'm not sure if I should put this function :
                          >
                          > def cekaj():
                          > while msvcrt.kbhit():
                          > msvcrt.getch()
                          > msvcrt.getch()
                          >
                          > #Or this one, which sounds more logical according to help
                          > #kbhit() - Return true if a keypress is waiting to be read.
                          >
                          > def cekaj():
                          > msvcrt.getch()
                          > while msvcrt.kbhit():
                          > msvcrt.getch()
                          >
                          > It works both ways, not sure which one is right[/color]

                          The point of doing the first approach is that you are
                          reading *all* available keystrokes (in the loop), and
                          then sitting and waiting for one more keystroke before
                          continuing. If you don't read all keystrokes first,
                          then if the user has hit a key, say, five minutes
                          earlier, it will still be sitting in the queue and
                          your program will not actually stop at all.

                          Your second approach sits and waits (if necessary)
                          to read a single keystroke, and then if the user had
                          managed to hit two or more keys** before that, it will
                          consume all remaining keystrokes before continuing.
                          Not exactly the same thing, nor generally quite
                          what you want. Based on what you originally asked for,
                          the former is the correct approach.

                          ** Note that some keys will result in a pair of
                          values being retrieved by getch(), with the first one
                          being (generally... maybe always... I can't remember
                          but it's easy for you to experiment) a 0, and the second
                          being another code that identifies the key. The F5
                          key, for example, returns a 0 and then a 64 on the
                          second call to getch(). If you *really* want *any*
                          key to continue, you'll want to check for this sort
                          of thing as well... though personally I'd just use
                          raw_input() and keep my program portable. <wink>

                          -Peter

                          Comment

                          • BOOGIEMAN

                            #14
                            Re: Alternative to raw_input ?

                            On Sun, 13 Feb 2005 12:08:26 +1000, Nick Coghlan wrote:
                            [color=blue]
                            > Try this:
                            >
                            > print "Hit a key!"
                            > cekaj()
                            > print "Nap time!"
                            > time.sleep(15)
                            > print "Hit another key!"
                            > cekaj()
                            >
                            > with the two different implementations , and see what happens if you hit a key
                            > when the 'Nap Time!' prompt appears.[/color]

                            I see the difference now, thanks

                            Comment

                            • Francis Girard

                              #15
                              Re: Alternative to raw_input ?

                              Le vendredi 11 Février 2005 18:00, den a écrit :[color=blue]
                              > import msvcrt
                              > msvcrt.getch()[/color]

                              I frequently had the problem to have something similar but *portable*.
                              Something as short and simple.

                              Someone have an idea ?

                              Thank you

                              Francis Girard

                              Comment

                              Working...