This is a bit tricky I’m afraid.
Fist of all, I’m not sure if you already got this part right, but just in case: if you bind a button to EVT_BUTTON as usual, you are catching a CommandEvent, which has no knowledge of any keyboard modifiers.
The event you want to catch is really a MouseEvent: if you check its inheritance diagram, you’ll see that MouseEvent, indeed, has KeyboardState as one of its ancestors: bingo!
So, you will have to bind the button to EVT_LEFT_DOWN or (much better) to EVT_LEFT_UP to access this whole keyboard thing. Don’t forget to Skip the event in the callback, to let the next CommanEvent trigger.
Second, this is not how KeyboardState is supposed to work. You don’t have to create an empty instance by yourself. If you catch the right MouseEvent, then the event is already carrying all the KeyboardState info you need, since it derives from it. All you have to do is to ask directly to the event instance: specifically, you call KeyboardState.GetModifiers on the event instance.
This is a working example of what you want:
def __init__(self, *a, **k):
wx.Frame.__init__(self, *a, **k)
p = wx.Panel(self)
b = wx.Button(p, -1, 'clic', pos=(10, 10))
def onclic(self, evt):
mods = evt.GetModifiers() # THIS is how you call wx.KeyboardState.GetModifiers()
if mods == wx.MOD_CONTROL:
elif mods == wx.MOD_CONTROL|wx.MOD_SHIFT:
print('Control + Shift')
elif evt.HasAnyModifiers(): # again, this is really wx.KeyboardState.HasAnyModifiers()
print('Some other modifier')
app = wx.App()
edit: KeyboardState comes with a few handy shortcuts like HasAnyModifiers, ControlDown etc. You can use those if you want, instad of checking GetModifiers. But, again, on the event instance: ie, “if evt.ControlDown(): …”