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.