如何在后台运行Pynput?

问题描述

我正在使用pynput的def main(): with Listener(on_release=checkSPress) as listener: listener.join() # keep on listening def checkSPress(key): if 'char' in dir(key): if key.char == 's': print("Starting ...") main() ,并已加入线程。

s

按下s键后,我希望它终止侦听器并在主线程中运行命令。我想要这样做是因为在按下QApplication must be created inside main thread键之后,我创建了一个GUI,但是它抛出了一个错误,即void reflect(int height,int width,RGBTRIPLE image[height][width]) { RGBTRIPLE coloursofaddress[width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { // Step 3 coloursofaddress[width - 1 - j].rgbtRed = image[i][j].rgbtRed; coloursofaddress[width - 1 - j].rgbtGreen = image[i][j].rgbtGreen; coloursofaddress[width - 1 - j].rgbtBlue = image[i][j].rgbtBlue; // Step 4 image[i][j].rgbtRed = coloursofaddress[j].rgbtRed; image[i][j].rgbtGreen = coloursofaddress[j].rgbtGreen; image[i][j].rgbtBlue = coloursofaddress[j].rgbtBlue; } } return; } 。有没有办法我可以返回主线程并运行函数或完全终止侦听器(我对线程和Pynput相当陌生。)

解决方法

您只需要从return False callback停止Listner。只需尝试:

if key.char == 's':
    print("Starting ...")
    return False