Custom Mouse Module

Since the PlayerMouse is superseded, and usage of UserInputService is advised, it has been annoying to do stuff with the mouse. This module should help you with that. It wraps the mouse, replacing almost all of its features (stuff like PlayerMouse.Icon cannot be replaced for example), makes some features better (TargetFilter is a table!), and adds some additional functionalities. The module is here, the source code is here.

To get the mouse you have to use :GetMouse(). The returned object is singleton so shouldn’t be a problem to call it again.

local mouse = require(script.Parent.MouseModule):GetMouse()

Properties

  • Vector2 Position
    Position of mouse on screen

  • Vector2 Delta
    Change in screen position since last frame (similar to UIS:GetMouseDelta() which only works if mouse is locked in center)

  • CFrame Hit
    CFrame of mouse in 3D space, oriented towards mouse

  • Instance Target
    Object in 3D space the mouse is pointing at

  • Enum.NormalId TargetSurface
    Indicates the NormalId of the BasePart surface at which the mouse is pointing

  • Array TargetFilter
    Array (yay!) of objects ignored by the mouse when calculating mouse.Hit and mouse.Target, also has some helper methods listed below

  • Ray UnitRay
    A Ray directed towards the mouse’s world position, originating from the Camera 's world position

  • CFrame Origin
    A CFrame positioned at the Workspace.CurrentCamera and oriented toward the mouse`s 3D position.

  • Vector2 ViewPortSize
    Describes the width and the height of the game window in pixels

  • Int ViewPortSizeX
    Describes the width of the game window in pixels

  • Int ViewPortSizeY
    Describes the height of the game window in pixels

  • String Icon
    The content ID of the image used as the mouse’s icon

Event

  • RBXScriptSignal Button1Down
    Fired when the left mouse button is pressed. mouse.Position as parameter

  • RBXScriptSignal Button1Up
    Fires when the left mouse button is released. mouse.Position as parameter

  • RBXScriptSignal Button2Down
    Fired when the right mouse button is pressed. mouse.Position as parameter

  • RBXScriptSignal Button2Up
    Fires when the right mouse button is released. mouse.Position as parameter

  • RBXScriptSignal Button3Down
    Fired when the mouse wheel is pressed. mouse.Position as parameter

  • RBXScriptSignal Button3Up
    Fires when the mouse wheel is released. mouse.Position as parameter

  • RBXScriptSignal Move
    Fired when the mouse is moved.

  • RBXScriptSignal HitChanged
    Fires when the mouse.Hit property changes (very useful imo)

  • RBXScriptSignal WheelForward
    Fires when the mouse wheel is scrolled backwards

  • RBXScriptSignal WheelBackward
    Fires when the mouse wheel is scrolled forwards

Methods

  • RaycastResults Raycast(RaycastParams params, Int distance)
    Casts a ray with params as raycast parameters starting from the 3D version of the 2D position of the mouse (mouse.Position) towards the direction of the mouse distance long. params is by default {FilterDescendantsInstances = mouse.TargetFilter, FilterType = Enum.RaycastFilterType.Blacklist}.distance is by default 1000.

  • AddToFilter(Instance|Array|String a)
    If a is an instance, adds it to mouse.TargetFilter. If a is an array, adds all of its elements mouse.TargetFilter. If a is a string, it will be considered a tag for the CollectionService, and all objects tagged with a will be inserted (Note: objects tagged with a after calling this method are not automatically added and calling the method again is needed)

  • RemoveFromFilter(Instance|Array|String a)
    If a is an instance, removes it from mouse.TargetFilter. If a is an array, removes all of its elements from mouse.TargetFilter. If a is a string, it will be considered a tag for the CollectionService, and all objects tagged with a will be removed

  • IsInFilter(Instance a)
    Checks if a is in mouse.TargetFilter

  • Vector2 GetDelta()
    Returns the change in mouse.Position since last frame

  • CFrame GetVelocity()
    Returns the change in mouse.Hit since last frame

52 Likes

0_0 woah this is epic thanks now i will be cool!

3 Likes

Thank you! This is gonna make using the mouse much less annoying.

4 Likes

This is very handy, thank you for providing a simple mouse api.

Also there is a typo

2 Likes

Hey, what about adding a MouseClick event? I can do it myself like this, but it would be cool to have it there.

local mouse = mouseModule:GetMouse()

local mouseButtonDown

local mouseButtonUp

local LastDown = 0

mouseButtonDown = mouse.Button1Down:Connect(function()
	if mouseButtonUp then
		mouseButtonUp:Disconnect()
	end
	LastDown = time()
	mouseButtonUp = mouse.Button1Up:Connect(function()
		if LastDown >= time() - .25 then
			print("click")
			print(LastDown)
		end
	end)
end)
1 Like

Thanks for the suggestion! I assume you mean a “player held the mouse” event. I am actually in the process of adding that! I’m planning to add a couple of extra input events which would definitely be useful.

2 Likes

Wait, what do you mean by that? I meant mouseClick, a better way to detect CLICKS instead of down, or up and everything. Just like GUI Click Events

But how would that player held the mouse event work then?

Ah, I did actually rewrite all events including the Button1Up event, I used UIS for the re-implementation which should be good enough. With some testing they are perfect.

1 Like

Hm, I understand what you mean and I’m not forcing you to add a click event, I gave you the idea because mouse down and up might fire in different places, example, you hold down in the middle of the screen and let go over some CoreGui. Or a really long hold shouldn’t count as a click.

So this module didn’t use UIS for mouseDown and Up and stuff?

2 Likes