Slate: Intercepting Input Events with Preprocessors

Article written by Cody A.

Unreal Engine uses Slate to propagate input events, passing them either directly to a focus target for key events, or using a generated Hittest Grid to locate a widget under the cursor for mouse events. These events then bubble up the hierarchy, passing to each widget’s parent until the event is either marked as handled or reaches the viewport, where it proceeds to the Player Input system and the bindings configured in Project Settings. However, sometimes there is a need to intercept all input events regardless of their destination. This can be accomplished with Slate Preprocessors, allowing an early chance to process input without needing to implement handlers at multiple levels within the input pipeline.

Slate Preprocessors should inherit from the IInputProcessor interface, which includes a number of functions for handling different types of events. Note that preprocessors are the absolute first thing that will receive the input event, and returning true will terminate the event without any further handling:

virtual bool HandleKeyDownEvent(FSlateApplication& SlateApp, const FKeyEvent& InKeyEvent) override
	//Custom handling here
		return true;
	// Propagate input event for further handling by the standard Slate UI pipeline
	return false;

Once you’ve defined your preprocessor, you’ll need to register it with Slate. This can be done in your module’s StartupModule function, or when a particular UI action occurs (like a menu opening):

InputProcessor = MakeShared<FMyInputProcessor>(this);

Slate Preprocessors can be used for a variety of tasks, such as gating input while certain menus are open or filtering out certain keys for special handling without needing to listen for the key at multiple points throughout the input hierarchy. You can even use it to mutate events, terminating the incoming event and synthesizing a different input event to fire manually.