Polling from keyboard

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

    #1

    Polling from keyboard

    I am trying to find a way to poll the keyboard. In my searching, I
    have found that Windows users are given the msvcrt module. Is there an
    equivilant for Unix systems?

    I am writing a p2p chat application, and would like to ideally approach
    user input in a manner similar to the way I am using poll() to moniter
    the sockets for events

    thanks in advance

    --
    lucas

  • Diez B. Roggisch

    #2
    Re: Polling from keyboard

    sturnfie@gmail. com wrote:
    [color=blue]
    > I am trying to find a way to poll the keyboard. In my searching, I
    > have found that Windows users are given the msvcrt module. Is there an
    > equivilant for Unix systems?
    >
    > I am writing a p2p chat application, and would like to ideally approach
    > user input in a manner similar to the way I am using poll() to moniter
    > the sockets for events[/color]

    I currently do that under Linux using /dev/input/*. However, that's for a
    special case where we want several keyboards read simultaneously.

    I guess what you want is a RAW-mode for your terminal, or alternatively the
    event-system of your windowing system. Then you can get every keystroke.
    But we need somewhat more information to be more helpful I fear.

    diez

    Comment

    • Petr Jakes

      #3
      Re: Polling from keyboard

      I am using following code which I have found on
      http://www.ibiblio.org/obp/py4fun/ few months ago. It works well for my
      purposes. Another way is to use Curses library.
      HTH
      Petr Jakes

      #!/usr/local/bin/python
      #
      # t t y L i n u x . p y
      #
      # getLookAhead reads lookahead chars from the keyboard without
      # echoing them. It still honors ^C etc
      #
      import termios, sys, time
      if sys.version > "2.1" : TERMIOS = termios
      else : import TERMIOS

      def setSpecial () :
      "set keyboard to read single chars lookahead only"
      global oldSettings
      fd = sys.stdin.filen o()
      oldSettings = termios.tcgetat tr(fd)
      new = termios.tcgetat tr(fd)
      new[3] = new[3] & ~TERMIOS.ECHO # lflags
      new[3] = new[3] & ~TERMIOS.ICANON # lflags
      new[6][6] = '\000' # Set VMIN to zero for lookahead only
      termios.tcsetat tr(fd, TERMIOS.TCSADRA IN, new)

      def setNormal () :
      "restore previous keyboard settings"
      global oldSettings
      fd = sys.stdin.filen o()
      termios.tcsetat tr(fd, TERMIOS.TCSADRA IN, oldSettings)

      def readLookAhead () :
      "read max 1 chars (arrow escape seq) from look ahead"
      return sys.stdin.read( 1)

      Comment

      Working...