简体   繁体   English

如何确定是否在wxPython事件处理程序中按下了Shift键?

[英]How do I determine if the Shift key was pressed in wxPython event handler?

I have a custom dialog which has a few buttons on it. 我有一个自定义对话框,上面有几个按钮。 Since I want to keep it small, I would like if each button did as many things as possible, thus, I would like to have the buttons behave differently if the shift key is pressed when they are clicked. 因为我想保持较小,所以我希望每个按钮都尽可能多地执行操作,因此,如果单击Shift键时希望它们的行为有所不同。 According to this page of the docs, a keyevent has a ShiftDown() function which returns true if the shift key is depressed. 根据文档的页面, keyevent具有ShiftDown()函数,如果按下shift键,该函数将返回true。 The only problem is that my button and handler are bound by wx.EVT_BUTTON , a command event which has no such attribute. 唯一的问题是我的按钮和处理程序由wx.EVT_BUTTON绑定, wx.EVT_BUTTON是一个没有此类属性的命令事件。 Is there any way to have the command event carry information about the shift key, or somehow pass that into the handler? 有什么方法可以使命令事件携带有关shift键的信息,或者以某种方式将其传递给处理程序? Thanks in advance 提前致谢

Basically you need to bind to a couple of other events, specifically wx.EVT_KEY_DOWN and wx.EVT_KEY_UP so you know when the shift key is up or down. 基本上,您需要绑定几个其他事件,特别是wx.EVT_KEY_DOWN和wx.EVT_KEY_UP,这样您才能知道Shift键是向上还是向下。 Check out the following piece of code: 查看以下代码:

import wx

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.shift_down = False

        btn = wx.Button(self, label="Test Shift")
        btn.Bind(wx.EVT_KEY_DOWN, self.onKeyDown)
        btn.Bind(wx.EVT_KEY_UP, self.onKeyUp)
        btn.Bind(wx.EVT_BUTTON, self.onButton)

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Event handler for when the button is pressed
        """
        if self.shift_down:
            print "Shift key is down!"
        else:
            print "Shift key is up!"

    #----------------------------------------------------------------------
    def onKeyDown(self, event):
        """
        If shift key is down, set class variable to True
        """
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_SHIFT:
            self.shift_down = True
        event.Skip()

    #----------------------------------------------------------------------
    def onKeyUp(self, event):
        """
        If shift key is up, set class variable to False
        """
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_SHIFT:
            self.shift_down = False
        event.Skip()

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Demo")
        panel = MyPanel(self)
        self.Show()


#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Tested on Windows 7 with Python 2.6.6 and wxPython 2.8 在Windows 7上使用Python 2.6.6和wxPython 2.8进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM