3

I'm trying to create a virtual synthesizer with pygame and some other libraries. The note can be played in two ways: by clicking a button on the screen or by pressing keys on the keyboard. The keyboard input does not work while clicking on the screen does.

The following code is the Pygame loop where both the input types have been specified.

for event in pygame.event.get():

    if event.type == pygame.QUIT:
        crashed = True

    if event.type == pygame.K_ESCAPE:
        crashed = True

    if pygame.mouse.get_pos()[0] in range(0, 50) and pygame.mouse.get_pos()[1] in range(400,
                                                                                        450) and event.type == pygame.MOUSEBUTTONDOWN:
        button(sineCod, (0, 255, 0))
        wave_1 = 0  # sine

    elif pygame.mouse.get_pos()[0] in range(100, 150) and pygame.mouse.get_pos()[1] in range(400,
                                                                                             450) and event.type == pygame.MOUSEBUTTONDOWN:
        button(sawCod, (0, 255, 0))
        wave_1 = 1  # saw
    elif pygame.mouse.get_pos()[0] in range(200, 250) and pygame.mouse.get_pos()[1] in range(400,
                                                                                             450) and event.type == pygame.MOUSEBUTTONDOWN:
        button(squareCod, (0, 255, 0))
        wave_1 = 2  # saw

    elif pygame.mouse.get_pos()[0] in range(300, 350) and pygame.mouse.get_pos()[1] in range(400,
                                                                                             450) and event.type == pygame.MOUSEBUTTONDOWN:
        button(triangleCod, (0, 255, 0))
        wave_1 = 3

    elif pygame.key.get_focused() is True:
        print("Receiving Keyboard Focus")
        if event.type == pygame.locals.KEYDOWN:
            if event.key == pygame.locals.K_w:  # C
                play(frequencies[0])

            if event.key == pygame.locals.K_2:  # C#
                play(frequencies[1])

            if event.key == pygame.K_w:  # D
                play(frequencies[2])

    if (pygame.mouse.get_pos()[0] in range(40) and pygame.mouse.get_pos()[1] in range(
            190)) or (pygame.mouse.get_pos()[0] in range(75) and pygame.mouse.get_pos()[1] in range(190,
                                                                                                    320)) and event.type == pygame.MOUSEBUTTONDOWN:  # C
        play(frequencies[0])

    elif pygame.mouse.get_pos()[0] in range(75, 150) and pygame.mouse.get_pos()[1] in range(
            320) and event.type == pygame.MOUSEBUTTONDOWN:  # D
        play(frequencies[2])

    elif pygame.mouse.get_pos()[0] in range(150, 225) and pygame.mouse.get_pos()[1] in range(
            320) and event.type == pygame.MOUSEBUTTONDOWN:  # E
        play(frequencies[4])

1 Answers1

1

event.type does not specify the key which is pressed. That is done by event.key

Instead of this:

if event.type == pygame.K_ESCAPE:

You should do this:

if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:

Or

pressed = pygame.key.get_pressed()
if pressed[pygame.K_ESCAPE]:
RohithS98
  • 500
  • 2
  • 14