Lua 中的 KeyPress 事件?

问题描述 投票:0回答:7

是否有可能让用户在 lua 上按下按键? 铁

while true do
    if keyPress(27)==true then
        print("You just pressed ESC")
    end
end
events lua keypress
7个回答
8
投票

Lua 以极端的可移植性为基础。因此,它基本上只提供 ANSI C 中可用的功能。 (我认为唯一的例外是动态链接,这是一种并非在所有平台上都可用的非 ANSI 功能,但它非常有用,以至于他们已经为很多人提供了它。)

ANSI C 不提供按键功能,所以默认的 Lua 库也不提供。

话虽如此,LuaRocks 存储库可能会将您带到具有此功能的库。例如,在 LuaRocks 页面上找到的 ltermbox 可能具有您需要的功能。 (注意,您可能必须删除不需要的部分。)可能还有其他可用的库。去挖吧。

否则,Lua 的全部point 就是可扩展性。它是一种可扩展的扩展语言。 手动滚动您自己的扩展 提供您想要的功能实际上并不是那么难。


3
投票

Lua 无货。可能有一个额外的图书馆。


3
投票

NTLua 项目中有对 getkey() 的绑定。你可以从那里获得一些资源。

(它只是包装了 getch())


3
投票

看起来你正在尝试制作游戏。对于 2D 游戏,您可能需要考虑 love2d。它看起来有点奇怪,但它确实有效并且与其他语言(例如 C)相比相对容易。


0
投票

第一件事是第一件事:如果您使用我的方法来执行此操作,则需要将您使用的脚本放在 LocalScript 中。不这样做会导致密钥不显示在控制台中(F9 查看控制台)。

好吧,现在我们知道它在 LocalScript 中,这是脚本:

local player = game.Players.LocalPlayer -- Gets the LocalPlayer
local mouse = player:GetMouse() -- Gets the player's mouse

mouse.KeyDown:connect(function(key) -- Gets mouse, then gets the keyboard
    if key:lower() == "e" or key:upper() == "E" then -- Checks for selected key (key:lower = lowercase keys, key:upper = uppercase keys)
        print('You pressed e') -- Prints the key pressed
    end -- Ends if statement
end) -- Ends function

如果您只想用一个键发出信号(仅小写字母或仅大写字母),请在下方查看。

仅限小写字母:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" then
        print('You pressed e')
    end
end)

仅大写:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "E" then
        print('You pressed E')
    end
end)

或者,如果您一般只想发出任意键的信号,您也可以这样做:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    print('You pressed '..key)
end)

我希望我能帮助回答你的问题。


0
投票

你必须使用 string.byte(io.read()):

虽然是真的 如果 string.byte(io.read()) == 27 那么 print("你刚刚按下了 ESC") 结尾 结束


-2
投票
if keypress=(29)==true then
print("hello")
end
© www.soinside.com 2019 - 2024. All rights reserved.