vamosrafa
vamosrafa

Reputation: 695

Python getch() - Multiple character

I am aware that python getch() is suitable for detecting single keystrokes.

Is there any method, that I can use the same function for detecting multiple keystrokes.s,

Also, is it possible that the program can be made to wait before it prints out the output.

e.g.: When I press 'w', the program must wait for another keystroke, 'a', before it prints the output for 'w'. I know this is workaround, but I think, as of now, this should do.

Sample code:

try:
    from msvcrt import getch
    print "I am Here"
except ImportError:
    print "Hi"
    def getch():
    print "I am here!"
        import sys, tty, termios            
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)

        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
def getchs():
    while True:
        yield getch()
for choice in getchs():  
        if choice == 'w':
            print (80 * '-')
            print ("You have chosen Orange...")
            print ("Here's the nutritional fact of the Orange:")
            print ("'One medium orange contains 1.23 grams of protein, 62 calories and 3.1 grams of dietary fiber.'")
            print (80 * '-')
        elif choice == 'a':
            print (80 * '-')
            print ("You have chosen Banana...")
            print ("Here's the nutritional fact of the Banana:")
            print ( "'One medium banana contains 1.29 grams of protein, 105 calories and 3.1 grams of dietary fiber")
            print (80 * '-')

Now this works perfect to detect 'w' and 'a'

How shall I incorporate the feature to have combination: 'wa', using the getch(), not raw_input

I have searched for this, could not find.

Also, will curses module help to achieve this?

Upvotes: 0

Views: 396

Answers (1)

MA1
MA1

Reputation: 2847

pykeylogger might help you. As per pykeylogger docs

It is currently available for Windows (NT/2000 and up), and Linux (using Xlib, so won't work on the console).

For windows only check keyboard hooks in pyhook

Upvotes: 2

Related Questions