0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

【UE5】C++ の Tick 内でキー入力判定する方法のメモ

Last updated at Posted at 2024-06-26

はじめに

簡単な機能を試したいときなどに、自分でトリガーする手段としてキー入力で判定したかったのでそれのメモ。
今回は例として Y キーをアサインしているが、ここにあるものから自由にアサインできるので参考までに。
ここであげるコードは Tick メソッド内に実装して、動作確認している。

押した瞬間の 1 回のみを判定する

if (GetWorld()->GetFirstPlayerController()->WasInputKeyJustPressed(EKeys::Y)) {
    UE_LOG(LogTemp, Warning, TEXT("Y key is pressed"));
}

動作ログ

押した瞬間だけ反応し、長押ししようが関係なく 1 回のみ。

LogTemp: Warning: Y key is pressed

離した瞬間の 1 回のみを判定する

if (GetWorld()->GetFirstPlayerController()->WasInputKeyJustReleased(EKeys::Y)) {
    UE_LOG(LogTemp, Warning, TEXT("Y key is released"));
}

動作ログ

離した瞬間だけ反応し 1 回のみ。

LogTemp: Warning: Y key is released

押している間ずっと true 判定する

if (GetWorld()->GetFirstPlayerController()->IsInputKeyDown(EKeys::Y)) {
    UE_LOG(LogTemp, Warning, TEXT("Y key is pressed"));
}

動作ログ

押している間ずっと true 判定がでている。(Tick で回る周期で出力)
離した瞬間が分かりやすいように released も仕込んである

LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is pressed
LogTemp: Warning: Y key is released

Documents

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0