After a lot of work at SO, I still have not found a good answer to what I hope is a fairly common need. Basically, I need the main thread to make the “material” until it receives an input, and then acts on this input, and then returns to the original “material”. My problem every time it seems that my program execution seems to stop completely at the original input, I call it in a thread or elsewhere. Warning I am new to python, but I hope this should not be too nasty to implement. Here is what I'm playing with (pulled from my other question where my threaded question was satisfied)
So, I'm trying to write a program that searches for keystrokes, and then does something in the main program, based on what the user enters. I am trying to start listening to the keyboard in a stream, and then compare something in a variable in my main loop, but I'm never going to enter keyboard input. In the code below, the print line can never be updated, but only the else block from the main while loop. What do I need to do so that my main loop knows about the keystrokes pressed by the user?
import threading
import time
kbdInput = ''
playingID = ''
def kbdListener():
global kbdInput
kbdInput = rawInput()
print "maybe updating...the kbdInput variable is: ",kbdInput
listener = threading.Thread(target=kbdListener)
while True:
print "kbdInput: ",kbdInput
print "playingID: ",playingID
if playingID != kbdInput:
print "Recieved new keyboard Input. Setting playing ID to keyboard input value"
playingID = kbdInput
else:
print "No input from keyboard detected. Sleeping 2 seconds"
time.sleep(2)
source
share