It is not possible to implement the demonstration window by hotkeys. I want to achieve the following: the window was minimized; by pressing the hot keys, the window changes to the show state.

I use PyQT and the keyboard library to intercept the keyboard. In a separate thread, I expect a keystroke (in this case, esc), and then show the window.

The window freezes and crashes. Perhaps because I work with a GUI from another thread. How can I solve the problem?

import sys from PyQt5.QtWidgets import QWidget, QApplication, QSystemTrayIcon, QStyle import keyboard import threading class ScreenArea(QWidget): def __init__(self): super().__init__() self.setMinimumSize(500, 500) # УстанавливаСм Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ #self.show() def show(w): while True: keyboard.wait('esc') w.show() app = QApplication(sys.argv) w = ScreenArea() threading.Thread(target=show, args=(w,)).start() sys.exit(app.exec_()) 

    1 answer 1

    I don't have a keyboard library, but I have a pynput library, which is designed for the same purposes.

    The example below is working, press the esc and space keys. How much is it thread safe? I think no.

    I added processing of some events: the symbol c and C ; the alt_l key, as well as the key combination ( Alt_l+c , Alt_l+C , Alt_r+c , Alt_r+C ). Try it.

     import sys from PyQt5.QtWidgets import QWidget, QApplication, QSystemTrayIcon, QStyle #import keyboard from pynput import keyboard # pip install pynput import threading class ScreenArea(QWidget): def __init__(self): super().__init__() self.setMinimumSize(500, 500) self.show() def show(w): # ΠšΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ ΠΊΠΎΠΌΠ±ΠΈΠ½Π°Ρ†ΠΈΠΈ для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ COMBINATIONS = [ {keyboard.Key.alt_l, keyboard.KeyCode(char='c')}, # Alt_l + c {keyboard.Key.alt_l, keyboard.KeyCode(char='C')}, # Alt_l + C {keyboard.Key.alt_r, keyboard.KeyCode(char='c')}, # Alt_r + c {keyboard.Key.alt_r, keyboard.KeyCode(char='C')} # Alt_r + C ] # # Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹ΠΉ ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ current = set() def executeAltC(): print ("\n *** НаТата комбинация клавиш: Alt+C\n *** Π‘Π΄Π΅Π»Π°ΠΉΡ‚Π΅ Ρ‡Ρ‚ΠΎ-Π½ΠΈΠ±ΡƒΠ΄ΡŒ :)") def get_key_name(key): """ Π€ΡƒΠ½ΠΊΡ†ΠΈΡŽ get_key_name(), Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΡΡ‚ΡŒ Ρ‚ΠΈΠΏ ΠΊΠ»ΡŽΡ‡Π°. Для Π½ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΊΠ»ΡŽΡ‡Π° ΠΎΠ½ Π²Π΅Ρ€Π½Π΅Ρ‚ key.char, Ρƒ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… ΠΎΠ΄ΠΈΠ½Π°Ρ€Π½Ρ‹Ρ… ΠΊΠ°Π²Ρ‹Ρ‡Π΅ΠΊ. Для Π΄Ρ€ΡƒΠ³ΠΈΡ… ΠΊΠ»ΡŽΡ‡Π΅ΠΉ ΠΎΠ½ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ имя ΠΊΠ»ΡŽΡ‡Π° (Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, Key.cmd, Key.alt, Key.ctrl ...) """ if isinstance(key, keyboard.KeyCode): return key.char else: return str(key) def on_press(key): key_name = get_key_name(key) if key == keyboard.Key.esc: print('--- НаТата клавиша: {}'.format(key_name)) w.hide() elif key == keyboard.Key.space: print('--- НаТата клавиша: {}'.format(key_name)) w.show() elif key == keyboard.Key.alt_l: print('--- НаТата клавиша: {}'.format(key_name)) # KeyCode события наТатия символа elif (key == keyboard.KeyCode.from_char('c')) or \ (key == keyboard.KeyCode.from_char('C')): print('--- НаТата клавиша: {}'.format(key_name)) else: print('Π˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΎΠ½Π½ΠΎ. Π’Ρ‹ Π½Π°ΠΆΠ°Π»ΠΈ: {}'.format(key_name)) current.clear() # Клавиши-ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€Ρ‹. Alt_l+c, Alt_l+C, Alt_r+c, Alt_r+C # any(iterable) -> bool. ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅Ρ‚, Π΅ΡΡ‚ΡŒ Π»ΠΈ срСди ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹Ρ… элСмСнтов # хотя Π±Ρ‹ ΠΎΠ΄ΠΈΠ½, ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°ΡŽΡ‰ΠΈΠΉ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ `истина` if any([key in COMBO for COMBO in COMBINATIONS]): current.add(key) if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS): executeAltC() current.clear() with keyboard.Listener(on_press=on_press) as listener: listener.join() app = QApplication(sys.argv) w = ScreenArea() threading.Thread(target=show, args=(w, ), daemon=True).start() # + daemon=True sys.exit(app.exec_()) 

    enter image description here

    • Thank! It's not strange, I did the same not the keyboard and it does not work. Using the library doesn’t matter to me) I don’t really understand how to catch the events of pressing a character on the keyboard? Let's say I'm trying to catch the alt + c combination. Alt I catch without problems (keyboard.Key.alt), but how to catch the "c" character? - chessinvin 2:53 pm
    • @chessinvin I added a code on questions you are interested in. Try it. - S. Nick