Here we prepare a template of WidgetComponent in a existing actor, its bReceiveHardwareInput=true. When we need to add widget component to somebody, attach it to the one who really need it. It is troublesome but don’t need to modify source code. The reason is explained below
Reason of troublesome usage
When level is loaded, all components are registered.
At the WidgetComponent register function, it will call CanReceiveHardwareInput() to determine whether to register a HitTester into viewport.
HitTester is a helper class to call GetHitResultAtScreenPosition() to capture WidgetComponent and the UI clicked. It is called every frame.
If not, the WidgetComponent will not register HitTester, then even though you set bReceiveHardwareInput=true by your custom cpp function, HitTester will not be created again.
So, use CreateComponentByClass to create a WidgetComponent is a bad idea, because the default value of bReceiveHardwareInput is false.
The solution is create a WidgetComponent in blueprint, and set bReceiveHardwareInput=true, then will game init, the WidgetComponent deserialization will read blueprint value. Or create a custom cpp class to derive from WidgetComponent class, override default value of bReceiveHardwareInput=true.