New Mouse Input Query Methods

Hello, developers. I just released some new API which should make grabbing mouse input easier for most purposes. Included in this change are 4 new methods to UserInputService that allow you to keep track of the buttons that are pressed, the location of the mouse, and the delta:

array<InputObject> 	UserInputService:GetMouseButtonsPressed()
	-- returns an array of active pressed down mousebuttons
	
bool 				UserInputService:IsMouseButtonDown(UserInputType mouseButton)
	-- returns a bool of whether or not the provided mouse button is pressed down.
	
Vector2				UserInputService:GetMouseLocation()
	-- returns the screen position of the mouse; topbar not taken into account
	
Vector2				UserInputService:GetMouseDelta()
	-- returns the change in the mouse's position since the previous frame
    -- Note: like the Delta property of an InputObject, this only works when the moue is locked. 

Thanks for reading, and be sure to give them a try. If you notice any problems with any of the API, or have any questions pertaining to them, please report them to me.

42 Likes

Ooh, wonderful! Glad that UserInputService is now able to handle the mouse’s position. Riddance’s the use of Player:GetMouse() in a lot more cases. Thanks for this!

2 Likes

still no built in mouse target ;(

Otherwise awesome tho!! :tada::confetti_ball::birthday::cake:

I’m sure they’ll get to target eventually, although it shouldn’t be too hard to work out the target from the position.

yeah, its just raycasting, but compare the 3-5 lines for the raycasting to the 2 words for mouse.target and you’ll see why it’s useful to have something like this

1 Like

Mouse.Target is kind of archaic because it uses mouse.TargetFilter to filter mouse rays, which can only be a single instance. It’s much more powerful if your TargetFilter can be a list, which is easy to implement with camera:ScreenPointToRay().

function getMouseTargetWithFilter(cursorPosition, filter)
	local ray = camera:ScreenPointToRay(cursorPosition.x, cursorPosition.y, 900)
	return workspace:FindPartOnRayWithIgnoreList(ray, filter)
end

If your code is well organized and modular, the difference between 3-5 lines and two words won’t matter because this should only appear in any of your code in one place in your entire game.

14 Likes

Yeah, most of it is my own laziness!
However most devs won’t switch to this whilst mouse.target is still supported and no better method available (for the same reason)

When can we expect to see :GetMouseWheelDelta()? It would be really advantageous to be able to determine how far the player has scrolled the mouse wheel and in what direction.

2 Likes

Why is it not taking into account the topbar space if we can now hide it in favor of the single hamburger icon???

1 Like

Not sure what you mean, but the top left of the top bar, or the true origin, is located at 0,0. The beginning of the in-game area is 0,36. (if you use GetMouseLocation)

1 Like

A great time to remind @TheGamer101 [to start fixing this] (http://devforum.roblox.com/t/changes-to-topbarenabled/29806/18).

4 Likes

I feel like this has an obvious answer, but what’s wrong with the current player mouse? It seems to work fine for me and isn’t deprecated or anything either? Are you just trying to make one standardized service for all the different inputs?

Well, we already had similar methods for grabbing keyboard and gamepad input, so we decided to include mouse input in that as well. So I suppose it’s some sort of standardization. Also afaik none of these methods would do anything to deprecate the Mouse object or pull people away from using it if they already do. They just add simpler ways to grab mouse input without having to set up hooks for the UserInputService Began, Changed and Ended events.

1 Like

Okay, that makes sense. Thanks for the reply!

I recently allowed inputs to pass through the top bar when it is disabled, so I think I will probably be able to do this soon. I really want to do it before my internship ends.

9 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.