-1

I want to be able to press any key to perform some sort of logic while other code is running. I don't know if parallel programming is required. I don't think it has to be. Basically I want to be able to perform something along the lines of this type of functionality

ex:

Welcome to questionnaire test (press 'j' to quit):

what is your fav color? # input answer
what is your fav number?
where are you from?
how do you do this?
press 'j'
#LOGIC

Pretty much I would be able to press a certain key at any moment to perform some sort of logic

I'm not sure where to look or how to implement this in python.

Liondancer
  • 15,721
  • 51
  • 149
  • 255

1 Answers1

1

Using pygame you can check for input every time around the main game loop:

while True:
    elapsed = clock.tick(FRAMERATE)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_j:
                # your logic goes here 

This will check for input many times a second, so will seem instant to the user.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • no that was merely and example. I want something like this. imagine youre playing a game and you press start to pause. Youre able to do so at anytime. I want to be able to do something like this – Liondancer Apr 15 '14 at 21:11
  • or maybe your logic is what i need but i am just not sure how to implement it – Liondancer Apr 15 '14 at 21:12
  • ex: if 'j' is ever pressed anywhere while running the program, do logic – Liondancer Apr 15 '14 at 21:14
  • hmm would u know of something else that doesn't require pygame? – Liondancer Apr 15 '14 at 21:22
  • What do you want, the moon on a stick? – jonrsharpe Apr 15 '14 at 21:25
  • something in teh standard library? – Liondancer Apr 15 '14 at 21:25
  • As I see it, you have two broad choices: some main loop you can keep checking in (like I've posted) or something ~parallel that can interrupt. I am not aware of any built-in "j pressed" module. – jonrsharpe Apr 15 '14 at 21:29
  • I think I might have foudn something. It seems as if pygame is the easiest way. http://stackoverflow.com/questions/13207678/whats-the-simplest-way-of-detecting-keyboard-input-in-python-from-the-terminal – Liondancer Apr 15 '14 at 21:41