The statement
t = threading.Thread(target=stall1())
does not do what you expect it to do, because stall1()
is a call to the function stall1
. That means the function will be called immediately in the main thread and the return value of the function will be passed to the keyword argument target
(None
in this case).
You have to pass the function object (stall1
) to the argument:
t = threading.Thread(target=stall1)
Change the function stall1
, so that it runs as long a state thread_running
is set:
stall = 0
thread_running = True
def stall1():
global stall
while thread_running:
if stall == 1:
stall = 0
keyboard.press('a')
keyboard.release('a')
Start the thread and initialize the joystick before the main application loop:
t = threading.Thread(target=stall1)
t.start()
joystick_count = pygame.joystick.get_count()
if joystick_count > 0:
joystick = pygame.joystick.Joystick(0)
joystick.init()
run = True
while run:
screen.fill(WHITE)
textPrint.reset()
textPrint.tprint(screen, "Number of joysticks: {}".format(joystick_count))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
thread_running = False
run = False
if event.type == pygame.KEYDOWN:
print(chr(event.key)) # print key (triggered from 'stall1')
if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
if joystick.get_button(0):
stall = 1
elif joystick.get_button(2):
print('yote')
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")
pygame.display.flip()
clock.tick(20)