检查是否按下了键,wxpython?
您好,我想知道是否有一种方法可以检查是否按住了某个键。
这是情况的一个例子
self.button2.Bind(wx.EVT_LEFT_DOWN, self.clickedbutton)
def clickedbutton(self, e):
if (Control is held down while the button has been clicked):
print "it works"
谢谢
Hello i am wondering if there is a way to check if a certain key is being held down.
Here is an example of the situation
self.button2.Bind(wx.EVT_LEFT_DOWN, self.clickedbutton)
def clickedbutton(self, e):
if (Control is held down while the button has been clicked):
print "it works"
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅使用 wx 的问题是您需要一个 KeyEvent 来访问控制键的实际状态。由于您在此类事件之外需要此信息,因此您需要手动跟踪它,而问题是很容易错过 KeyEvent,因为只有焦点控件才能获取它们,并且您不能指望它们传播。
万无一失的方法是利用某种平台特定的方式来查询此信息,如果您在 Windows 上查看 pyHook 或 win32api 来获取此信息。
在某些情况下,尽管仅 wx 方法可以工作,但您可以这样做:
The problem with using only wx for this is that you need a KeyEvent to access the actual state of the control key. Since you need this information outside of such an event you need to keep track of it manually, and the problem with that is that it is easy to miss a KeyEvent since only focused controls get them and you can't count on them propagating.
The foolproof way would be to utilize some platform specific way of querying this information, if you are on windows look in to pyHook or win32api for this.
In some cases though the wx only approach can work and here is how you do it:
WxPython 有一个函数 wx.GetKeyState(key),如果键当前按下,则返回 True。它对 Windows 上的所有键都成功。但文档指出“在 wxGTK 中,当前不使用 X11 后端时,此函数只能与修饰键( WXK_ALT 、 WXK_CONTROL 和 WXK_SHIFT )一起使用”。
这是一个便携式替代方案。它使用过滤功能作为应用程序的一部分来过滤所有事件。它捕获按键向上和按键按下事件。它可以扩展到其他事件。
WxPython has a function wx.GetKeyState(key) that returns True if the key is currently down. It succeeds for all keys on Windows. But the documentation states that "In wxGTK, this function can be only used with modifier keys ( WXK_ALT , WXK_CONTROL and WXK_SHIFT ) when not using X11 backend currently".
Here is a portable alternative. It uses a filter function as part of the App to filter all events. It captures the key up and key down events. It can be extended to other events.