Detecting key presses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tylertacky@gmail.com

    #1

    Detecting key presses

    Ok, I'm pretty new to python, so this might be a stupid question. I'm
    trying to write a simple text-based pong clone, and I can't figure out
    how to read key presses to move the paddles. I just need something that
    does the same thing as getch() and kbhit(). I can't use those because
    their windows only, and I'm running Linux.

    Someone suggested using curses, but that does crazy things with my
    output, and leaves the terminal unusable after the program closes.

  • Fan Zhang

    #2
    Re: Detecting key presses

    On 2006-06-18 13:20:06, tylertacky write:[color=blue]
    >Ok, I'm pretty new to python, so this might be a stupid question. I'm
    >trying to write a simple text-based pong clone, and I can't figure out
    >how to read key presses to move the paddles. I just need something that
    >does the same thing as getch() and kbhit(). I can't use those because
    >their windows only, and I'm running Linux.
    >
    >Someone suggested using curses, but that does crazy things with my
    >output, and leaves the terminal unusable after the program closes.
    >
    >--
    >http://mail.python.org/mailman/listinfo/python-list[/color]

    The python.org site has a faq about getting keystroke.
    It uses termios and fcntl modules.
    Here's the link:
    Contents: Library and Extension FAQ- General Library Questions- How do I find a module or application to perform task X?, Where is the math.py (socket.py, regex.py, etc.) source file?, How do I mak...


    Hope it can help you!
    -----
    Fan Zhang

    Comment

    • Diez B. Roggisch

      #3
      Re: Detecting key presses

      tylertacky@gmai l.com schrieb:[color=blue]
      > Ok, I'm pretty new to python, so this might be a stupid question. I'm
      > trying to write a simple text-based pong clone, and I can't figure out
      > how to read key presses to move the paddles. I just need something that
      > does the same thing as getch() and kbhit(). I can't use those because
      > their windows only, and I'm running Linux.
      >
      > Someone suggested using curses, but that does crazy things with my
      > output, and leaves the terminal unusable after the program closes.[/color]

      Maybe using pygame for a graphical version is appealing to you -
      additionally, the event-sytem of it will do what you ask for.

      Diez

      Comment

      • peter

        #4
        Re: Detecting key presses

        Not a stupid question at all - its something I was looking for, and was
        (and still am) surprised not to find a cross platform implementation.
        It must be possible - for a short while I dabbled with yabasic and
        there the same source code would recognise a keypress in both Windows
        and Linux.

        My solution was:-

        # Await key and return code - dos only
        def _keycode_msvcrt (self):
        #Loop till key pressed
        while 1:
        if msvcrt.kbhit():
        k=ord(msvcrt.ge tch())
        if k==0 or k==224: #Special keys
        k=1000+ord(msvc rt.getch()) #return 1000+ 2nd code
        pass
        break
        pass
        pass
        return k

        # Await key and return code - linux only
        def _keycode_linux2 (self):
        # Loop till key pressed
        # Set up keycode list
        a=[0,0,0,0,0,0]

        # Press a key and populate list
        try:
        os.system('stty -icanon')
        os.system('stty -echo')
        a[0]=ord(sys.stdin. read(1))
        if a[0]==27:
        a[1]=ord(sys.stdin. read(1))
        if a[1]==91:
        a[2]=ord(sys.stdin. read(1))
        if (a[2]>=49 and a[2]<=54) or a[2]==91:
        a[3]=ord(sys.stdin. read(1))
        if a[3]>=48 and a[3]<=57:
        a[4]=ord(sys.stdin. read(1))
        finally:
        os.system('stty echo')
        os.system('stty icanon')

        # Decode keypress
        if a==[ 10, 0, 0, 0, 0, 0]: k= 13 # Enter
        elif a==[ 27, 27, 0, 0, 0, 0]: k= 27 # Esc (double
        press)
        elif a==[ 27, 91, 91, 65, 0, 0]: k=1059 # F1
        elif a==[ 27, 91, 91, 66, 0, 0]: k=1060 # F2
        elif a==[ 27, 91, 91, 67, 0, 0]: k=1061 # F3
        elif a==[ 27, 91, 91, 68, 0, 0]: k=1062 # F4
        elif a==[ 27, 91, 91, 69, 0, 0]: k=1063 # F5
        elif a==[ 27, 91, 49, 55, 126, 0]: k=1064 # F6
        elif a==[ 27, 91, 49, 56, 126, 0]: k=1065 # F7
        elif a==[ 27, 91, 49, 57, 126, 0]: k=1066 # F8
        elif a==[ 27, 91, 50, 48, 126, 0]: k=1067 # F9
        elif a==[ 27, 91, 50, 49, 126, 0]: k=1068 # F10
        elif a==[ 27, 91, 50, 51, 126, 0]: k=1133 # F11
        elif a==[ 27, 91, 50, 52, 126, 0]: k=1134 # F12
        elif a==[ 27, 91, 50, 126, 0, 0]: k=1082 # Ins
        elif a==[ 27, 91, 51, 126, 0, 0]: k=1083 # Del
        elif a==[ 27, 91, 49, 126, 0, 0]: k=1071 # Home
        elif a==[ 27, 91, 52, 126, 0, 0]: k=1079 # End
        elif a==[ 27, 91, 53, 126, 0, 0]: k=1073 # Pg Up
        elif a==[ 27, 91, 54, 126, 0, 0]: k=1081 # Pg Dn
        elif a==[ 27, 91, 65, 0, 0, 0]: k=1072 # Up
        elif a==[ 27, 91, 66, 0, 0, 0]: k=1080 # Down
        elif a==[ 27, 91, 68, 0, 0, 0]: k=1075 # Left
        elif a==[ 27, 91, 67, 0, 0, 0]: k=1077 # Right
        elif a==[127, 0, 0, 0, 0, 0]: k= 8 # Backspace
        else: k=a[0] # Ascii code

        # Done
        return k


        # Return key code
        def key(self,case=' NONE'):

        # Get OS name and call platform specific function
        a=sys.platform
        if a=='linux2': #Linux (works on Fedora
        Core 1 and 3)
        k=self._keycode _linux2()
        elif a=='win32': #windows
        k=self._keycode _msvcrt()
        else: #unknown
        k=ord(raw_input ())

        # Adjust case
        if case=='UPPER':
        if k>=97 and k<=122:
        k=k-32
        if case=='LOWER':
        if k>=65 and k<=90:
        k=k+32

        # Done
        return k

        A bit clumsy, I know (for example it needs a a double press to
        recognise the escape key), and I'm not sure I understand why it works,
        but for me it was a passable solution.

        Peter

        Comment

        • blue99@interia.pl

          #5
          Re: Detecting key presses

          [color=blue]
          > Someone suggested using curses, but that does crazy things with my
          > output, and leaves the terminal unusable after the program closes.[/color]

          It's very good suggestion but you should use also initscr
          and endwin functions.

          Regards,
          Rob

          Comment

          Working...