Why can't I get OnChar in my wxControl-derived class?

I'm trying to create a control. I've derived a class from wxControl and hooked it's EVT_CHAR message. But I never get notified for evt_char events. *Except* if I hold down the ALT key. If I hold down ALT, I get the WXK_LEFT and WXK_RIGHT I'm looking for.

I'm using Python 2.2 and wxPython 2.3.2.1 on an XP Professional box.

Here's the code I'm having trouble with (it's in a frame for testing):

# $Revision: $
# $Author: $
# $Date: $

from wxPython.wx import *

class HexEdit(wxControl):

  LINE_SPACING = 1
  BYTE_SPACING = 1

  def __init__(self, parent, ID, bytes,
      pos = wxDefaultPosition, size = wxDefaultSize,
      style = 0, validator = wxDefaultValidator,
      name = "hexedit"):

    wxControl.__init__(self, parent, ID, pos, size, style, validator, name)

    self.bytes = bytes
    self.pos = 0

    EVT_PAINT(self, self.OnPaint)
    EVT_CHAR(self, self.OnChar)
    EVT_SET_FOCUS(self, self.OnGainFocus)
    EVT_KILL_FOCUS(self, self.OnLostFocus)

  def OnGainFocus(self, event):
    print "got focus"

  def OnLostFocus(self, event):
    print "lost focus"

  def OnChar(self, event):

    if event.GetKeyCode() == WXK_RIGHT:
      if self.pos < len(self.bytes) - 1:
        self.pos += 1
        print self.pos
        self.Refresh()

    elif event.GetKeyCode() == WXK_LEFT:
      if self.pos > 0:
        self.pos -= 1
        print self.pos
        self.Refresh()

    event.Skip()

  def OnPaint(self, event):
    width, height = self.GetClientSizeTuple()
    dc = wxPaintDC(self)

    # set background color and clear
    dc.SetBackground(wxBrush(
      wxSystemSettings_GetSystemColour(wxSYS_COLOUR_WINDOW), wxSOLID))
    dc.Clear()

    byteWidth, lineHeight = dc.GetTextExtent("AA")
    x = 0
    y = 0
    for i in range(len(self.bytes)):
      # if we're at pos, set the background to the highlighted color
      if i == self.pos:
        oldTextBg = dc.GetTextBackground()
        dc.SetBackgroundMode(wxSOLID)
        dc.SetTextBackground(
          wxSystemSettings_GetSystemColour(wxSYS_COLOUR_HIGHLIGHT))

      byteStr = "%02x" % self.bytes[i]
      dc.DrawText(byteStr, x, y)

      # if we're at pos, set the background back
      if i == self.pos:
        dc.SetTextBackground(oldTextBg)
        dc.SetBackgroundMode(wxTRANSPARENT)

      if i > 0 and i % 8 == 0:
        x = 0
        y += lineHeight + self.LINE_SPACING
      else:
        x += byteWidth + self.BYTE_SPACING

class HexEditFrame(wxFrame):

  OK = 100
  EXIT = 101

  def __init__(self, parent):
    wxFrame.__init__(self, parent, -1, "HexEditFrame")
    panel = wxPanel(self, -1)

    # create controls
    self.hexEdit = HexEdit(panel, -1, [0,1,2,3,4,5,6,7,8,9])
    #self.hexEdit = wxTextCtrl(panel, -1, "abcdefg")
    self.okButton = wxButton(panel, self.OK, "O&k")
    self.exitButton = wxButton(panel, self.EXIT, "&Exit")

    # create line of buttons at bottom
    buttonSizer = wxBoxSizer(wxHORIZONTAL)
    buttonSizer.Add(self.okButton, 0, wxEXPAND | wxALL, 2)
    buttonSizer.Add(0, 0, 1, wxEXPAND, 0)
    buttonSizer.Add(self.exitButton, 0, wxEXPAND | wxALL, 2)

    # line up controls
    mainSizer = wxBoxSizer(wxVERTICAL)
    mainSizer.Add(self.hexEdit, 1, wxEXPAND | wxALL, 0)
    mainSizer.Add(buttonSizer, 0, wxEXPAND | wxALL, 2)

    panel.SetAutoLayout(true)
    panel.SetSizer(mainSizer)

    EVT_BUTTON(self, self.OK, self.OnOk)
    EVT_BUTTON(self, self.EXIT, self.OnExit)
    self.okButton.SetDefault()
    self.hexEdit.SetFocus()

  def OnOk(self, event):
    wxMessageBox("OK Pressed")

  def OnExit(self, event):
    self.Destroy()

app = wxPySimpleApp()
frame = HexEditFrame(None)
frame.Show(true)
app.SetTopWindow(frame)
app.MainLoop()

···

--
Mark Wright
mwright@pro-ns.net

Mark Wright wrote:

I'm trying to create a control. I've derived a class from wxControl and hooked it's EVT_CHAR message. But I never get notified for evt_char events. *Except* if I hold down the ALT key. If I hold down ALT, I get the WXK_LEFT and WXK_RIGHT I'm looking for.

You need to use the wxWANTS_CHARS style.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!