AreCapsLocked(); not exposed to Blueprints

Hi,

Building a game using Blueprints but also learning C++ at the same time. Can anybody tell me how to expose AreCapsLocked to Blueprint? It really important for my game to be able to know the current state of the Caps Lock, especially since it could be turned on before the player starts the game.

Thanks

Hi,

In order to get the current state of the Caps Lock, the AreCapsLocked() method has to be called through the current application instance. Although this instance is platform specific, Unreal Engine gives you the necessary API to check the Caps Lock state in a generic and cross-platform way. However, you would have to do a little bit of work to properly expose this method to Blueprints. Here’s a step by step guide on how to make this a static BlueprintPure node that can be accessed from any blueprint:

  1. To access the running application instance, you have to first enable Slate and SlateCore modules. Open your Code Editor, go to [YourProjectName].Build.cs and either uncomment or add this line at the end of the class

    PrivateDependencyModuleNames.AddRange(new string[] { “Slate”, “SlateCore” });

  2. We’re going to create a new static method within Blueprint Function Library class. You can skip to step#3 If you have already added one to your project. To create a Blueprint Function Library, go to the Editor, click on Add New > New C++ class > Scroll all the way down and select Blueprint Function Library as its parent. Let’s name it MyBlueprintFunctionLibrary (a better naming convention can be something like [YourProjectName]BlueprintFunctionLibrary).

  3. In the header file, create a new static method named AreCapsLocked (or anything that makes sense) and mark it with UFUNCTION specifier BlueprintPure:

    UFUNCTION(BlueprintPure, Category = "Utilities|InputEvent")
    

    static bool AreCapsLocked();

  4. In the .cpp file, include the necessary headers and implement the method as such:

    #include "MyBlueprintFunctionLibrary.h"
    

    #include “GenericPlatform/GenericApplication.h”
    #include “SlateApplication.h”

    bool UMyBlueprintFunctionLibrary::AreCapsLocked()
    {
    return FSlateApplication::Get().GetModifierKeys().AreCapsLocked();
    }
    There you go! Compile your code, go to any blueprint and type “CapsLock”. You should now be able to see and get this node.

226457-arecapslocked.png

Hope this helps.

~Vizgin

4 Likes

Just realised I never replied to this. Thank you so much for a well thought out and concise answer. I really appreciate it!

2 Likes