5

While I was working on a pygame project, I noticed that when I change screen's luminosity the window was shutting down. The only key that is suppose to do that in my code is the "escape" key :

for evt in pg.event.get():
    if evt.type == pg.KEYDOWN:
        if evt.key == pg.K_ESCAPE:
            exit()

This works without any problem when I press escape.

The problems comes from the fact that changing luminosity acts exactly like pressing the "escape" key. I've tried to print both events and no matter if I press "escape" or if I change luminosity, the output is exactly the same :

<Event(768-KeyDown {'unicode': '\x1b', 'key': 27, 'mod': 4096, 'scancode': 41, 'window': None})>

(This is also the case when I press the "mute microphone" key for example, but for some reason, the "volume up/down" keys are not concerned by this behavior)

Is there a way to differentiate between those keys with events that look exactly the same ? I want my window to close only when I press escape, not when I change luminosity.

You can recreate this situation with the most basic pygame setup :

# Imports
import pygame as pg
import sys
    
# Init
pg.init()

# Display
WIDTH, HEIGHT = 500, 500
screen = pg.display.set_mode((WIDTH, HEIGHT))
    
# Functions
def handle_input():
    for evt in pg.event.get():
        if evt.type == pg.QUIT:
            exit()
        if evt.type == pg.KEYDOWN:
            print(evt)
            if evt.key == pg.K_ESCAPE:
                exit()

def exit():
    pg.quit()
    sys.exit()

# Main loop
if __name__ == "__main__":
    while True:
        handle_input()

Then try to press all your function keys one by one while the window is showing (and in focus) and let me know if you observe the same thing.

Ps: Someone suggested, this question was answered here. It is not the case at all. My question is not about how to get inputs with pygame. I obviously already know how to do that. This question was not answered, so it should not be closed.

Anto
  • 267
  • 1
  • 11
  • 1
    For me, some of the function key presses don't make it to the pygame event handler, e.g. mute microphone, turn on keyboard backlight, increase / decrease brightness. Pressing fn+Esc for me toggles Function lock, but again pygame doesn't see that. What operating system and pygame versions are you using? You can also get a human readable version of the key pressed with `py.key.name(evt.key)` – import random Aug 23 '22 at 03:23
  • @importrandom That's suprising, so it might come from my setup rather that pygame itself ? I've tried `print(pg.key.name(evt.key))` and when I press luminosity up/down keys, output is : "escape" so it really does detect most of my function keys as if I pressed "escape". I am on windows for this specific project and my pygame version is 2.1.2. – Anto Aug 23 '22 at 06:16
  • 1
    It might be a result of the keyboard driver, on my laptop F5 has a function to toggle the keyboard backlight. This keypress doesn't trigger the pygame event. On my main keyboard F5 has a Search function. If I press it the Windows search dialogue pops up, but I still see the `AC Search` keydown event. You could change your event handling to require a modifier, such as Shift + Esc. Here's an [example](https://stackoverflow.com/a/72877990/2280890) – import random Aug 23 '22 at 07:27
  • @importrandom I guess that could be a solution but it doesn't feel right, espically since I don't use "escape" only to close the app. In my app I have elements you can click on, and when you do so : a surface pops on top and let's you configure the element you clicked. Background also gets grayed out while you are configuring the element. When a configuration surface is showing the escape key simply hides this surface and the application goes back to normal instead of shutting down. I found this is a very intuitive way to close those configuration surfaces and I'd like to keep it like that – Anto Aug 23 '22 at 08:25
  • Sounds like you don't want the Escape key to ever quit your program, especially if an impatient user presses Escape multiple times to close a dialogue.Tthere's no specific reason it should. Relying on closing the window should be fine (`QUIT` event). – import random Aug 24 '22 at 06:34
  • 2
    I get your point, this is just a habit. I always use "escape" to quit in all my pygame projects for me it's way more convenient. But we are moving towards a user experience question, that wasn't the point of my original post. I just wanted to know if there was a way to differentiate the "escape" key and my other function keys that send the same event as "escape" If not, I'm sure I'll find a work around, that is not a problem. Thanks for your help anyway ! – Anto Aug 24 '22 at 06:44

1 Answers1

0

Excerpt and summary from the comments:

For me, some of the function keys don't make it into the Pygame event handler, e.g. mute microphone, turn on keyboard backlight, increase/decrease brightness. When I press fn+Esc I toggle the function lock, but pygame doesn't see that either.
It could be the keyboard driver, on my laptop F5 has a function to toggle the keyboard backlight. This keystroke does not trigger the ?ygame event. On my main keyboard, F5 has a search function. When I press it, the Windows search dialog opens, but I still see the AC search event. You could change your event handling to require a modifier, such as Shift + Esc.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174