How to use UserInputService in Plugins?

The title explains it self, I’ve tried using GetKeysDown it returns only one key being held, tried Input began parameter it returns only one.

Any help is really appreciated. (I’ll not use Plugin Action though)

My Code:

UIS.InputBegan:Connect(function(Input, Proccessed)
	for _, v in ipairs(GroupedItems) do

		if v.ShortCutKeyTable and v.OtherShortCutKeyTable then
			
			
			print(UIS:IsKeyDown(v.ShortCutKeyTable) , UIS:IsKeyDown(v.OtherShortCutKeyTable))
			if UIS:IsKeyDown(v.ShortCutKeyTable) and UIS:IsKeyDown(v.OtherShortCutKeyTable) then
				
			end


		end
	end
end)

Any help is appreciated.

I don’t know how to make plugins but since you helped me I’m helping you too. I’m trying something and I’ll be right back.

2 Likes

Thanks but It’s all good; you don’t need to help.

1 Like

The InputBegan event only fires for one input at a time, so you won’t be able to detect multiple keys being held down simultaneously using that event alone. Instead, you can use the InputService to keep track of which keys are currently being held down.

Here’s an example implementation using the InputService:

local InputService = game:GetService("UserInputService")

local heldKeys = {}

InputService.InputBegan:Connect(function(input, processed)
    if not processed then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            heldKeys[input.KeyCode] = true
        end
    end
end)

InputService.InputEnded:Connect(function(input, processed)
    if not processed then
        if input.UserInputType == Enum.UserInputType.Keyboard then
            heldKeys[input.KeyCode] = nil
        end
    end
end)

while true do
    if heldKeys[Enum.KeyCode.LeftControl] and heldKeys[Enum.KeyCode.A] then
        print("Control + A is being held down")
    end
    wait()
end

In this example, we’re using a heldKeys table to keep track of which keys are currently being held down. We update this table whenever a UserInputType of Keyboard is detected in the InputBegan and InputEnded events.

Then, in a loop, we check whether heldKeys contains the desired key combination (Enum.KeyCode.LeftControl and Enum.KeyCode.A in this case). If it does, we execute the desired action (printing a message to the console in this example).

Note that this loop should not be used in production code, as it can potentially cause performance issues. Instead, you should use this logic within a specific context where you need to detect the key combination.

2 Likes

Doesn’t look like you can use the user input service for plugins. Look into PluginActions:

1 Like

I’am getting my posts deleted or hidden for zero reason, for “giving out scripts” while I explained them at 100%…

1 Like

Thanks for the reply.

What I’m doing is creating a table with the user info so I can’t use UserInputService rather I wanna wait for an event for the user not use a while loop as it’s a bad practice and then I’ll check if the user actually pressed the buttons. The issue is detecting which keys they have pressed (it returns only once and returns other false)

local Test = {
{
TurnInto = "Model"
ShortCut = "E"
SecondShortCut = "F"
}
}

@ItsMeKlc As I’ve said I don’t want to use Plugin Actions as they require the user to create their own shortcuts (unless I’m missing something since I’m not familiar with Plugin Action) which is annoying. Rather I wanna detect their input themselves.

I see, in that case, you can use the UserInputService to detect key presses outside of a while loop. Here’s an example implementation that listens for a key press and then checks if the key combination matches the one you’re looking for:

local UserInputService = game:GetService("UserInputService")
local test = {
{
TurnInto = "Model",
ShortCut = Enum.KeyCode.E,
SecondShortCut = Enum.KeyCode.F,
},
}

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
-- Check if the input is a keyboard key press
if input.UserInputType == Enum.UserInputType.Keyboard then
    -- Check if the input matches the desired key combination
    for _, shortcut in ipairs(test) do
        if input.KeyCode == shortcut.ShortCut and UserInputService:IsKeyDown(shortcut.SecondShortCut) then
            print("Key combination detected: " .. shortcut.ShortCut.Name .. " + " .. shortcut.SecondShortCut.Name)
            -- Execute the desired action here
            return
        end
    end
end
end)

In this example, we’re using the InputBegan event to listen for a key press. When a key press is detected, we check if the input matches the desired key combination by looping through the test table. If the input matches the desired combination, we execute the desired action (printing a message to the console in this example).

Note that we’re also using the IsKeyDown method of the UserInputService to check if the second key in the combination is being held down. This allows us to detect key combinations where both keys are being held down simultaneously.

1 Like

Thanks! I’ll try it.

1 Like

Sadly it doesn’t work. Only returns the KeyCode not the IsKeyDown.

1 Like

@HolisThaDev it works Thanks! But one issue sometimes it doesn’t trigger.