My friend challenged me to make a small program that allows the user to press a button on a Xbox controller and have it do multiple functions on the keyboard (like reWASD). I thought I almost made it to the end and found the having keyboard.press() and keyboard.release() wouldn't do its function. Is there any possible way to make it do what I hoped it to do. Sorry for the bad English I am freshmen and its 1 am and I am new to stakoverflow formatting.
Here is my code.
import keyboard # using module keyboard
import pygame
import threading
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()
BLACK = pygame.Color('black')
WHITE = pygame.Color('white')
stall = 0
def stall1():
global stall
while stall == 1:
keyboard.press('a')
keyboard.release('a')
stall = 0
# This is a simple class that will help us print to the screen.
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint(object):
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)
def tprint(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, (self.x, self.y))
self.y += self.line_height
def reset(self):
self.x = 10
self.y = 10
self.line_height = 15
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
screen = pygame.display.set_mode((500, 700))
pygame.display.set_caption("My Game")
textPrint = TextPrint()
while True: # making a loo
t = threading.Thread(target=stall1())
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks.
joystick_count = pygame.joystick.get_count()
textPrint.tprint(screen, "Number of joysticks: {}".format(joystick_count))
events = pygame.event.get()
for event in events:
if event.type == pygame.JOYBUTTONDOWN:
print("Button Pressed")
if joystick.get_button(0):
stall = 1
# Control Left Motor using L2
elif joystick.get_button(2):
# Control Right Motor using R2
print('yote')
elif event.type == pygame.JOYBUTTONUP:
print("Button Released")
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
# Get the name from the OS for the controller/joystick.
name = joystick.get_name()
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
pygame.display.flip()
# Limit to 20 frames per second.
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
Just so I can try to explain simply. I want press the a button and have it type something. Using threading, pygame, and keyboard. Sorry, im new to coding and formatting on stackoverflow.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With