Hi,
I’m creating a scene which doesn’t have a Character in it and I want to be able to detect a keypress inside a C++ script so I can do some other tasks that I believe I know how to perform already.
I’ve asked ChatGPT if it could help me with this and the code it spit out fails to compile (as i believe it’s outdated now).
I’m guessing there is a slightly different way of going about this in UE5/5.1 so any help would be appreciated.
Here is the code I tried that failed because it didn’t like the definition of the last line:
#include "MyKeyActor.h"
// Called every frame
void AMyKeyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Handle key input
HandleKeyInput(DeltaTime);
}
// Called when the game starts or when spawned
void AMyKeyActor::BeginPlay()
{
Super::BeginPlay();
// Set up input component
InputComponent = FindComponentByClass<UInputComponent>();
if (InputComponent)
{
// Bind key events
InputComponent->BindKey(EKeys::SpaceBar, IE_Pressed, this, &AMyKeyActor::OnKeyPressed);
InputComponent->BindKey(EKeys::SpaceBar, IE_Released, this, &AMyKeyActor::OnKeyReleased);
}
}
// Called when a key is pressed
void AMyKeyActor::OnKeyPressed(FKey Key)
{
UE_LOG(LogTemp, Warning, TEXT("Key %s pressed"), *Key.ToString());
}
// Called when a key is released
void AMyKeyActor::OnKeyReleased(FKey Key)
{
UE_LOG(LogTemp, Warning, TEXT("Key %s released"), *Key.ToString());
}
// Handle key input
void AMyKeyActor::HandleKeyInput(float DeltaTime)
{
// Check if input component exists
if (!InputComponent)
{
return;
}
// Handle input
InputComponent->ProcessInputStack();
}
InputComponent->ProcessInputStack();
I get an error from the compiler along the line that ProcessInputStack isn’t defined, no matter what combination params I plug in to it. I believe the source comes from PlayerInput.h but using
virtual void ProcessInputStack(const TArray<UInputComponent*>& InputComponentStack, const float DeltaTime, const bool bGamePaused);
in the form: (nullptr, DeltaTime, false) still produces an error.
I’m not looking for the blueprint solution here as I want a script that will detect my keyboard input and fire off code to set a position of a Camera and for whatever reason this only seems to be possible in C++.
Thanks!
Tim