KeyCode displaying as "Unknown"

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:
image

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	print(input.KeyCode)
end)
1 Like

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 :slight_smile:

1 Like

Above is right, but if you would like to continue using UIS (as roblox recommends), you can use input.UserInputType, which contains mouse actions.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	print(input.UserInputType)
end)

https://developer.roblox.com/en-us/api-reference/property/InputObject/UserInputType

3 Likes

I’ll have a go with both ways, thanks

1 Like

Using UserInputType only returns Keyboard if I press a button.

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)
1 Like

Using KeyCode.Name or UserInputType.Name also removes the Enum.KeyCode bit from the print statement.

1 Like

This is meant to happen, as the mouse input is not a KeyCode.

1 Like

Yes, I was just speculating, it’s not a complaint haha.

1 Like

I’ve rewritten it using your code and added some of my other bits into it and it works.

How would I be able to print the number of times it’s been pressed in the last 3 seconds for example, like Blender’s screencast?

I’ll try to look for something for this, please be patient - thanks

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)
2 Likes

pressed, lastPressed and amount are not members of input as a table.

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

Oh no, that was my first impression, but that code works, thanks a lot.

1 Like

Some input object like the mouse uses UserInputType and not KeyCode.

1 Like