I’m planning on making a screencast plugin, but I’ve come across an issue with UserInputService, where I print the KeyCode of the key pressed, but it displays as Unknown when I click any of the three mouse buttons:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
print(input.KeyCode)
end)
You should use something else for this. There is no keycode for any of the mouse’s buttons. Use player:GetMouse() on the client side to get their mouse, then work from there
Yes. That is because UserInputType doesn’t have specific keys.
If you want to get keys and the MouseButtons, you can do something like the following.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
print(input.KeyCode)
else
print(input.UserInputType)
end
end)
I came up with something like this. Note: You can probably shorten this/rewrite it in an easier way, but hey, it works!
I tried explaining what everything does, please try to also understand a code, as that will help you learn it.
local UserInputService = game:GetService("UserInputService")
local inputs = {}
local multipleInputDetection = 0.5 -- how long it should wait until it resets how much it was pressed
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if inputs.pressed == input.KeyCode and os.clock() < inputs.lastPressed then -- if it's the same input and the last input was not longer than 0.5 seconds ago
inputs.amount += 1 -- add a press
else
inputs.pressed = input.KeyCode -- else update the input
inputs.amount = 1 -- reset amounts it was pressed
end
inputs.lastPressed = os.clock() + multipleInputDetection -- update the last pressed time
print(inputs.pressed.Name.." x"..inputs.amount) -- print what was pressed and how much
else
if inputs.pressed == input.UserInputType and os.clock() < inputs.lastPressed then -- same as above, except with mouse inputs
inputs.amount += 1
else
inputs.pressed = input.UserInputType
inputs.amount = 1
end
inputs.lastPressed = os.clock() + multipleInputDetection
print(inputs.pressed.Name.." x"..inputs.amount)
end
end)
Is that an error you get? It’s fine for me.
If you mean whether it’ll error because they aren’t defined in the first place, when the code is run for the first time, it will run the else-block, where it’ll get defined