How to handle RMB and LMB clicks in c++

I need to branch based on if the RMB or LMB is clicked. What is the best way to do this in cpp. I have actions set up for rmb and lmb. One way I thought, would be to write 2 functions for each action that sets a private “is clicked” variable to true or false based on press or release. but that seems like a hassle. I notice that blueprint has a right mouse and left mouse pressed boolean, I was wondering if there is something similar in c++.

Thanks guys, let me know what the best way of handling this is.

Hi Kekeoki,

You can check if a key is pressed from UPlayerInput::IsPressed(FKey InKey) which returns a boolean.

Hope that helps. Cheers,

Hi Kekeoki.

Depending if you have a character or simply a player controller at hand, you want to make sure that

  • an input component is set

  • that you bind to it IE_Pressed and IE_Released actions to a function delegate.

  • that you also set up your input commands in your project settings.

The First person shooter tutorial on the Unreal wiki (A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums) will provide valuable information, especially the part on Adding Projectiles and Shooting which details how to handle LMB (you can infer RMB handling from it).

If you only have a player controller, it’s a bit more tricky, but not too difficult. I shared my struggles with the issue at the end of this post (Player controller input handling - C++ Programming - Unreal Engine Forums). Read the Wiki article and then bind your delegates in the same manner after registering the input component.

Cheers.