🍃
【UE5】自作Shortcut Key(ショートカットキー)を作成してショートカットキーが押されたらログを出してみる
概要
この記事は一人アドベントカレンダー by ダリアの25日目の記事です。
今回は自作ショートカットキーを作成し、ショートカットキーが押されたらログを出す仕組みを作ろうと思います。
環境
UE5.4.4
やり方
- (なければ)Editorモジュールを作成する
- ショートカットを定義するクラス
TCommands
を継承したクラスを作成する - モジュールクラスにショートカットキーを登録する
Editorモジュールを作成する
Editorモジュールを作成し、以下のモジュールを追加します。
ExampleShortcutCommandEditor.Build.cs
using UnrealBuildTool;
public class ExampleShortcutCommandEditor : ModuleRules
{
public ExampleShortcutCommandEditor(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
}
);
PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"Slate",
"SlateCore",
+ "InputCore",
+ "UnrealEd",
+ "EditorStyle",
}
);
}
}
TCommands
を継承したクラスを作成する
ショートカットを定義するクラスショートカットキーを登録するには、TCommands
クラスを継承したクラスが必要なので作成します。
今回ショートカットキーは他のショートカットキーと被らなさそうな所としてCtrl+Shift+Spaceとします。
#pragma once
#include <Framework/Commands/Commands.h>
class FExampleLogCommand : public TCommands<FExampleLogCommand>
{
public:
FExampleLogCommand();
virtual void RegisterCommands() override;
TSharedPtr<FUICommandInfo> ShowLogCommand;
};
#include "AIStopCommand.h"
#define LOCTEXT_NAMESPACE "AIStop"
FExampleLogCommand::FExampleLogCommand()
: TCommands("ShowLog",LOCTEXT("ShowLogCommandLabel", "Show Log"), NAME_None, FAppStyle::GetAppStyleSetName())
{
}
void FExampleLogCommand::RegisterCommands()
{
UI_COMMAND(ShowLogCommand,"ShowLog", "Show Log", EUserInterfaceActionType::Button, FInputChord(EModifierKey::Control | EModifierKey::Shift, EKeys::SpaceBar))
}
#undef LOCTEXT_NAMESPACE
モジュールクラスにショートカットキーを登録する
先ほど作ったコマンドを登録してショートカットキーが呼ばれたら処理を呼べるようにします。
#pragma once
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
class FExampleShortcutCommandEditorModule : public IModuleInterface
{
private:
virtual void StartupModule() override;
virtual void ShutdownModule() override;
};
#include "ExampleShortcutCommandEditor.h"
#include "AIStopCommand.h"
#include "Kismet2/DebuggerCommands.h"
#define LOCTEXT_NAMESPACE "FExampleShortcutCommandEditorModule"
void FExampleShortcutCommandEditorModule::StartupModule()
{
//ショートカットキーの登録を行う
FExampleLogCommand::Register();
if (FPlayWorldCommands::GlobalPlayWorldActions.IsValid())
{
//ショートカットキーが呼ばれたら何をするか記述する
FPlayWorldCommands::GlobalPlayWorldActions->MapAction(FExampleLogCommand::Get().ShowLogCommand,
FExecuteAction::CreateLambda(
[]()
{
//今回はログを呼ぶだけにする
UE_LOG(LogTemp,Display,TEXT("Call from Shortcut"));
}));
}
}
void FExampleShortcutCommandEditorModule::ShutdownModule()
{
FExampleLogCommand::Unregister();
}
#undef LOCTEXT_NAMESPACE
IMPLEMENT_MODULE(FExampleShortcutCommandEditorModule, ExampleShortcutCommandEditor)
こんな感じで呼ばれていたら成功です。
おまけ ショートカットキーの自作した項目の見つけ方
自分の場合ですが、編集
->エディタの環境設定
->キーボードショートカット
->Take Recorder
のStartRecording
から探した所見つけることが出来たので、このショートカットキーはどうなってるんだろう?の参考になれば幸いです。
実際に登録されてる図
実際のコマンドクラス
Engine\Plugins\VirtualProduction\Takes\Source\TakeRecorder\Private\TakeRecorderCommands.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Framework/Commands/Commands.h"
class FTakeRecorderCommands : public TCommands<FTakeRecorderCommands>
{
public:
FTakeRecorderCommands();
TSharedPtr<FUICommandInfo> StartRecording;
TSharedPtr<FUICommandInfo> StopRecording;
/** Initialize commands */
virtual void RegisterCommands() override;
};
Engine\Plugins\VirtualProduction\Takes\Source\TakeRecorder\Private\TakeRecorderCommands.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "TakeRecorderCommands.h"
#include "TakeRecorderStyle.h"
#define LOCTEXT_NAMESPACE "TakeRecorderCommands"
FTakeRecorderCommands::FTakeRecorderCommands()
: TCommands<FTakeRecorderCommands>("TakeRecorder", LOCTEXT("TakeRecorderCommandLabel", "Take Recorder"), NAME_None, FTakeRecorderStyle::StyleName)
{
}
void FTakeRecorderCommands::RegisterCommands()
{
UI_COMMAND(StartRecording, "StartRecording", "Start recording", EUserInterfaceActionType::Button, FInputChord(EModifierKey::Control | EModifierKey::Shift, EKeys::R));
UI_COMMAND(StopRecording, "StopRecording", "Stop recording", EUserInterfaceActionType::Button, FInputChord(EModifierKey::Control | EModifierKey::Shift, EKeys::S));
}
#undef LOCTEXT_NAMESPACE
Discussion