How to wait until keyboard input?

I’m trying to make a script that yields until the player gives an input. I’ve already programmed for when the player is mobile, but I want the player to press a keyboard key instead of pressing the mouse. Can anyone help?
Here is the code I’ve come up with so far:

local InputType
	if UserInputService.TouchEnabled then
		InputType = Player:GetMouse().Button1Down
	else
		InputType = UserInputService.InputBegan
	end
	InputType:Wait()

Any help will do, thanks!

1 Like
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
    repeat task.wait() until input.UserInputType == Enum.UserInputType.Keyboard
   -- code here
end)

If I use this instead of the function I’m currently using then it will not work on mobile. Any other ideas?

Couldn’t you do something like:

repeat
     local inputObject, gameProcessed = userInputService.InputBegan:Wait()
until inputObject.KeyCode == Enum.KeyCode.K or inputObject.UserInputType == Enum.UserInputType.Touch
2 Likes

Here is a simple localscript that waits until a key on the keyboard is pressed:

local userInputService = game:GetService("UserInputService")

repeat
    local input, gameProcessed = userInputService.InputBegan:Wait()
until input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed -- Game Processed is a variable that will be equal to true if the player is using the keyboard to type in the chat, or any other textbox. So if the player is typing in the chat, you might not want this event to succeed.

print("A player has pressed a key!")

Now, if you only want to stop yielding when the player has pressed a specific key, and just not any key in particular, then this block of code will yield until the player pressed ‘X’

local userInputService = game:GetService("UserInputService")

repeat
    local input, gameProcessed = userInputService.InputBegan:Wait()
until not gameProcessed and input.KeyCode == Enum.KeyCode.X -- Game Processed is a variable that will be equal to true if the player is using the keyboard to type in the chat, or any other textbox. So if the player is typing in the chat, you might not want this event to succeed.

print("A player has pressed the 'X' key!")

You could try to detect what device the player is playing on, and then use the function that is designed for computers whenever the player is on computer, or use the function that is designed for mobile users whenever the player is on mobile.

Here are plenty of topics that talk about how to detect what device a player is using

How to Detect what Device a Player is Using

How Would I Go About Detecting Player's Device?

The Most Accurate Way To Detect The Player's Device

Detecting If a player is on a mobile or PC device

Search results for 'How to detect what device a player is on' - DevForum | Roblox

4 Likes

I want to be able to use this, but is there a way that I can make a function that uses this if the player is on PC and another function that detects the players tap when the player is on mobile?

I already know decently well how to detect what device a players using but I feel that if I use this then it would be one type of input or the other.

What are you trying to do exactly?

This is going to be a completely mobile compatible game, and when the player is on mobile it will utilize the mouse as the input (because then the player will just have to tap the screen to continue)
And in the code I’ve come up with that side of it works, but now I need to figure out how to utilize something like this so that when the player is on pc they have to press a keyboard key.

local InputType
	if UserInputService.TouchEnabled then
		InputType = Player:GetMouse().Button1Down
	else	
	end
	InputType:Wait()

But if I use some of the functions above instead of this, then only the computer side of it will work and mobile users will not be able to progress.

Like this?

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local InputType
if UIS.TouchEnabled and not UIS.KeyboardEnabled then
	InputType  = Player:GetMouse().Button1Down
end
UIS.InputBegan:Connect(function(Input, gpe)
	if Input.KeyCode == Enum.KeyCode.E then
		--
	end
end)
local function waitForInputType(inputType)
	while true do
		local input, processed = UserInputService.InputBegan:Wait()
		if processed then continue end
		if input.UserInputType == inputType then break end
	end
end

if UserInputService.KeyboardEnabled then
	waitForInputType(Enum.UserInputType.Keyboard)
else
	waitForInputType(Enum.UserInputType.Touch)
end
2 Likes

This is perfect! Exactly what I’m looking for, thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.