Detecting Key Strokes?

This is the place for queries that don't fit in any of the other categories.

Detecting Key Strokes?

Postby murster972 » Fri Aug 30, 2013 8:22 am

if i had an if statement, how would i put it so that if a certain key was pressed it would perform a certain action??
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Detecting Key Strokes?

Postby Mekire » Fri Aug 30, 2013 8:41 am

Seems like far too general a question. What kind of program are you writing? What gui/api are you using? What is your goal? What is the meaning of life?

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Detecting Key Strokes?

Postby murster972 » Fri Aug 30, 2013 8:51 am

just in general, for example if i was writing a text based game, and the user pressed the left arrow key, he would go left. How would i do that?
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Detecting Key Strokes?

Postby Mekire » Fri Aug 30, 2013 9:20 am

If you are using Linux you could use the curses module (not compatible with windows). If you are using windows and python 2.7 you could try pyHook.

I have only experimented minimally with either.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Detecting Key Strokes?

Postby murster972 » Fri Aug 30, 2013 9:21 am

Thank you ill try that :)
murster972
 
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: Detecting Key Strokes?

Postby metulburr » Fri Aug 30, 2013 9:22 am

GUI libraries make it a breaze to handle. It depends on what Operating System your on. For example, in linux you can easily use NCurses to do this. Or depending on your OS too, you can use termios or msvcrt

an example of either or:
Code: Select all
import sys

if sys.platform[:3] == 'win':
    import msvcrt
    def getkey():
        return msvcrt.getch()
elif sys.platform[:3] == 'lin':
    import termios, sys, os
    TERMIOS = termios

    def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
            c = os.read(fd, 1)
        finally:
            termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

while True:
    k = getkey().decode()
    print('you pressed: {}'.format(k))


and then if using linux, a way to grab arrow key input upon keypress:
Code: Select all
import sys
import termios
import contextlib


@contextlib.contextmanager
def raw_mode(file):
    old_attrs = termios.tcgetattr(file.fileno())
    new_attrs = old_attrs[:]
    new_attrs[3] = new_attrs[3] & ~(termios.ECHO | termios.ICANON)
    try:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, new_attrs)
        yield
    finally:
        termios.tcsetattr(file.fileno(), termios.TCSADRAIN, old_attrs)


def main():
    print('exit with ^C or ^D')
    with raw_mode(sys.stdin):
        try:
            while True:
                ch = sys.stdin.read(1)
                print('ch is: {}'.format(ord(ch)))
                if not ch or ch == chr(4):
                    print('breaking')
                    break
                print('%02x' % ord(ch))
        except (KeyboardInterrupt, EOFError):
            pass


if __name__ == '__main__':
    main()


I have hardly played around with these, as i would just use a GUI library to make it simple.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: micseydel and 4 guests