Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What can i use for key pressing events?

I would have used mouse.Keydown and such but its now outdated and i need the newer tutorial. can someone please post a link in the comments or explain it to me?

2 answers

✓ Accepted answer

The replacement for KeyDown would be the InputBegan event of UserInputService.

How it works is whenever any type of input is used then the event will provide the function with the input information which is in a dictionary and if the event was game processed already. The input dictionary provides Delta, KeyCode, Position, UserInputState, and UserInputType. The information you would need for key pressing would be the KeyCode. The boolean commonly known as game processed will be true if say a user were typing in a text box or pressing a Roblox hotkey such as i, o, /, etc. If you plan on making a custom chat bar, I would recommend using TextBoxFocused and TextBoxFocusReleased events.

game:GetService('UserInputService').InputBegan:connect(function(Input,GameProcessed) --We're setting up the event to run a function each time the client hits a button.
    if Input.KeyCode --[[This is the dictionary, we are comparing the KeyCode information in the dictionary.]] == Enum.KeyCode.T then --So now, if the client hits t, the if statement will run.
        print('T was pressed!')
    end
end)

Just a note, this script must be in a LocalScript, otherwise it will not work. Modify it to your preferences.

######Hopefully this answered your question, if so hit the accept answer button. If you have any questions, feel free to comment below.

1 comment

0

Ty but what would i have to wirte for it to hold until release

You can use this,

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()

Open = false

function PressQ(key)-- Key that is fired when pressed. You can change it!
	if (key == "q") then -- Change this if you changed the Q key.
		if (Open == false) then

			Open = true
		elseif (Open == true) then

			Open = false
		end
	end
end

Mouse.KeyDown:connect(PressQ) -- Change this also if you changed the key.

2 comments

0

"I would have used mouse.Keydown and such but its now outdated and i need the newer tutorial." ~Asker

0

Oh my bad... Ill try to find somthing try :GetMouse()

This discussion is preserved as it was. New answers, comments, and votes are closed.