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 there is a module called msvcrt, which has a getch function, so I am wondering if there is a way to simulate this in OSX, or more simply to just constantly input data from the keyboard.
Asked
Active
Viewed 8,292 times
1
-
Duplicate of [raw_input in python without pressing enter](http://stackoverflow.com/questions/3523174/raw-input-in-python-without-pressing-enter) – Lennart Regebro Jan 04 '12 at 09:59
-
No, it isn't. The accepted answer to that question is blocking, i.e. it will *wait* for a key press. If he used that solution in his game, everything on the screen would freeze while it waited for the user to press a key. – Lambda Fairy Jan 04 '12 at 20:39
-
It is more likely a duplicate of this: [Polling the keyboard in Python](http://stackoverflow.com/questions/292095/polling-the-keyboard-in-python). I can't reflag the question, unfortunately. – Lambda Fairy Jan 04 '12 at 20:51
2 Answers
1
Try the curses
library:
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)

Lambda Fairy
- 13,814
- 7
- 42
- 68
-
Out of curiosity, what graphics library are you using? If you're using something that's designed for writing games, it should have some kind of keyboard system built in. By the way, if you want graphics and interactivity, pygame (http://pygame.org/) is great. – Lambda Fairy Jan 03 '12 at 21:46
-
It is a graphics library that we used in my class at school. It's not very good for using this game, so I should look into using pygame. Here's a link the the module though. http://mcsp.wartburg.edu/zelle/python/graphics/graphics/index.html – fahrbach Jan 04 '12 at 16:44
0
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)

Biggyboiii
- 5
- 4

LeSgHsW
- 1
-
I'm sorry if the comment is somewhat messed up, but it's the first time that I actually write something on stackoverflow – LeSgHsW Jul 24 '19 at 22:07
-
1Please learn to format your questions, specifically code. You can use triple backticks or an indented block to format code. There are also handy helper buttons :) – watzon Jul 24 '19 at 22:20