keypressed() function

Discussion in 'Python' started by pinkfloydhomer@gmail.com, Dec 26, 2006.

  1. Guest

    I need a function (blocking or non-blocking) that tells me if a key has
    been pressed (even before it has been released etc.). Also, I would of
    course like to know _which_ key has been pressed.

    I know that this probably does not exist in the Python library already
    as a platform-independant abstraction (even though it probably could),
    but then I would at least like solutions that works on Windows and on
    Linux.

    /David
     
    , Dec 26, 2006
    #1
    1. Advertising

  2. robert Guest

    wrote:
    > I need a function (blocking or non-blocking) that tells me if a key has
    > been pressed (even before it has been released etc.). Also, I would of
    > course like to know _which_ key has been pressed.
    >
    > I know that this probably does not exist in the Python library already
    > as a platform-independant abstraction (even though it probably could),
    > but then I would at least like solutions that works on Windows and on
    > Linux.
    >
    > /David
    >


    Its a terminal I/O function - not a platform function. E.g. On Win only in a rough console msvcrt.kbhit() does it. In PythonWin, IPython, Crust ... things are of course different.
    On regular Unix terminals you have the sys.stdin file:

    sys.stdin.read(1) #maybe in a thread and interthread-pass it to your main loop

    or possibly trick with fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NDELAY|os.O_NONBLOCK)

    when nothing is on sys.stdin - you get immediately an IOError:

    >>> sys.stdin.read(1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    IOError: [Errno 11] Resource temporarily unavailable


    And see also other ioctl, termios, tty, cbreak, curses .. functions to get things early before \n buffering depending on the terminal mode )


    Robert
     
    robert, Dec 26, 2006
    #2
    1. Advertising

  3. At Tuesday 26/12/2006 10:25, wrote:

    >I need a function (blocking or non-blocking) that tells me if a key has
    >been pressed (even before it has been released etc.). Also, I would of
    >course like to know _which_ key has been pressed.


    On Windows you can listen to the messages WM_KEYDOWN/WM_KEYUP.
    Or, for a specific key, you can use GetKeyState/GetAsyncKeyState. For
    the whole keyboard use GetKeyboardState.


    --
    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, Dec 26, 2006
    #3
  4. > I know that this probably does not exist in the Python library already
    > as a platform-independant abstraction (even though it probably could),
    > but then I would at least like solutions that works on Windows and on
    > Linux.


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

    But it is blocking.

    --
    mvh Björn
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Dec 28, 2006
    #4
  5. Ravi Teja Guest

    wrote:
    > I need a function (blocking or non-blocking) that tells me if a key has
    > been pressed (even before it has been released etc.). Also, I would of
    > course like to know _which_ key has been pressed.
    >
    > I know that this probably does not exist in the Python library already
    > as a platform-independant abstraction (even though it probably could),
    > but then I would at least like solutions that works on Windows and on
    > Linux.


    Hmm.. 2 questions on this today. On Windows PyHook will work. It
    signals for both Key Up and Key Down events. However it is a Windows
    only module making use of very platform specific API. I am not sure if
    there is an equivalent for Linux.
     
    Ravi Teja, Dec 29, 2006
    #5
    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. vertigo

    keyPressed event problem

    vertigo, Apr 25, 2004, in forum: Java
    Replies:
    3
    Views:
    4,418
  2. William Zumwalt

    how to get KeyPressed event inside JWindow

    William Zumwalt, Dec 19, 2004, in forum: Java
    Replies:
    4
    Views:
    3,795
    Michael Dunn
    Dec 21, 2004
  3. boki
    Replies:
    1
    Views:
    6,929
  4. Boki
    Replies:
    1
    Views:
    879
    Darryl Pierce
    Apr 16, 2005
  5. Karl Ebener

    KeyPressed function?

    Karl Ebener, Nov 17, 2004, in forum: C++
    Replies:
    7
    Views:
    6,053
    E. Robert Tisdale
    Nov 17, 2004
Loading...

Share This Page