0

I am writing an interface where I'd like to have a user click a button, then capture his next keystroke.

I can currently capture all the keys on the keyboard, except for those like tab or the arrow keys which cause the button to lose focus when pressed. I know that I need to unbind the window from keypress events during the capture interval.

I've already tried unbinding from the frame, the notebook tab widget, and the panel that's inside it, and it has not worked.

Where in the hierarchy do I need to unbind key presses to avoid having them behave the way they normally do? Thanks

1 Answer 1

1
import wx 

a = wx.App(redirect=False)
class XFrame(wx.Frame):
    def __init__(self,*args):
        wx.Frame.__init__(self,*args)
        b = wx.Button(self,-1,"Click")
        b.Bind(wx.EVT_BUTTON,self.OnButton)
    def OnButton(self,evt):
        self.SetFocus()
        self.Bind(wx.EVT_CHAR,self.OnChar)
    def OnChar(self,evt):
        print evt.KeyCode
        self.Unbind(wx.EVT_CHAR)

f = XFrame(None,-1,"A Frame")

f.Show()
a.MainLoop()

I think anyway

Sign up to request clarification or add additional context in comments.

1 Comment

Ha! It never occurred to me to bind the keypress event to the window itself. Thank you! My code has been modified and runs beautifully now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.