Skip to content

Instantly share code, notes, and snippets.

@samuelmaddock
Created November 18, 2016 01:33
Show Gist options
  • Save samuelmaddock/c0582aa8bf53c1ccd86a38463de261d3 to your computer and use it in GitHub Desktop.
Save samuelmaddock/c0582aa8bf53c1ccd86a38463de261d3 to your computer and use it in GitHub Desktop.
Unreal Engine 4 UE4 Hack to bind a delegate to all key presses. This allows for multiple keys to be pressed down at once without blocking events.
// example.h
DECLARE_DYNAMIC_DELEGATE_TwoParams(FBindAllKeysDelegate, const FKey&, Key, bool, bKeyPressed);
// example.cpp
void UExampleStatics::BindAllKeyInputActions(AActor *Actor, const FBindAllKeysDelegate& Delegate)
{
// Get input component from actor
UInputComponent *InputComponent = Actor->InputComponent;
if (InputComponent == nullptr) return;
// Bind to all keys
const FKey Key = EKeys::AnyKey;
// Listen for key pressed
FInputKeyBinding KBP(FInputChord(Key, false, false, false, false), EInputEvent::IE_Pressed);
KBP.bConsumeInput = true;
KBP.bExecuteWhenPaused = false;
KBP.KeyDelegate.GetDelegateWithKeyForManualSet().BindLambda([=](const FKey& Key) {
Delegate.ExecuteIfBound(Key, true);
});
InputComponent->KeyBindings.Add(KBP);
// Listen for key released
FInputKeyBinding KBR(FInputChord(Key, false, false, false, false), EInputEvent::IE_Released);
KBR.bConsumeInput = true;
KBR.bExecuteWhenPaused = false;
KBR.KeyDelegate.GetDelegateWithKeyForManualSet().BindLambda([=](const FKey& Key) {
Delegate.ExecuteIfBound(Key, false);
});
InputComponent->KeyBindings.Add(KBR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment