I have an app I wrote in Python/wxPython. When I click a button, the action that is taken will depend on whether or not a modifier key (such as CTRL) is currently pressed. I can't toggle a swich based on a keypress event (eg ctrldown = True or False) because the form may not have focus continuously. Ideally, the button click code should be something like

if ctrldown:
    #ctrl key is pressed - do one action
else:
    #ctlr key is not pressed - do something else

Is there any way of checking the current status of the keys like CTRL, SHIFT, ALT, etc?

Recommended Answers

All 2 Replies

MouseEvent (you are talking mouse events, right?) has methods AltDown(), ControlDown(), ShiftDown(), MetaDown()...

It's not a mouse event. it's a buttonClick event and the ControlDown method doesn't apply there. I tried the following:

def OnButtonClick(self,event):
        
        #get the name of the image set from the button

        self.Curr = event.EventObject
        
        #hide the previous set
        
        if not event.ControlDown() and self.Prev != None and self.Prev != self.Curr:
            #print "hide",self.Prev.GetName()
            self.HideFiles(self.Prev)
            self.Prev = None
            
        if self.Curr.Show:
            #print "HIDE",self.Curr.GetName()
            self.HideFiles(self.Curr)
            self.Curr = None
        else:
            #print "SHOW",self.Curr.GetName()
            self.ShowFiles(self.Curr)
            self.Prev = self.Curr

        self.RefreshViewer()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.