I’m trying to figure out which mouse button is being pressed but I’m not able to get a hit when I poll for specific mouse buttons being pressed.
void AStrategyHUD::DrawHUD()
{
APlayerController* MyPC = GetOwningPlayerController();
FVector pos;
pos.Set(100, 20, 0);
if (MyPC->IsInputKeyDown(EKeys::SpaceBar)) <-gets triggered correctly, therefore the logic is sound and the syntax is right
{
DrawDebugString(GetWorld(), pos, "Spacebar Down");
}
if (MyPC->IsInputKeyDown(EKeys::LeftMouseButton)) <-never gets triggered, regardless of mouse state
{
DrawDebugString(GetWorld(), pos, "LMB Down");
}
if (MyPC->IsInputKeyDown(EKeys::RightMouseButton)) <-never gets triggered, regardless of mouse state
{
DrawDebugString(GetWorld(), pos, "RMB Down");
}
}
I added that in just to see if it was the missing problem. It was not.
I’ve tried adding the code in other classes as well with no luck anywhere. I’m totally stumped, so I’m currently downloading the full engine source code so that I can step through the functions using the debugger
I have a player controller which binds the action mappings to a function. For the sake of debugging purposes, I only care to know if the mapped function gets called. I have a breakpoint set within the called function to let me know if it got triggered. If I press the “1” button on the keyboard, the breakpoint is hit. This lets us know that the event is being fired correctly and is configured right. Since I also have the left mouse button bound to the same action mapping, clicking should fire the same event. It does not.
I’ve looked over this input documentation very carefully, paying particular attention to the input processing procedure. I’m pretty sure I don’t have an input enabled actor, so the next object to receive the input should be the player controller. Either something else is consuming the mouse click events before they get to the player controller, or the player controller is getting the mouse click events but the “IsInputKeyDown()” method is returning false when it should be returning true.
Here is the code for my player controller:
//Constructor:
AStrategyPlayerController::AStrategyPlayerController(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer), bIgnoreInput(false)
{
PrimaryActorTick.bCanEverTick = true;
bHidden = false;
bShowMouseCursor = true;
bEnableClickEvents = true;
}
void AStrategyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
InputHandler = ConstructObject<UGameInput>(UGameInput::StaticClass(), this);
}
void AStrategyPlayerController::ProcessPlayerInput(const float DeltaTime, const bool bGamePaused)
{
if (IsInputKeyDown(EKeys::RightMouseButton) || IsInputKeyDown(EKeys::LeftMouseButton)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::LeftMouseButton)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::RightMouseButton)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::MiddleMouseButton)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::ThumbMouseButton)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::ThumbMouseButton2)) //<--- doesn't trigger
{
int a = 0;
}
if (WasInputKeyJustPressed(EKeys::SpaceBar)) //<---sanity check: It triggers!
{
int a = 0;
}
Super::ProcessPlayerInput(DeltaTime, bGamePaused);
}
I didn’t know this, but if you enable “Project Settings -> Input -> Mouse Properties -> Use Mouse for Touch”, ALL mouse button events will be registered as TOUCH events, NOT mouse button events. This means that WasInputKeyJustPressed(EKeys::LeftMouseButton) will never fire because internally the mouse button events create an “EKeys::Touch1” event. The fix is to not use mouse for touch.
How did I figure this out?
I downloaded the entire engine source code, rebuilt the engine, added my project to the engine, and used the debugger to step into the engine input source code. I found that internally they maintain an KeyStateMap array which contains a list of every input detected during that frame and my mouse inputs were not showing up, but touch inputs were. In addition, I added break points for mouse clicks and found that they were being added as touch events instead of individual key events. It only took me a few seconds to figure out what the problem really was. I couldn’t have figured this out without the complete source code, so THANK YOU EPIC for releasing the full source!
For someone who has a future similar problems you could also have done “showdebug input” console command which will show you a debug version of what the key state map thinks it is.