[wxPython] Key events

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Key events

Gary Strangman

Hi,

I'm quite new to wxPython (though I already think it is an excellent
addi-on to my favorite programming language :-).  I'm having a little
trouble, probably due to my ignorance ... for which I apologize in
advance. (I'm on WinNT, wxPython2.1b2.) I'm trying to capture key events,
not unlike the hangman.py demo.  The demo works fine.  In my case, I have
a frame with 2 panels ... one for drawing, one for controls.  In the init
for the frame, I have tried both of the following ...

        EVT_CHAR(self, self.keypressed)
        EVT_CHAR_HOOK(self, self.keypressed)

... where self is the frame, and self.keypressed is (for the moment)
simply:

    def keypressed(self, event):
        print 'You pressed the ',event.KeyCode(),' key.', event

Using the EVT_CHAR, pressing a key _never_ results in a call to
keypressed.  Using the EVT_CHAR_HOOK, keypressed() is called for special
keys (return, shift, control, alt, tab, esc, fcn keys, arrow keys, etc.)
but not for any "regular" keys (a-z, 0-9, etc.).  I've tried setting the
focus on the frame and on the panel, and neither helps.  Anyone willing to
take pity on me and tell me what I'm missing?  Looking through hangman.py
my guess would be nothing, but that's apparently inaccurate ... ;-)

Gary



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Key events

Harm van der Heijden
Gary Strangman wrote:
>
[...]
> Using the EVT_CHAR, pressing a key _never_ results in a call to
> keypressed.  Using the EVT_CHAR_HOOK, keypressed() is called for special
> keys (return, shift, control, alt, tab, esc, fcn keys, arrow keys, etc.)
> but not for any "regular" keys (a-z, 0-9, etc.).  I've tried setting the
> focus on the frame and on the panel, and neither helps.  Anyone willing to
                                ^^^^^
> take pity on me and tell me what I'm missing?  Looking through hangman.py
> my guess would be nothing, but that's apparently inaccurate ... ;-)

I had the exact same problem when I wrote hangman.py a couple of months ago.
The solution was to derive the window that was supposed to capture the key
events from a wxWindow, not from a wxPanel. (wxFrames also don't seem to
react to key events).

Glad to see somebody still playing hangman :-)

Regards,

Harm

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Key events

Harm van der Heijden
Harm van der Heijden wrote:

>
> Gary Strangman wrote:
> >
> [...]
> > Using the EVT_CHAR, pressing a key _never_ results in a call to
> > keypressed.  Using the EVT_CHAR_HOOK, keypressed() is called for special
> > keys (return, shift, control, alt, tab, esc, fcn keys, arrow keys, etc.)
> > but not for any "regular" keys (a-z, 0-9, etc.).  I've tried setting the
> > focus on the frame and on the panel, and neither helps.  Anyone willing to
>                                 ^^^^^
> > take pity on me and tell me what I'm missing?  Looking through hangman.py
> > my guess would be nothing, but that's apparently inaccurate ... ;-)
>
> I had the exact same problem when I wrote hangman.py a couple of months ago.
> The solution was to derive the window that was supposed to capture the key
> events from a wxWindow, not from a wxPanel. (wxFrames also don't seem to
> react to key events).
>
> Glad to see somebody still playing hangman :-)

Oops, forgot to clarify something. In the frame's init, don't use
EVT_CHAR(self, self.OnChar)
but do something like
wnd = wxWindow(self, -1) # take the whole frame client space by default
EVT_CHAR(wnd, self.OnChar)

which is more or less how it's done in hangman.

Harm

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Key events

Robin Dunn
>
> Oops, forgot to clarify something. In the frame's init, don't use
> EVT_CHAR(self, self.OnChar)
> but do something like
> wnd = wxWindow(self, -1) # take the whole frame client space by default
> EVT_CHAR(wnd, self.OnChar)
>

Right, this is because key and char events are not "command events" so the
don't travel up the containment hierarchy but only go to the window where
the event happened.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users