Key - Version 1.0.0

Key :keyboard:

Key Module Introduction: This module allows you to create/manage UsersInput using the new API.

:desktop_computer: API: Key - Utils

Get KeyModule: Key

If you have any feedback/question/suggestion feel free to let me know :grin:

Get Started


Creating Action

To create new user inputs we gonna first call .ConstructInput()

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

This will return a new InputAction that we can handle complety for inputs/Guibuttons


Connecting Inputs

Two type of connection can be set:

  • ConnectPressedEvent(callback : ( ) → ( ))
  • ConnectReleasedEvent(callback : ( ) → ( ))
        local ReplicatedStorage = game:GetService("ReplicatedStorage")
        local Key = require(ReplicatedStorage.Key)

        local PrintAction = Key.ConstructInput({
            InputContextName = "PrintAction", -- Name of the InputContext;
            Sink = false,
            Priority = 1000, -- Priority level
            AttachedGuiButton = {}; -- Gui button to bind with
            Enabled = true,
            Parent = nil, -- Nil = parented to key module
            Keybinds = {Enum.KeyCode.E},
            InputType = Enum.InputActionType.Bool -- input type
        })

        PrintAction:ConnectPressedEvent(function(...) 
            print("pressed")	
        end)

        PrintAction:ConnectReleasedEvent(function(...) 
            print("released")	
        end)

now when we press E pressed will be printed and when we release E released gonna be printed you can directly disconnect events see: “Disconnection”


Gui Buttons

Almost the same with Connection but at the “AttachedGuiButton” we gonna pass a TextButton

        local ReplicatedStorage = game:GetService("ReplicatedStorage")
        local Players = game:GetService("Players")

        local Buttons = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

        local Key = require(ReplicatedStorage.Key)

        local PrintAction = Key.ConstructInput({
            InputContextName = "PrintAction", -- Name of the InputContext;
            Sink = false,
            Priority = 1000, -- Priority level
            AttachedGuiButton = {Buttons.A}; -- Gui button to bind with
            Enabled = true,
            Parent = nil, -- Nil = parented to key module
            Keybinds = {Enum.KeyCode.E},
            InputType = Enum.InputActionType.Bool -- input type
        })

        PrintAction:ConnectPressedEvent(function(...) 
            print("pressed")	
        end)

        PrintAction:ConnectReleasedEvent(function(...) 
            print("released")	
        end)

now when we gonna click on Buttons A pressed will be fired and samefor released when we gonna release the click.


Fire


The action can call a :Fire() function that need a boolean value

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    PrintAction:Fire(true)

pressed will be printed


State

ActionState return a boolean

false = no input pressed
true = input pressed


Changing keys


No Gui Buttons

You can change keys during the runtime of the InputAction

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Players = game:GetService("Players")

    local Buttons = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    task.wait(5)

    PrintAction:ChangeKeys({Enum.KeyCode.A})

E will be disconnected and the key A will the new key
you can eventually insert multiple keys to create new one


Gui Buttons

Buttons can also be changed during runtime

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Players = game:GetService("Players")

    local Buttons = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {Buttons.A}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    task.wait(5)

    PrintAction:ChangeKeys({Enum.KeyCode.A},{Buttons.B}) -- Buttons A will replaced by Buttons B

if no gui buttons is added the provided keycodes will be connected the gui Button
ex:
PrintAction:ChangeKeys({Enum.KeyCode.A}) → will be connected to Buttons A too


Adding keys


No Gui Buttons

You can add keycodes during runtime of the action

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Players = game:GetService("Players")

    local Buttons = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    task.wait(5)

    PrintAction:AddKeys({Enum.KeyCode.A}) -- E and A will be connected to the pressed event

If you try to insert a already connected key you will receive a warn but other keys will still be added


Gui Buttons

You can add keycodes/GuiButtons during runtime of the action

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Players = game:GetService("Players")

    local Buttons = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")

    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {Buttons.A}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    task.wait(5)

    PrintAction:AddKeys({Enum.KeyCode.A},{Buttons.B})

Buttons B will be connected to the KeyCode A

Same case with :ChangeKeys() if no GuiButtons is passed the default Inserted Buttons will be connected

ex:
PrintAction:AddKeys({Enum.KeyCode.A}) → Button A will be connected to E and A


Removing keys

You can remove keys at any time of the runtime

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    PrintAction:RemoveKeys({Enum.KeyCode.E})
    --No more keys connected

Remove the keycode E from the action

If you try to remove a non connected key a warn will printed but we still remove others connected keys.


Disconnecting events

Multiple Disconnect Functions can be call


Disconnecting Pressed Event

Disconnecting the ConnectedPressedEvent

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)

    PrintAction:DisconnectPressedEvent()

this will instantly disconnect the .Pressed event of the action


Disconnecting Released Event

Disconnecting the ConnectReleasedEvent

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectReleasedEvent(function(...) 
        print("released")	
    end)

    PrintAction:DisconnectReleasedEvent()

this will instantly disconnect .Released event of the action


With Disconnect All Events

Disconnecting all provided events

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local Key = require(ReplicatedStorage.Key)

    local PrintAction = Key.ConstructInput({
        InputContextName = "PrintAction", -- Name of the InputContext;
        Sink = false,
        Priority = 1000, -- Priority level
        AttachedGuiButton = {}; -- Gui button to bind with
        Enabled = true,
        Parent = nil, -- Nil = parented to key module
        Keybinds = {Enum.KeyCode.E},
        InputType = Enum.InputActionType.Bool -- input type
    })

    PrintAction:ConnectReleasedEvent(function(...) 
        print("released")	
    end)

    PrintAction:ConnectPressedEvent(function(...) 
        print("pressed")	
    end)


    PrintAction:DisconnectEvents()

.Pressed and .Released will be disconnected.


Properties of Key

KeyAction.__InputActionInstance / return the current InputContext Instance

KeyAction.__InputPressedSignalConnected / return a boolean if the pressed event is not connected yet

KeyAction.__InputReleasedSignalConnected / return a boolean if the released event is not connected yet

KeyAction.AttachedGuiButton / return a array of all provided GuiButtons during .ConstructInput() call


Info

I’m still working on this module more update will coming soon.


Version

VERSION : 1.0.0

3 Likes