Skip to main content

PreferredInput

This item only works when running on the client. Client

A helper library for observing the preferred user input of the player. This is useful for determining what input schemes to use during gameplay. A player might switch from using a mouse to a gamepad mid-game, and it is important for the game to respond to this change.

The PreferredInput library is part of the Input package.

local PreferredInput = require(packages.Input).PreferredInput

Functions

observe

PreferredInput.observe(handler(preferredEnum.PreferredInput) → (() → ())?) → () → ()

Observes the preferred input. In other words, the handler function will be fired immediately, as well as any time the preferred input changes.

The returned function can be called to disconnect the observer.

local disconnect = PreferredInput.observe(function(preferred)
	-- Fires immediately & any time the preferred input changes
	print(preferred)

	return function()
		-- Preferred input has changed
	end
end)

-- If/when desired, observer can be stopped by calling the returned function:
disconnect()

get

PreferredInput.get() → Enum.PreferredInput

Gets the preferred input. This is equivalent to UserInputService.PreferredInput.

Show raw api
{
    "functions": [
        {
            "name": "observe",
            "desc": "Observes the preferred input. In other words, the handler function will\nbe fired immediately, as well as any time the preferred input changes.\n\nThe returned function can be called to disconnect the observer.\n\n```lua\nlocal disconnect = PreferredInput.observe(function(preferred)\n\t-- Fires immediately & any time the preferred input changes\n\tprint(preferred)\n\n\treturn function()\n\t\t-- Preferred input has changed\n\tend\nend)\n\n-- If/when desired, observer can be stopped by calling the returned function:\ndisconnect()\n```",
            "params": [
                {
                    "name": "handler",
                    "desc": "",
                    "lua_type": "(preferred: Enum.PreferredInput) -> (() -> ())?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 44,
                "path": "modules/input/PreferredInput.luau"
            }
        },
        {
            "name": "get",
            "desc": "Gets the preferred input. This is equivalent to\n[`UserInputService.PreferredInput`](https://create.roblox.com/docs/reference/engine/classes/UserInputService#PreferredInput).",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Enum.PreferredInput"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 52,
                "path": "modules/input/PreferredInput.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "PreferredInput",
    "desc": "A helper library for observing the preferred user input of the\nplayer. This is useful for determining what input schemes\nto use during gameplay. A player might switch from using\na mouse to a gamepad mid-game, and it is important for the\ngame to respond to this change.\n\nThe PreferredInput library is part of the Input package.\n\n```lua\nlocal PreferredInput = require(packages.Input).PreferredInput\n```",
    "realm": [
        "Client"
    ],
    "source": {
        "line": 19,
        "path": "modules/input/PreferredInput.luau"
    }
}