如何使用Python中的Tkinter检查当前是否按下了某个键?

时间:2020-12-27 02:27:10

Is there any way to detect which keys are currently pressed using Tkinter? I don't want to have to use extra libraries if possible. I can already detect when keys are pressed, but I want to be able to check at any time what keys are pressed down at the moment.

有没有办法检测当前使用Tkinter按下哪些键?如果可能的话,我不想使用额外的库。我已经可以检测到按下按键的时间,但我希望能够随时检查当前按下了哪些按键。

1 个解决方案

#1


I think you need to keep track of events about keys getting pressed and released (maintaining your own set of "currently pressed" keys) -- I believe Tk doesn't keep track of that for you (and Tkinter really adds little on top of Tk, it's mostly a direct interface to it).

我认为你需要跟踪按下和释放按键的事件(维护你自己的“当前按下”按键) - 我相信Tk不会为你记录这一点(而且Tkinter真的在Tk,它主要是它的直接接口)。

#1


I think you need to keep track of events about keys getting pressed and released (maintaining your own set of "currently pressed" keys) -- I believe Tk doesn't keep track of that for you (and Tkinter really adds little on top of Tk, it's mostly a direct interface to it).

我认为你需要跟踪按下和释放按键的事件(维护你自己的“当前按下”按键) - 我相信Tk不会为你记录这一点(而且Tkinter真的在Tk,它主要是它的直接接口)。