简体   繁体   中英

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. According to this page of the docs, a keyevent has a ShiftDown() function which returns true if the shift key is depressed. The only problem is that my button and handler are bound by wx.EVT_BUTTON , a command event which has no such attribute. Is there any way to have the command event carry information about the shift key, or somehow pass that into the handler? 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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