Initial keyboard focus

I'm using python-wxGTK 2.5.3.1-5 on Suse 9.3.

I have a top level frame that I put a wxGLCanvas in. When I first start the program the canvas receives EVT_MOTION events without having to click in it first. However, EVT_KEY_DOWN or EVT_CHAR events require me to click in the canvas first. Once I click in the canvas, keyboard events are captured as long as the window has focus (the mouse doesn't even need to be in the window).

Any ideas of how to get keyboard events to the wxGLCanvas with window focus when the program starts (i.e. before clicking in the canvas)?

Here is an example of the code I'm using. I took the base of this from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325392/index_txt

Thanks,
James

···

###################################################################
from wxPython.glcanvas import wxGLCanvas
from wxPython.wx import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys,math

name = 'ball_glut'

class myGLCanvas(wxGLCanvas):
     def __init__(self, parent):
         wxGLCanvas.__init__(self, parent,-1)
         EVT_PAINT(self, self.OnPaint)
         EVT_CHAR(self, self.OnChar)
         self.init = 0
         return

     def OnChar(self, event):
         print "OnChar"
         key = event.GetKeyCode()
         if (key < 256):
             key = chr(key)
             if key == 'Q':
                 print "Quitting"
                 wxExit()
             else:
                 print "Unknown key '%s'" % key
         event.Skip()

     def OnPaint(self,event):
         dc = wxPaintDC(self)
         self.SetCurrent()
         if not self.init:
             self.InitGL()
             self.init = 1
         self.OnDraw()
         return

     def OnDraw(self):
         # ... bunch of gl stuff
         return

     def InitGL(self):
         # ... bunch of gl stuff
         return

def main():
     app = wxPySimpleApp()
     frame = wxFrame(None,-1,'ball_wx',wxDefaultPosition,wxSize(400,400))
     canvas = myGLCanvas(frame)
     frame.Show()
     app.MainLoop()

if __name__ == '__main__': main()

###########################################################################

James Bigler wrote:

I'm using python-wxGTK 2.5.3.1-5 on Suse 9.3.

I have a top level frame that I put a wxGLCanvas in. When I first start the program the canvas receives EVT_MOTION events without having to click in it first. However, EVT_KEY_DOWN or EVT_CHAR events require me to click in the canvas first. Once I click in the canvas, keyboard events are captured as long as the window has focus (the mouse doesn't even need to be in the window).

Any ideas of how to get keyboard events to the wxGLCanvas with window focus when the program starts (i.e. before clicking in the canvas)?

def main():
    app = wxPySimpleApp()
    frame = wxFrame(None,-1,'ball_wx',wxDefaultPosition,wxSize(400,400))
    canvas = myGLCanvas(frame)

Try adding this:

       wx.CallAfter(canvas.SetFocus)

···

    frame.Show()
    app.MainLoop()

if __name__ == '__main__': main()

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