| Version: 3.3.1

Public Types | Public Member Functions | List of all members
wxEventFilter Class Referenceabstract
Class List by Category » Events

`#include <wx/eventfilter.h>`

Inheritance diagram for wxEventFilter:
[legend]

## Detailed Description

A global event filter for pre-processing all the events generated in the program.

This is a very simple class which just provides FilterEvent() virtual method to be called by wxEvtHandler before starting process of any event. Thus, inheriting from this class and overriding FilterEvent() allows capturing and possibly handling or ignoring all the events happening in the program. Of course, having event filters adds additional overhead to every event processing and so should not be used lightly and your FilterEvent() code should try to return as quickly as possible, especially for the events it is not interested in.

An example of using this class:

// This class allows determining the last time the user has worked with
// this application:
class LastActivityTimeDetector : public wxEventFilter
{
public:
LastActivityTimeDetector()
{
wxEvtHandler::AddFilter(this);
m_last = wxDateTime::Now();
}
virtual ~LastActivityTimeDetector()
{
wxEvtHandler::RemoveFilter(this);
}
virtual int FilterEvent(wxEvent& event)
{
// Update the last user activity
const wxEventType t = event.GetEventType();
if ( t == wxEVT_KEY_DOWN || t == wxEVT_MOTION ||
t == wxEVT_LEFT_DOWN ||
t == wxEVT_RIGHT_DOWN ||
t == wxEVT_MIDDLE_DOWN )
{
m_last = wxDateTime::Now();
}
// Continue processing the event normally as well.
return Event_Skip;
}
// This function could be called periodically from some timer to
// do something (e.g. hide sensitive data or log out from remote
// server) if the user has been inactive for some time period.
bool IsInactiveFor(const wxTimeSpan& diff) const
{
return wxDateTime::Now() - diff > m_last;
}
private:
wxDateTime m_last;
};
wxDateTime
wxDateTime class represents an absolute moment in time.
Definition: datetime.h:77
wxDateTime::Now
static wxDateTime Now()
Returns the object corresponding to the current time in local time zone.
wxEventFilter
A global event filter for pre-processing all the events generated in the program.
Definition: eventfilter.h:83
wxEventFilter::Event_Skip
@ Event_Skip
Process event as usual.
Definition: eventfilter.h:89
wxEventFilter::FilterEvent
virtual int FilterEvent(wxEvent &event)=0
Override this method to implement event pre-processing.
wxEvent
An event is a structure holding information about an event passed to a callback or member function.
Definition: event.h:93
wxEvtHandler::RemoveFilter
static void RemoveFilter(wxEventFilter *filter)
Remove a filter previously installed with AddFilter().
wxEvtHandler::AddFilter
static void AddFilter(wxEventFilter *filter)
Add an event filter whose FilterEvent() method will be called for each and every event processed by w...
wxTimeSpan
wxTimeSpan class represents a time interval.
Definition: datetime.h:2068
wxEVT_MOTION
wxEventType wxEVT_MOTION
Definition: event.h:5385
wxEVT_LEFT_DOWN
wxEventType wxEVT_LEFT_DOWN
Definition: event.h:5379
wxEVT_RIGHT_DOWN
wxEventType wxEVT_RIGHT_DOWN
Definition: event.h:5383
wxEventType
int wxEventType
A value uniquely identifying the type of the event.
Definition: event.h:5162
wxEVT_KEY_DOWN
wxEventType wxEVT_KEY_DOWN
Definition: event.h:5405
wxEVT_MIDDLE_DOWN
wxEventType wxEVT_MIDDLE_DOWN
Definition: event.h:5381

Notice that wxApp derives from wxEventFilter and is registered as an event filter during its creation so you may also override FilterEvent() method in your wxApp-derived class and, in fact, this is often the most convenient way to do it. However creating a new class deriving directly from wxEventFilter allows isolating the event filtering code in its own separate class and also having several independent filters, if necessary.

Category: Events

Since
  2.9.3

## Public Types
enum | { Event_Skip = -1 , Event_Ignore = 0 , Event_Processed = 1 }
| Possible return values for FilterEvent(). More...

## Public Member Functions
| wxEventFilter ()
| Default constructor. More...
virtual | ~wxEventFilter ()
| Destructor. More...
virtual int | FilterEvent (wxEvent &event)=0
| Override this method to implement event pre-processing. More...

## Member Enumeration Documentation

## ◆ anonymous enum

anonymous enum

Possible return values for FilterEvent().

Enumerator
Event_Skip | Process event as usual.
Event_Ignore | Don't process the event normally at all.
Event_Processed | Event was already handled, don't process it normally.

## Constructor & Destructor Documentation

## ◆ wxEventFilter()

wxEventFilter::wxEventFilter | ( |  | ) |

Default constructor.

Constructor does not register this filter using wxEvtHandler::AddFilter(), it's your responsibility to do it when necessary.

Notice that the objects of this class can't be copied.

## ◆ ~wxEventFilter()

virtual wxEventFilter::~wxEventFilter | ( |  | ) |
| virtual

Destructor.

You must call wxEvtHandler::RemoveFilter() before destroying this object (possibly from the derived class destructor), failure to do this is indicated by an assert unless assertions are disabled.

## Member Function Documentation

## ◆ FilterEvent()

virtual int wxEventFilter::FilterEvent | ( | wxEvent & | event | ) |
| pure virtual

Override this method to implement event pre-processing.

This method allows filtering all the events processed by the program, so you should try to return quickly from it to avoid slowing down the program to a crawl.

Although the return type of this method is `int`, this is only due to backwards compatibility concerns and the actual return value must be one of the `Event_XXX` constants defined above:

- Event_Skip to continue processing the event normally (this should be used in most cases).
- Event_Ignore to not process this event at all (this can be used to suppress some events).
- Event_Processed to not process this event normally but indicate that it was already processed by the event filter and so no default processing should take place either (this should only be used if the filter really did process the event).

Implemented in wxAppConsole.