使用Python Tkinter时如何绑定/映射全局热键? [英] How bind/map global hotkey when using Python Tkinter?

查看:152
本文介绍了使用Python Tkinter时如何绑定/映射全局热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定/映射一个全局热键,当最小化应用程序窗口并且用户不关注它时,它也可以工作.我正在寻找一种跨平台的方式.我可以使用Tkinter做到这一点吗?

I want to bind/map a global hotkey, which also works when application window is minimized and user not focused on it. I'm looking for a cross-platform way. Can I do this using Tkinter?

推荐答案

使用tkinter时,如果窗口没有键盘焦点,它将不会收到按键事件,因此您将无法执行所需的操作.

With tkinter, if the window does not have keyboard focus it won't receive the keypress events so you won't be able to do what you want.

但是,通过Pynput,您可以收听按键事件:

However, with pynput you can listen to the keypress events:

from pynput.keyboard import Listener

def on_press(key):
    print("PRESSED", key)


with Listener(on_press=on_press) as listener:
    listener.join()

这应该是跨平台的(但要有一些特定格式的限制).

This should be cross-platform (but with some patform specific limitations).

这篇关于使用Python Tkinter时如何绑定/映射全局热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆