You are not logged in.

#1 2005-08-01 17:47:43

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

python - listening to keyboard events

does anyone how to write a simple program in python that listens to keyboard events and then prints them back to the user from a command line?

Im not looking for code that requires a return key, just somethign that  prints out a button when it is pressed.

any help is greatly appreciated,

Thanks.

Offline

#2 2005-08-01 17:57:28

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python - listening to keyboard events

I think you're looking for GNU readline... here's a hacked version which compiles into apython module:
http://www.eblong.com/zarf/zymb/readline.c

Offline

#3 2005-08-01 18:16:20

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: python - listening to keyboard events

wow, i didnt think it would be this complicated.

my intention is to use it in a state machine.

Im writing a quiz that runs from the command line, i would like to change the verbosity by using the arrow keys on the keyboard (up and down).

and i would like to cycle forward and back through the questions by using the forward and back arrows.

would you still suggest using the readline.c module for this?

Offline

#4 2005-08-01 18:24:30

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: python - listening to keyboard events

you could alway rely on pygame, but i guess it would be a little bit to bloated for your usage.

Offline

#5 2005-08-01 18:39:57

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: python - listening to keyboard events

thanks cmp,  that could work using something like this -

for event in pygame.event.get():
     ...

but im curious if there are any other ways

Offline

#6 2005-08-01 20:23:28

juergen
Developer
From: Frankfurt/Germany
Registered: 2005-04-11
Posts: 48
Website

Re: python - listening to keyboard events

You can put your terminal in noncanonical mode.

import termios, sys, os

def getkey():
    term = open("/dev/tty", "r")
    fd = term.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] &= ~termios.ICANON & ~termios.ECHO
    termios.tcsetattr(fd, termios.TCSANOW, new)
    c = None
    try:
        c = os.read(fd, 1)
    finally:
        termios.tcsetattr(fd, termios.TCSAFLUSH, old)
        term.close()
    return c

if __name__ == '__main__':
        print 'type something'
        s = ''
        while 1:
                c = getkey()
                if c == 'n':
                        break
                print 'got', c
                s = s + c

        print s

Offline

#7 2005-08-01 20:54:02

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: python - listening to keyboard events

juergen wrote:

You can put your terminal in noncanonical mode.

Nice! I didn't even know python had a terminal io module... good call

Offline

#8 2005-08-03 16:56:13

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: python - listening to keyboard events

Thanks for the suggestion juergen,

It works really well for any event a->z, 0->9 and a few special characters, but it doesnt seem to work well with arrow keys. I checked abunch of docs on the module, but couldnt find anything

Any suggestions to get this to work with arrow keys?

Offline

#9 2005-08-03 22:40:44

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: python - listening to keyboard events

try the curses module?

Offline

#10 2005-08-04 00:34:49

giddygiddyBA
Member
From: Montreal
Registered: 2004-12-30
Posts: 66

Re: python - listening to keyboard events

considered using the curses module, in all honesty, if i was to rewrite the code i would do it in curses.

the non-canonical mode works really well, all i need is for it to pick up arrow keys

Offline

#11 2005-08-04 09:09:50

juergen
Developer
From: Frankfurt/Germany
Registered: 2005-04-11
Posts: 48
Website

Re: python - listening to keyboard events

giddygiddyBA wrote:

the non-canonical mode works really well, all i need is for it to pick up arrow keys

To make your code working on different terminal types this would require parsing the terminfo database. Better go with curses before reinventing the wheel.

Offline

Board footer

Powered by FluxBB