How to work with wx.EventFilter

I would like to learn about wx.EventFilter.I have these codes as following,but it doesn’t work. why?
class LastActivityTimeDetector(wx.EventFilter):

def __init__(self):

    wx.EventFilter.__init__(self)

    wx.EvtHandler.AddFilter(self)

    self.last = wx.DateTime.Now()        

def __del__(self):

    wx.EvtHandler.RemoveFilter(self)

def FilterEvent(self, event):
    t = event.GetEventType()
    if t == wx.EVT_KEY_DOWN.typeId or t == wx.EVT_MOTION.typeId or \
       t == wx.EVT_LEFT_DOWN.typeId or t == wx.EVT_RIGHT_DOWN.typeId or \
       t == wx.EVT_MIDDLE_DOWN.typeId:

        self.last = wx.DateTime.Now()
        print(self.last)
       
    return self.Event_Skip

def IsInactiveFor(self, diff):

    return wx.DateTime.Now()

class MyFrame7(wx.Frame):
def init(self):

	wx.Frame.__init__(self,None,-1)	
	LastActivityTimeDetector()		

if name == ‘main’:
app = wx.App()
frame = MyFrame7()
frame.Show()
app.MainLoop()

what are you aiming at ? :upside_down_face:

I want to know about wx.EventFilter how to filter the events.I mean the way how it work in real situations . Does it work like conditional statement if ? Or it plays the same role in judging choice as conditional statement if.

Fortune favours the bold :rofl:

I already have an example of the wx.EventFilter.I created an app which is derived from wx.App and I overrided the function of self.FilterEvent().In function,I called my own filterevent.

class myapp(wx.App):
 	def __init__(self):
 		wx.App.__init__(self)
 		self.a=LastActivityTimeDetector()
 	def FilterEvent(self, event):
 		self.a.FilterEvent(self.evt)		
 		return self.Event_Skip  

Now, my own filterevent can run .But I found if I use Bind(),I can get the same result。I want to read source code,but can’t find its. Could you do me a favor?

I’ve to say you work yourself down to the knuckels, but if Bind() does the same there must be a damned good reason for wx.EventFilter still being there, mustn’t it ? :pray:

I do it just for trying to understand it how to work,not intend to use it in real situation.

it’s the number 1 the dispatcher looks at (according to the docu)

g l o b a l

import wx

class Gui(wx.Frame):

    def __init__(self, parent):
        super().__init__(parent, title='wx.EventFilter (global)')

        btn = wx.Button(self, label='left click me, please')
        btn.Bind(wx.EVT_BUTTON, self.test)

        self.Centre()
        self.Show()

    def test(self, _):
        with  Test(self, 'lalala') as dlg:
            dlg.ShowModal()

class Test(wx.TextEntryDialog):

    def __init__(self, parent, msg):
        super().__init__(parent, msg)
        self.Show()

class WxApp(wx.App):

    def __init__(self):
        super().__init__(filename=None)

    def FilterEvent( self, evt):
        print(f'{evt.GetId()} --> {evt}')
        return self.Event_Skip

    def OnInit(self):
        Gui(None)
        self.MainLoop()
        return True

WxApp()

l o c a l

import wx

class EventFilter(wx.EventFilter):
    def __init__(self):
        super().__init__()
        wx.EvtHandler.AddFilter(self)

    def FilterEvent(self, evt):
        print(f'{evt.GetId()} --> {evt}')
        return self.Event_Skip

    def __del__(self):
        wx.EvtHandler.RemoveFilter(self)

class Gui(wx.Frame):

    def __init__(self, parent):
        super().__init__(parent, title='wx.EventFilter (local)')

        btn = wx.Button(self, label='left click me, please')
        btn.Bind(wx.EVT_BUTTON, self.test)

        self.Centre()
        self.Show()

    def test(self, _):
        with  Test(self, 'lalala') as dlg:
            dlg.ShowModal()

class Test(wx.TextEntryDialog):

    def __init__(self, parent, msg):
        super().__init__(parent, msg)
        self.evf = EventFilter()
        self.Show()

    def __del__(self):
        del self.evf

app = wx.App()
Gui(None)
app.MainLoop()

Thanks.I got it :smile: :smile: :smile: :smile:

One more thing,I compared your example of local and my example that I first gave.They differ in whether they are assigned at call time.e.g I gave it likeLastActivityTimeDetector(),you use it like self.evf = EventFilter().I find if I also assign values like self.evf=LastActivityTimeDetector(), my program will run normally.So the key question is why it need to be assigned to run.

this may be a less mystical version of what’s going on (the documentation sometimes sheds light upon a black box)

import wx

class EventFilter(wx.EventFilter):
    def __init__(self):
        super().__init__()

    def FilterEvent(self, evt):
        print(f'{evt.GetId()} --> {evt}')
        return self.Event_Skip

class Gui(wx.Frame):

    def __init__(self, parent):
        super().__init__(parent, title='wx.EventFilter (local)')

        btn = wx.Button(self, label='left click me, please')
        btn.Bind(wx.EVT_BUTTON, self.test)

        self.Centre()
        self.Show()

    def test(self, _):
        with  Test(self, 'lalala') as dlg:
            dlg.ShowModal()

class Test(wx.TextEntryDialog):

    def __init__(self, parent, msg):
        super().__init__(parent, msg)
        self.evf = EventFilter()
        self.AddFilter(self.evf)
        self.Show()

    def __del__(self):
        self.RemoveFilter(self.evf)

app = wx.App()
Gui(None)
app.MainLoop()

Thanks for your hard work!

well, on the way I got interested myself, what made it bearable and as a farewell hint set up the typeId’s at the init like this (membership testing, or are you one who likes to see the machine sweat) :innocent:

class EventFilter(wx.EventFilter):
    def __init__(self):
        super().__init__()
        self.internals = {
            wx.EVT_UPDATE_UI._getEvtType(),
            wx.EVT_IDLE._getEvtType()}

    def FilterEvent(self, evt):
        if evt.GetEventType() not in self.internals:
            print(f'{evt.GetEventType()} --> {evt}')
        return self.Event_Skip

Thanks for you ! I got it ! :+1: