简体   繁体   English

透明 window 失去焦点使键盘事件不起作用

[英]Transparent window losing focus makes keyboard events not working

I have a transparent window. If you press m it turns translucent and if you press esc turns transparent again.我有一个透明的 window。如果你按m它会变成半透明的,如果你按esc会再次变成透明的。 The thing is that this mechanism works if you haven't clicked the mouse.问题是,如果您没有单击鼠标,此机制就会起作用。 Once you click it, this mechanism stops working.单击它后,此机制将停止工作。 My intuition is that when i mouse click, the window loses focus and stops reading events.我的直觉是,当我单击鼠标时,window 失去焦点并停止读取事件。 I don't know how to make it to ignore mouse clicks and keep reading events.我不知道如何让它忽略鼠标点击并继续阅读事件。

Should I use maybe a keyboard/mouse listener?我应该使用键盘/鼠标侦听器吗? (not familiar with that) (不熟悉那个)

pygame.init()

info = pygame.display.Info()
w = info.current_w
h = info.current_h
screen = pygame.display.set_mode((w, h), pygame.NOFRAME) # For borderless, use pygame.NOFRAME
done = False
fuchsia = (255, 0, 128)  # Transparency color
dark_red = (255, 0, 0)

# Create layered window
hwnd = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
                       win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
# NOTE: Transparent
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY) 
# NOTE: Translucent
#win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 50, win32con.LWA_ALPHA)


transparent = True
clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_q:
                done = True
            if event.key == pygame.K_ESCAPE:
                # NOTE: Turn screen transparent
                transparent = True
                fuchsia = (255, 0, 128)
                win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 0, win32con.LWA_COLORKEY) # NOTE: Transparent
            if event.key == pygame.K_m:
                # NOTE: Turn screen translucent
                transparent = False
                win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*fuchsia), 65, win32con.LWA_ALPHA) # NOTE: Translucent
                show_text()

    if transparent:
        screen.fill(fuchsia)  # Transparent background
    else:
        screen.fill((255, 255, 255))  # Translucent background

    pygame.display.update()
    clock.tick(60)


pygame.quit()

Keyboard input goes to the foreground window. When you click "on" a transparent window, the mouse click goes right through that window, and makes the window underneath the foreground window (which will now receive keyboard input).键盘输入进入前台 window。当您单击“透明”window 时,鼠标单击会直接穿过该 window,并使 window 位于前景 window 下方(现在将接收键盘输入)。 If you need to observe keyboard input regardless of whether your window is the foreground window, you'll need to use lower-level input processing infrastructure (like Raw Input ).如果您需要观察键盘输入,而不管您的 window 是否是前台 window,您将需要使用较低级别的输入处理基础设施(如Raw Input )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM