IsKeyDown
This function returns whether the user is holding down the key associated with the given Enum/KeyCode
. It returns true if the specified key is pressed or false if it is not pressed.
This can be used to check if a specific key, such as the space bar, is being pressed. For example:
local UserInputService = game:GetService("UserInputService")
local spaceHeld = UserInputService:IsKeyDown(Enum.KeyCode.Space)
To retrieve a list of all keys pressed by the user, use the UserInputService/GetKeysPressed
function.
Since UserInputService
is client-side only, this function can only be used in a LocalScript
.
See also
UserInputType/IsGamepadButtonDown
- A similar event with a different use: To check if a givenEnum/KeyCode|button
on aEnum/UserInputType|gamepad
is pressed.
Parameters
Name | Type | Default | Description |
---|---|---|---|
|
The |
Returns
Return Type | Summary |
---|---|
Whether the specified key is being held down |
Code Samples
Special Action on Key Combo Press
This example uses the UserInputService/IsKeyDown
function to create different behaviors when a shift key is held down than when a shift key is not held down when user input UserInputBegan|begins
.
The local function IsShiftKeyDown() returns whether the left or right shift Enum/KeyCode/keys
are pressed.
local UserInputService = game:GetService("UserInputService")
-- Shift keys
local shiftKeyL = Enum.KeyCode.LeftShift
local shiftKeyR = Enum.KeyCode.RightShift
-- Return whether left or right shift keys are down
local function IsShiftKeyDown()
return UserInputService:IsKeyDown(shiftKeyL) or UserInputService:IsKeyDown(shiftKeyR)
end
-- Handle user input began differently depending on whether a shift key is pressed
local function Input(input, gameProcessedEvent)
if not IsShiftKeyDown() then
-- Normal input
else
-- Shift input
end
end
UserInputService.InputBegan:Connect(Input)