import sysfrom pynput import keyboard, mouseKill = Falsedef on_press(key):global Killif key == keyboard.Key.esc:print("终止程序!")k_listener.stop()Kill = Trueif str(key) == r"'\x11'":print("触发快捷键!")k_listener.stop()print('键盘按下:{0}'.format(key))def on_click(x, y, button, pressed):if pressed:print('鼠标按下:{0} 在 ({1}, {2})'.format(button, x, y))else:print('鼠标释放:{0} 在 ({1}, {2})'.format(button, x, y))m_listener.stop()# 监听键盘和鼠标事件,先监听鼠标,用户按下Ctrl+Q后,开始监听鼠标点击和释放的位置一次,然后回到键盘监听。# 在键盘监听区间可以按ESC退出程序if __name__ == '__main__':while not Kill:with keyboard.Listener(on_press=on_press) as k_listener:k_listener.join()if not Kill:with mouse.Listener(on_click=on_click) as m_listener:m_listener.join()
700字范文,内容丰富有趣,生活中的好帮手!