Accessing Editor Keybindings in C++

Hello, I am working on an editor tool that will help validate Unreal projects for my team (a checklist of everything that needs to be correct for the project/levels/actors)

I want to be able to get the values of the editor keybindings, specifically “Save Current level”, to see if it is bound or not

A search of the API turns up UUnrealEdKeyBindings | Unreal Engine 5.6 Documentation | Epic Developer Community

which has an array of KeyBindings, if this is it it would be helpful for me to see an example of how to access it.

Hi,

Hope you’re doing well.

Try to use the UUnrealEdOptions in order to get UUnrealEdKeyBindings, so you can get the array and check it, e.g.:

void UCustomEditorExample::SimpleEditorKeyBindCheck()
{
	UUnrealEdOptions* Options = GetMutableDefault<UUnrealEdOptions>();
	if (Options)
	{
		UUnrealEdKeyBindings* KeyBind = Options->EditorKeyBindings;
		if (KeyBind)
		{	
			for (const FEditorKeyBinding Binding : KeyBind->KeyBindings)
			{
				UE_LOG(LogTemp, Log, TEXT("Command: %s, Key: %s"), *Binding.CommandName.ToString(), *Binding.Key.GetDisplayName().ToString());
			}
		}
	}
}

if it works, you can improve this function to a bool function which return if some command is bound, like: bool IsSaveLevelBound() const.

Hope that helps - take care!

1 Like

Thanks, That does appear to be the array I want to access
Right now however this line of code
UUnrealEdKeyBindings* KeyBind = Options->EditorKeyBindings;

is returning null when I try out the code, I do not have a lot of experience with C++ but If I can at least get the array I’ll be able to go from there to check what I need.

I see.

Try initialize it by hand, like:

if (!Options->EditorKeyBindings)
{
	Options->EditorKeyBindings = NewObject<class UUnrealEdKeyBindings>(Options, UUnrealEdKeyBindings::StaticClass(), NAME_None, RF_Transactional);
}
// for loop goes here.

Remember, this code is editor only, if you try runtime it won’t work.

With the array returning null and this line of code failing to compile, I went down another path.

unresolved external symbol "private: static class UClass * __cdecl UUnrealEdKeyBindings::GetPrivateStaticClass(void)" (?GetPrivateStaticClass@UUnrealEdKeyBindings@@CAPEAVUClass@@XZ) referenced in function "class UUnrealEdKeyBindings * __cdecl NewObject<class UUnrealEdKeyBindings>(class UObject *,class UClass const *,class FName,enum EObjectFlags,class UObject *,bool,struct FObjectInstancingGraph *,class UPackage *)" (??$NewObject@VUUnrealEdKeyBindings@@@@YAPEAVUUnrealEdKeyBindings@@PEAVUObject@@PEBVUClass@@VFName@@W4EObjectFlags@@0_NPEAUFObjectInstancingGraph@@PEAVUPackage@@@Z)

By loading up the “EditorKeyBindings.ini” file inside the “\AppData\Local\UnrealEngine\5.4\Saved\Config\WindowsEditor” folder I can check to see if the keybind has been overridden to none, the only catch is that this file is only updated on Editor exit so it can’t check it live, This will work for now.

bool UValidationBPLibrary::GetKeyBindings() {
	FString localPath = FPlatformProcess::UserSettingsDir();
	localPath.Append("UnrealEngine/");
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION == 4
	localPath.Append("5.4");
#endif
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION == 5
	localPath.Append("5.5");
#endif
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION == 6
	localPath.Append("5.6");
#endif
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION == 7
	localPath.Append("5.7");
#endif

	localPath.Append("/Saved/Config/WindowsEditor/EditorKeyBindings.ini");

	FString file = "";

	FFileHelper::LoadFileToString(file, *localPath);

	if (file != "") {
		UE_LOG(LogTemp, Warning, TEXT("%s loaded"), *FString(localPath));
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("Could not load %s"), *FString(localPath));
	}

	if (file.Contains("~Quote~LevelEditor~Quote~,~Quote~CommandName~Quote~:~Quote~Save~Quote~,~Quote~ChordIndex~Quote~:0,~Quote~Control~Quote~:false,~Quote~Alt~Quote~:false,~Quote~Shift~Quote~:false,~Quote~Command~Quote~:false,~Quote~Key~Quote~:~Quote~None~Quote~~CloseBracket~")) {
		return true;
	}
	else {
		return false;
	}
}

That’s great.

If it is working for your, that’s great!!

This code means you should add the module to the <your_project>Editor.Build.cs; As I’ve told you, that code is editor only.

I’m happy you solved your issue.

Take care.