物品命令:/give @p opensecurity:keypad 64
一个带有按钮的键盘,按下按钮向与其连接的计算机发送事件。
程序
keypad = require("component").os_keypad
keypad.setEventName("eventName") -- 设置按下按钮时返回的事件名称,默认为“keypad"
keypad.setDisplay("displayText", optional int:textColor) -- 设置键盘的显示文本和可选的文本颜色代码(Minecraft颜色代码)
keypad.setVolume(0.5); -- 设置键盘蜂鸣音的音量 (最好是双精度数字)
keypad.getVolume(); -- 返回键盘蜂鸣音的当前音量
自定义标签和颜色
这是默认的顺序,可以更改其中任何一个以更改该位置的按钮标签。
customButtons = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"}
这与按钮标签的工作方式相同,但它采用颜色代码来为该位置的按钮标签着色。
customButtonColor = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
使用自定义的按钮和颜色设置键盘。
keypad.setKey(customButtons, customButtonColor)
事件
有关事件的更多信息,请参阅OpenComputers维基 => OpenComputers Event API
event = require("event")
eventName, address, button, button_label = event.pull("keypad")
print("key pressed: " .. button)
当键盘上的按钮被按下时,可以监听并处理这些事件。事件包含事件名称、地址、按下的按钮ID和按钮标签。
示例脚本
keypad = require("component").os_keypad
event = require("event")
local pin = "1234"
local keypadInput = ""
-- set this to true if you want to run the script as daemon
local runScriptInBackground = false
function updateDisplay()
local displayString = ""
for i=1,#keypadInput do
displayString = displayString .. "*"
end
keypad.setDisplay(displayString, 7)
end
function checkPin()
if keypadInput == pin then
keypad.setDisplay("granted", 2)
else
keypad.setDisplay("denied", 4)
end
keypadInput = ""
os.sleep(1)
end
function keypadEvent(eventName, address, button, button_label)
print("button pressed: " .. button_label)
if button_label == "*" then
-- remove last character from input cache
keypadInput = string.sub(keypadInput, 1, -2)
elseif button_label == "#" then
-- check the pin when the user confirmed the input
checkPin()
else
-- add key to input cache if none of the above action apply
keypadInput = keypadInput .. button_label
end
updateDisplay()
end
-- listen to keypad events
event.listen("keypad", keypadEvent)
-- clear keypad display
keypad.setDisplay("")
if not runScriptInBackground then
-- sleep until the user interrupts the program with CTRL + C
local stopMe = false
event.listen("interrupted", function() stopMe = true; end)
while not stopMe do os.sleep(0.1) end
-- ignore keypad events on exit
event.ignore("keypad", keypadEvent)
-- show that the keypad is inactive
keypad.setDisplay("inactive", 6)
end
资料分类: | 设备 |
最大叠加: | 64个 / 组 |