# 输入控制
此篇为C++对输入的控制。
# PlayerController
在PlayerController中进行输入控制,这是最推荐的一种,该类中有很多输入控制类:
void AMyPlayerController::Tick(float DeltaSeconds)
{
if(IsInputKeyDown(EKeys::A))
{
UE_LOG(LogTemp, Warning, TEXT("playcontroller is pressing A!"));
}
if(WasInputKeyJustPressed(EKeys::B))
{
UE_LOG(LogTemp, Warning, TEXT("playcontroller is pressing B!"));
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# Pawn
Pawn和PlayerController是联系很紧密的,但是Pawn中却没有上述方法,在该类中,可以使用绑定事件来处理:
void AMyPawnCommonLogic::BeginPlay()
{
Super::BeginPlay();
//绑定虚拟轴,PressedA为Input中的虚拟按键名
InputComponent->BindAction("PressedA",IE_Pressed,this,&AMyPawnCommonLogic::PressAKey);
}
void AMyPawnCommonLogic::PressAKey()
{
UE_LOG(LogTemp, Warning, TEXT("play source!"));
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# GameInstance
在Gameplay框架中一般不是用这个来处理输入的,但是我还是想在这里面尝试下:
void UMyGameInstance::Init()
{
GetWorld()->GetTimerManager().SetTimer(myRepeatTimerHandle, this, &UMyGameInstance::RepeatRunFunction, 1.0f, true);
}
void UMyGameInstance::Shutdown()
{
UE_LOG(LogTemp, Warning, TEXT("game instance is destroyed!"));
GetWorld()->GetTimerManager().ClearTimer(myRepeatTimerHandle);
}
void UMyGameInstance::RepeatRunFunction()
{
float pressTime =UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetInputKeyTimeDown(FKey("A"));
if(pressTime>0)
{
UE_LOG(LogTemp, Warning, TEXT("press a!"));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19