JcMaco
JcMaco

Reputation: 1278

Prevent a timer from updating a text box if the key cursor is in the box

Is it possible to check if a TextCtrl is under keyboard focus (blinking cursor in text box) without defining a handler for EVT_SET_FOCUS?

I just want to do a quick boolean check to prevent a wx.Timer from overwriting the text box if the user is writing something in the box.

Upvotes: 0

Views: 341

Answers (1)

tom10
tom10

Reputation: 69242

You can bypass a timer update by finding which window has the focus (using FindFocus) and comparing this to your TextCtrl window. Then, if your TextCtrl has the focus you can leave it alone. Here's an example:

import wx

class TestFrame(wx.Frame):

    def __init__(self):
        self.count = 0
        wx.Frame.__init__(self, None, -1, "test frame", size=(200, 100))
        self.panel = wx.Panel(self, -1)
        button = wx.Button(self.panel, -1, "b", pos=(10, 40))
        self.text = wx.TextCtrl(self.panel, -1, `self.count`, size=(50, 25))
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.incr_text)
        self.timer.Start(1000)

    def incr_text(self, event):
        self.count += 1
        f = self.FindFocus()
        if not f==self.text:
            self.text.SetValue(`self.count`)

if __name__=="__main__":
    app = wx.PySimpleApp()
    TestFrame().Show()
    app.MainLoop()

Upvotes: 3

Related Questions