Python 3 - How to input data without pressing in OSX

前端 未结 2 1299
半阙折子戏
半阙折子戏 2020-12-18 16:28

I am attempting to move a character using the \"asdw\" keys in a game, but I cannot find a way to constantly input data without pressing return. I have seen that on windows

相关标签:
2条回答
  • 2020-12-18 16:42

    Try the curses library:

    http://docs.python.org/py3k/library/curses.html

    Curses is a library for controlling the terminal, and includes features such as drawing box shapes as well. It's available on any POSIX-compatible system, which includes Mac OS X and GNU/Linux.

    Here's an example:

    import curses
    import time
    
    # Turn off line buffering
    curses.cbreak()
    
    # Initialize the terminal
    win = curses.initscr()
    
    # Make getch() non-blocking
    win.nodelay(True)
    
    while True:
        key = win.getch()
        if key != -1:
            print('Pressed key', key)
        time.sleep(0.01)
    
    0 讨论(0)
  • 2020-12-18 17:02

    You can use Turtle to do something like this:

    import turtle
    Sc = turtle.Screen()
    Sc.setup(width=0, height=0)         #this hides turtle's windows
    def a():                            #that's the function that you want to run when the key is pressed
        #code here
        Sc.listen()                         #this tells the program to listen 
        for a keypress
        Sc.onkey("#The key here", #the function call here) #this tells the program
    

    What function to call when a certain key is pressed

    # An example of pressing the key "w"
    Sc.onkey("w", a)
    
    0 讨论(0)
提交回复
热议问题