Reputation: 2165
I have a wxPython grid (wx.grid) class with rows/columns and such. I'm trying to detect when the user performs a "Control + Click" on a particular cell. Right now I have:
def __init__(self, parent, size):
grd.Grid.__init__(self, parent, -1, size=size)
self.control_button_pressed = False
self.Bind(grd.EVT_GRID_CELL_LEFT_CLICK, self._OnSelectedCell)
self.Bind(wx.EVT_KEY_DOWN, self._OnKeyPress)
self.Bind(wx.EVT_KEY_UP, self._OnKeyUp)
def _OnKeyPress(self, event):
self.control_button_pressed = True
event.Skip()
def _OnKeyLift(self, event):
self.control_button_pressed = False
def _OnSelectedCell(self, event):
print "Cell Selected"
This works fine when just clicking on the cell, but when I perform a Control + Click, this event doesn't even fire.
How can I bind this event?
Upvotes: 0
Views: 1719
Reputation: 33071
I think you'll need to bind to EVT_KEY_DOWN and EVT_KEY_UP. In the key down event, set some variable like "self.ctrl" to True. In the up event, set it to False. You should probably initially set it to False as well. Then when it's held down, it becomes True and as long as you call event.Skip(), your grid event should fire when you click. Something along those lines should work anyway.
This might help you understand key events better: http://www.blog.pythonlibrary.org/2009/08/29/wxpython-catching-key-and-char-events/
Never mind this answer --> see Robin Dunn's
Upvotes: 0
Reputation: 6206
The Grid class is already handling Ctrl-Click events to implement adding cells to the collection of selected cells. Since the grid is already consuming that event for that purpose then the event is not propagated or converted into grid events (other than selection events.) However you can intercept the lower level mouse events before the grid gets them, and do your checks there. Try binding handlers directly to the grid window component of the Grid, like this:
self.GetGridWindow().Bind(wx.EVT_LEFT_UP, self.onLeftUp)
Be sure to call event.Skip() in your mouse event handler so the Grid can still get and process the event.
You do not have to worry about catching the key events for this because the event object passed to the mouse event handlers includes methods for getting the state of the modifier keys at the time that the mouse event happened.
Upvotes: 1