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 Preferred class is part of the Input package.

local PreferredInput = require(packages.Input).PreferredInput

Types

InputType

type InputType = "MouseKeyboard" | "Touch" | "Gamepad"

The InputType is just a string that is either "MouseKeyboard", "Touch", or "Gamepad".

Properties

Current

This item is read only and cannot be modified. Read Only
PreferredInput.Current: InputType

The current preferred InputType.

print(PreferredInput.Current)

Functions

Observe

PreferredInput.Observe(handler(preferredInputType) → ()) → () → ()

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)
end)

-- If/when desired, observer can be stopped by calling the returned function:
disconnect()
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)\nend)\n\n-- If/when desired, observer can be stopped by calling the returned function:\ndisconnect()\n```",
            "params": [
                {
                    "name": "handler",
                    "desc": "",
                    "lua_type": "(preferred: InputType) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 74,
                "path": "modules/input/PreferredInput.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Current",
            "desc": "The current preferred InputType.\n\n```lua\nprint(PreferredInput.Current)\n```",
            "lua_type": "InputType",
            "readonly": true,
            "source": {
                "line": 53,
                "path": "modules/input/PreferredInput.luau"
            }
        }
    ],
    "types": [
        {
            "name": "InputType",
            "desc": "The InputType is just a string that is either `\"MouseKeyboard\"`,\n`\"Touch\"`, or `\"Gamepad\"`.",
            "lua_type": "\"MouseKeyboard\" | \"Touch\" | \"Gamepad\"",
            "source": {
                "line": 14,
                "path": "modules/input/PreferredInput.luau"
            }
        }
    ],
    "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 Preferred class is part of the Input package.\n\n```lua\nlocal PreferredInput = require(packages.Input).PreferredInput\n```",
    "realm": [
        "Client"
    ],
    "source": {
        "line": 42,
        "path": "modules/input/PreferredInput.luau"
    }
}