Check Keyboard Events in code

Hey,

I would like to handle Player Inputs inside the PlayerController.

    virtual void TickActor(float DeltaSeconds, ELevelTick TickType, FActorTickFunction& ThisTickFunction)
	{
		if (IsInputKeyDown(EKeys::A))
		{
			
		}
            if (WasInputKeyJustPressed(EKeys::LeftMouseButton))
		{

		}
       }

But somehow it doesn’t recognize any inputs.
I really would like to handle inputs inside the code.

Another question:
Is it a good idea to handle userinputs inside the PlayerController?

Yes, it’s okay to handle them inside the player controller.

Have you enabled input events?

Ok wasn’t sure, because as far as i know they are used/checkd in the default blueprint character via the Editor.

I’ve tried using EnableInput(this) in the PlaylerController, but it seems not to be the right way to do that.

right now i use:

Contoller::BeginPlay()
{
PrimaryActorTick.bCanEverTick = true;
EnableInput(this);
}

Controller::Tick()
{

if (IsInputKeyDown(EKeys::A))
.
.
or
,
,
if (WasInputKeyJustPressed(EKeys::LeftMouseButton))

}

The Tick() works, but the conditions not

EnableInputs() seems to work, but it still doesn’t recognize any keyboard inputs. ((WasInputKeyJustPressed(EKeys::A) is never true)

The correct way to do this would first be to add your input in your project settings.

Once you have that, you can add this in your code :

void Contoller::SetupInput()
{
        EnableInput(this)
	    check(InputComponent);
        InputComponent->BindAction("PressingA", IE_Released, this,   &Contoller::AReleased);
}

void Contoller::AReleased()
{
       //Do stuff here
}

Be sure to add the UFUNCTION() macro when declaring your function in your .h, or the Bind will not work.

Hope this helps!

Mick

Is it possible to enable all keyboard keys without adding them inside the editor?

I want to create a “controller settings menu” where the user can assign any key to different actions (move forward,backward etc), therefore I would have to add all the keys to the editor and write a function for every key.

Is there another way?

Yes, you can also do this :

InputComponent->BindKey(EKeys::A, IE_Released, this, &Contoller::AReleased);

Alternatively, you could create your own InputComponent and use the IsControllerKeyDown() function to do whatever you want!

but i would still have to add the key, like in the picture?

yes, you have to create it in the editor like in the pic, then connect the code to it.

so, if i want the user to be able to assign any key to a ingame action, i have to add all keys there? :o

No you don’t have too. Like I said, using this :

InputComponent->BindKey(EKeys::A, IE_Released, this, &Contoller::AReleased);

You don’t need to do what is in the picture.

But if you dont bind the key with BindKey, it doesn’t recognize they key right?

Like I said in my previous comment, if you don’t want to use the BindKey approach, the other solution would be to create a new InputComponent or too modify InputComponent so you have access to the IsControllerKeyDown().

At least that’s what I would try. I never needed to do what you intend to do.