LuaU脚本(Roblox),如何使用脚本按下按键

一个示例可能是这样的

local E = game:GetService('UserInputService').SetKeyDown(Enum.KeyCode.E)

但是它当然不起作用,因为我不能仅仅使用这个东西让我的游戏自己按下E,所以需要更长的东西,如果您找到了解决方案,是否也能做一个可以将其按下的解决方案?

原文链接 https://stackoverflow.com/questions/69075785

点赞
stackoverflow用户16665626
stackoverflow用户16665626

输入只能在客户端上注册,因此您将需要在 LocalScript 中编写代码。有两个服务用于获取玩家的输入:-

该示例展示了如何使用 UserInputService 获取玩家的 LeftMouseButton 输入。

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        print("The left mouse button has been pressed!")
    end
end

UserInputService.InputBegan:Connect(onInputBegan)

该示例展示了如何使用 ContextActionService 将用户输入绑定到上下文操作。上下文是装备的工具,动作是重新装填某个武器。

local ContextActionService = game:GetService("ContextActionService")

local ACTION_RELOAD = "Reload"

local tool = script.Parent

local function handleAction(actionName, inputState, inputObject)
    if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
        print("Reloading!")
    end
end

tool.Equipped:Connect(function ()
    ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)

您应该查看维基页面。

2021-09-07 06:51:51
stackoverflow用户2860267
stackoverflow用户2860267

翻译

看起来你希望编写一个脚本来按下 E 键,但这是不可能的。

跟 Giant427 提供的示例一样,你可以将操作绑定到按键上,也可以手动调用与这些操作绑定的函数,但你无法编写一个脚本来触发键盘输入。

Markdown格式

## 翻译

看起来你希望编写一个脚本来按下 `E` 键,但这是不可能的。

跟 Giant427 提供的示例一样,你可以将操作绑定到按键上,也可以手动调用与这些操作绑定的函数,但你无法编写一个脚本来触发键盘输入。

## Markdown 格式
2021-09-11 00:01:03