Avoiding focus shift on enter in wx.grid.Grid

Hi,

Is there a way to stop the focus from going to the next cell when you hit enter after entering a value in a cell?

Thanks,
Soren

You just need to catch the keycode(s) for Enter by binding the cell's
editor to EVT_KEY_DOWN and stop it from passing on by not calling
event.Skip(). Of course, you'll want to call event.Skip() for all
other key codes or you won't be able to type anything into the cell.

···

On Jun 20, 12:43 pm, Søren Nielsen <soren.skou.niel...@gmail.com> wrote:

Hi,

Is there a way to stop the focus from going to the next cell when you hit
enter after entering a value in a cell?

Thanks,
Soren

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hm that only seems to work if enter is pressed on the cell without entering anything in the cell… but if text is entered into the cell and enter is then pressed the focus still moves down to the next cell. Is there a way to avoid that?

Soren

···

On Mon, Jun 21, 2010 at 9:59 AM, Mike Driscoll kyosohma@gmail.com wrote:

On Jun 20, 12:43 pm, Søren Nielsen soren.skou.niel...@gmail.com > > wrote:

Hi,

Is there a way to stop the focus from going to the next cell when you hit

enter after entering a value in a cell?

Thanks,

Soren

You just need to catch the keycode(s) for Enter by binding the cell’s

editor to EVT_KEY_DOWN and stop it from passing on by not calling

event.Skip(). Of course, you’ll want to call event.Skip() for all

other key codes or you won’t be able to type anything into the cell.


Mike Driscoll

Blog: http://blog.pythonlibrary.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

That's odd...catching the keys has always worked for me. Here's some
sample code:

<code>

self.myGrid.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.onCellEdit)

def onCellEdit(self, event):
    '''
    When cell is edited, get a handle on the editor widget
    and bind it to EVT_KEY_DOWN
    '''
    editor = event.GetControl()
    self.editorID = event.GetId()
    if wx.Platform == '__WXMSW__':
        editor.Bind(wx.EVT_KEY_DOWN, self.onEditorKey)
    event.Skip()

def onEditorKey(self, event):
    keycode = event.GetKeyCode()
    if keycode == WXK_RETURN:
       return
    event.Skip()
</code>

Something like that should work...

···

On Jun 21, 8:17 pm, Søren Nielsen <soren.skou.niel...@gmail.com> wrote:

Hm that only seems to work if enter is pressed on the cell without entering
anything in the cell.. but if text is entered into the cell and enter is
then pressed the focus still moves down to the next cell. Is there a way to
avoid that?

Soren

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Ah ok, thanks Mike! I didn’t do it under a EVT_GRID_EDITOR_CREATED event.

···

On Tue, Jun 22, 2010 at 9:56 AM, Mike Driscoll kyosohma@gmail.com wrote:

On Jun 21, 8:17 pm, Søren Nielsen soren.skou.niel...@gmail.com > > wrote:

Hm that only seems to work if enter is pressed on the cell without entering

anything in the cell… but if text is entered into the cell and enter is

then pressed the focus still moves down to the next cell. Is there a way to

avoid that?

Soren

That’s odd…catching the keys has always worked for me. Here’s some

sample code:

self.myGrid.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.onCellEdit)

def onCellEdit(self, event):

'''

When cell is edited, get a handle on the editor widget

and bind it to EVT_KEY_DOWN

'''

editor = event.GetControl()

self.editorID = event.GetId()

if wx.Platform == '__WXMSW__':

    editor.Bind(wx.EVT_KEY_DOWN, self.onEditorKey)

event.Skip()

def onEditorKey(self, event):

keycode = event.GetKeyCode()

if keycode == WXK_RETURN:

   return

event.Skip()

Something like that should work…


Mike Driscoll

Blog: http://blog.pythonlibrary.org

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en