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?

BPL
  • 9,632
  • 9
  • 59
  • 117
Hoseong Jeon
  • 1,240
  • 2
  • 12
  • 20
  • 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](https://stackoverflow.com/help/how-to-ask). – BPL Jun 16 '18 at 10:20
  • @BPL I edited my question. – Hoseong Jeon Jun 16 '18 at 10:41

1 Answers1

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()
BPL
  • 9,632
  • 9
  • 59
  • 117
  • Doesn't fix the bug that's causing the issue. The problem is in the if statement that checks which key was pressed. – Mike from PSG Jun 16 '18 at 14:15
  • @MikeyB What do you mean? I've tested this thing before posting my answer and it was working, [here](https://dl.dropboxusercontent.com/s/hjjpok2ijjx69gt/2018-06-16_16-25-14.mp4) – BPL Jun 16 '18 at 14:26
  • 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. – Mike from PSG Jun 16 '18 at 14:51
  • @BPL I edited my question. Could you answer my another question? – Hoseong Jeon Jun 24 '18 at 04:37
  • @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 – BPL Jun 24 '18 at 11:32
  • @HoseongJeon I've got a possible way to go about that problem, when you've created the new question on the new thread just let me know. Thanks – BPL Jun 24 '18 at 11:55
  • @BPL What do you mean the new thread? What do you want me to do? – Hoseong Jeon Jun 27 '18 at 09:23
  • @HoseongJeon Take a look this [one](https://physics.meta.stackexchange.com/a/19)... Usually when a question has been solved already and you've got a new one (a follow up like this one for instance) that requires new code, new comments, new validation... The right thing to do is opening a new question ("Ask the question" button) – BPL Jun 27 '18 at 09:58
  • @BPL Oh, Okay. How can I tag you when I right a new question? – Hoseong Jeon Jun 27 '18 at 10:03
  • @HoseongJeon No worries, use the same tags than the used on this question and I'll check it out. Or just left a comment here when it's done. Also, I'll edit your current question to make sure it matches with the validated answer – BPL Jun 27 '18 at 10:14
  • @BPL Oh.. I asked question a few minutes before you answer me, so I have to wait 90 minutes. I think I can post my question around 1 hour later.. – Hoseong Jeon Jun 27 '18 at 10:15
  • @BPL I always thank you very much about your kindness – Hoseong Jeon Jun 27 '18 at 10:16
  • @HoseongJeon No problem, at the very beginning when started using Stackoverflow I used to have the same mistakes than you, ie: asking multiple questions in one thread. It takes time learn all these little Stackoverflow rules... even now I still try to improve my "how to ask good question" skills, which it's not always easy ;) . You can take a look to the links [here](https://stackoverflow.com/help/how-to-ask), they're quite helpful ones. Remember, good questions will tipically produce good answers. – BPL Jun 27 '18 at 10:22
  • @BPL Thank you so much. I think I have to read the link you recommended :). – Hoseong Jeon Jun 27 '18 at 10:24
  • @BPL I think I have to wait almost 40 minutes or so. I'll tell you when I write a new question. Thanks in advance :). – Hoseong Jeon Jun 27 '18 at 11:00
  • @BPL I'm sorry I'm late. I asked new question. – Hoseong Jeon Jun 27 '18 at 12:42
  • @HoseongJeon Ok, good, already answered, didn't know there was a limitation between asked questions when having low-rep – BPL Jun 27 '18 at 13:23