5

Is there anyway to use pygame to get input from the console, rather than having to display a separate window to get input? I'm using pygame to track how long the keys on the keyboard are pressed.

The following code doesn't work (this is just a minimal example, it doesn't actually keep track of time elapsed):

pygame.init()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            print event.key, 'pressed'

It doesn't look like any pygame event is being raised. If I add

screen = pygame.display.set_mode((640, 480))

After

pygame.init()

then the event is raised, but I have that ghastly window I don't want to deal with.

To explain why I don't want the window, I envision this application being a command-line utility, so I can't have that. Is there any functional reason preventing pygame from running on the command-line?

Thanks!

EDIT: I speculated that the problem was pygame.init(), and that I only needed to initialize the key and event modules. According to http://www.pygame.org/docs/tut/ImportInit.html I should have been able to call

pygame.key.init()
pygame.event.init()
but it didn't work.
Sam Cantrell
  • 585
  • 6
  • 19

5 Answers5

2

Pygame is designed for making (graphical) games, so it only captures key presses when there is a window displayed. As Ignacio said in his answer, reading from the command line and from another window are very different things.

If you want to create a command line application, try curses:

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

Unfortunately, it only works on Linux and Mac OS X.

Lambda Fairy
  • 13,814
  • 7
  • 42
  • 68
  • But according to [http://stackoverflow.com/questions/4190544/detect-automatic-key-repeats-in-curses](http://stackoverflow.com/questions/4190544/detect-automatic-key-repeats-in-curses), I can't detect if a key is being held down or not... – Sam Cantrell Mar 22 '12 at 15:32
  • @Sam Isn't that a separate problem? Why do you need that anyway? – Mizipzor Mar 23 '12 at 10:27
  • @mizipzor I was trying to design a program to quantify input from the keyboard into notes and lengths for Lilypond. Thus, I need to know how long the user is holding down the key so that I can determine which note length should be assigned. – Sam Cantrell Mar 24 '12 at 15:16
1

If you just make the window really small using

screen = pygame.display.set_mode((1, 1))

you can't see it. So you are clicked into the window but you don't notice.

If you click anywhere of course it stops working. You have to click on the pygame window icon for it to work again.

Kevin
  • 11
  • 1
1

Console input comes in via stdin, which pygame is not prepared to handle. There is no reliable way to get press/release events via stdin since it's dependent on the terminal sending it keypresses.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Try pygame.display.iconify(). This will hide the pygame screen, and you will still be able to detect keypresses.

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
0

If you just plain don't want a window of any kind at all, you can use PyHook. If you just want a console application, get user input with the built-in Python command "raw_input(...)".

geometrian
  • 14,775
  • 10
  • 56
  • 132