Hold Mouse Button vs. Tap Mouse Button

Is there a way that I can have one function tied to tapping the Left Mouse Button, but having a different action occur if the left mouse button is held?

For example, if a player taps the Left Mouse Button, their character will swing their sword. But if the player holds the mouse button, the character will kick their opponent. I know I could easily just switch that to another button, but I want to know how holding a button will work because it’s more practical for the mechanics I have in mind.

Use the release for tap and use press for hold.

That doesn’t work.

You will have to time iti think. Once the user presses the mousebutton, set a variable and increas it by delta time, check every frame if accumulated deltatime has reached your desired “hold time”, if yes, fire the hold button down action. On button release check if accumulated deltatime is less than desired “hold time”, if yes, fire the action user tapped the mouse button. I hope you get the gist of what i mean, sorry if little vague, but thats the only thing i could think of right now.

2 Likes

Why wouldn’t that work? When pressed make a bool that becomes true.
Make the bool false when release.

So now when is pressed make the bool true. Then add a delay. How ever long you want to check. Do a branch. Is the button release? (False) Then do tap logic. If it still true then do hold logic.

The original reply was splitting the actions to Pressed vs. Released.

This doesn’t work on its own because the “Pressed” Blueprints will try to resolve every time the button is pressed - regardless of whether it’s being held or not. So, if a person just taps or holds, that first bit of logic will play out on both occasions. Without any input buffering, it does not allow the Released logic to play out.

Adding a delay doesn’t account for, say, button mashing. If I make the hold for 1 second, an accidental double tap could trigger the hold. And even if it could, it would have to check that time regardless of whether you tap or hold the button because merely pressing it would trigger the check. So, there’d be a delay for any tap attacks added, as well.

Unreal really needs better input buffering support because the plugin on the Marketplace is not at all intuitive.

I think I understand what you’re saying. So pressing creates a variable, X, that tracks the time the button is held. Releasing checks that time - if it is less than X, trigger one set of actions. If it is greater, do the other action. im not 100% sure it’ll work, and having to do on-tick events for a combat action in a multiplayer PvP game hopefully doesn’t weigh the game down too much… haha

There is a function called ‘get input key time down’ it will return the time a key has been pressed. You can then use a float>float comparison to see if the input time is longer than the float threshold, which can drive a branch.

only thing is that this function will return 0 if you call it on the frame the key is released, so you need to set a float with the input key time down every frame, which is basically the same as adding the deltatime each frame like tiskan mentioned

I have tried to use GetInputKeyTimeDown to get LeftMouseButton, it dose not work. But when I double click the left mouse button it works. I mean if you click twice and hold the button at the second time you will get the result when you listening left mouse button.

When I new a project it works. Is there any node or setting would forbid the mouse pressed?

Finally, I have found the issue. In project setting - input, you should set default viewport mouse capture mode as “capture pernanently including initial mouse down” then you can catch mouse down event at first pressed left mouse button.

1 Like

Howdy! Got a little lost in your last comment. You might want to try doing it with timers, as it can give you more control about this issue.

So the main idea is you set a timer to check if you have pressed the key longer than an user-defined threshold (HoldingThreshold variable). If that is the case, this sets a different timer in charge of pulsing your action (Tick event below the first timer). Once you release the key, it checks if you were holding (second timer was set) and in case not, it launches the function assigned to “pressed”. Note that the first timer is supposed to launch only once, therefore the Looping bool is set to false.

I know it can be a little intrincate, but give it a try and let me know if it works for you.

1 Like

Here’s another different way of going about it that I made, it might not be the best for your case but it’s what I currently do for handling detecting if the lmb is held down long enough for box selecting units.

Both in tick, not really the best way but since it’s only on the player character I didn’t really care. I also use enums to handle alot of things since there’s so many different states the mouse can be in depending on if its used to build, select units, mark orders, etc.

I was looking for something like this, short long click detection, but found nothing so i made this macro!

Another version if you want to trigger long click as soon as it reach the threshold instead of when releasing key.
Can probably be simplified a bit!

It worked, thanks bro