3

I'm using Python 3.5 and I want to make multi-keystroke function. I want to make a function that notices Ctrl+Q but my program didn't notice it.

Here's my code:

import threading, pygame
from pygame.locals import *
from time import sleep

pygame.init()
screen = pygame.display.set_mode((1160, 640), 0, 0)
screen.fill((255, 255, 255))

pygame.display.flip()

def background():
    number = 0
    while True:
        if number < 10:
            number = number + 1
            print(number)
            sleep(1)
        else:
            print("10 seconds are over!")
            break

def foreground():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.key.get_mods() & pygame.KMOD_CTRL and pygame.K_q:
                    print('HELLO_WORLD')


b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()

I also changed

def foreground():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.key.get_mods() & pygame.KMOD_CTRL and pygame.K_q:
                    print('HELLO_WORLD')

to

def foreground():
    while True:
        key = pygame.key.get_pressed()
        if key[pygame.key.get_mods() & pygame.KMOD_CTRL and pygame.K_q]:
            print('HELLO_WORLD')

but it didn't notice Ctrl+Q.

How can I make it?

2
  • While the question could become interesting you're not giving a lot of relevant info. ie: global keystrokes or widget's keystrokes? any specific platform or platform independent? ... But the most important, how-to-ask. Commented Jun 16, 2018 at 10:20
  • @BPL I edited my question. Commented Jun 16, 2018 at 10:41

1 Answer 1

2

Here's a possible fix for your code:

import threading
import pygame
from pygame.locals import *
from time import sleep
import sys

pygame.init()
screen = pygame.display.set_mode((1160, 640), 0, 0)
screen.fill((255, 255, 255))
pygame.display.flip()


def background():
    number = 0
    while True:
        if number < 10:
            number = number + 1
            print(number)
            sleep(1)
        else:
            print("10 seconds are over!")
            break


def foreground():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if pygame.key.get_mods() & pygame.KMOD_CTRL and event.key == pygame.K_q:
                    print('HELLO_WORLD')

            pygame.display.update()


b = threading.Thread(name='background', target=background)
b.start()
foreground()
Sign up to request clarification or add additional context in comments.

17 Comments

Doesn't fix the bug that's causing the issue. The problem is in the if statement that checks which key was pressed.
@MikeyB What do you mean? I've tested this thing before posting my answer and it was working, here
Oh, wait, I didn't see your change to the IF statement. I see two conditions there now. The bug was that one line of code. I'll take down my answer.
@BPL I edited my question. Could you answer my another question?
@HoseongJeon Sure, I can give it a shot. But make sure to put the new content in a new thread, that's how StackOverflow goes. 1 question per thread
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.