Mouse Events (386400)

How to return KeyCode.Mouse0 or KeyCode.Mouse1. Event.current return only keyboard events,Event.current.button returns 0 1 or 2 mouse button code,but this is not comfortable,are there any way to return exactly KeyCode.Mouse1 KeyCode.Mouse2 KeyCode.Mouse3 etc…?
Thanks :slight_smile:

And on the ninth day, God created documentation.

1 Like

As far as i can see Input.GetMouseButtonDown do not return KeyCode.Mouse1…Mouse2 etc… :?

No, but it IS how you check to see if that mouse button has been used that frame. :wink:

Why is it so important to use KeyCode.Mouse0, etc?

I write the GUI menu for change ingame input control config , and more comfortable immediately get KeyCode.Mouse1/2/3 without spare check.Like ;
while (!Input.anyKey){
yield;
}
if (Event.current.isMouse){
if ( Event.current.button==0)
pressedkey =KeyCode.Mouse0;
etc…
}
else if (Event.current.isKey)
pressedkey = Event.current.keyCode;

Well, I would file it as a bug report to UT - it does seem like they forgot to set that property when it’s a mouse event.

In the meantime, you seem to have the workaround.

YOu can use this method to get all keys there are.

public KeyCode FetchKeyOnGUI()
	{	
		if(Event.current.type == EventType.KeyDown)
		{
			Debug.Log("KEYDOWN");
			return Event.current.keyCode;	
			
		}
		else if(Event.current.type == EventType.MouseDown)
		{
			Debug.Log("MOUSEDOWN");
			return (KeyCode)((int)KeyCode.Mouse0 + Event.current.button);
			
		}
		else
		{
			return KeyCode.None;
		}
		
	}
1 Like

Heh, I very recently had this same problem. The GetKey() works on both keys and button events. all mouse click is button events where keyboard keys are key events. My fix to this was pretty simple;

if (Event.current.isMouse)
{
     Debug.Log((KeyCode)Enum.Parse(typeof(KeyCode), "mouse" + Event.current.button, true));
}

the GetKey(KeyCode.Mouse0-7) works fine if you don’t wanna use “GetButton(int)”

Hope this helps a little :slight_smile:

PS: Mind the casing, it may be wrong

1 Like

I’ve done something similar, “Mouse” needs to be capitalized and it’s fine.