Description
This is the component provided by the Keypad block.
Component name: os_keypad
.
This is the component provided by the Keypad block.
Component name: os_keypad
.
setEventName(name:string)
Sets the event name returned when you press a button, default is keypad
setDisplay(displayText:string[, textColor:number])
Sets a text to display with a textColor ( a minecraft color code (0-15))
setVolume(volume:number)
sets the keypad beep volume (expects double numbers as input)
getVolume()
returns the current volume of the keypad beep
setKey(customButtons:table, customButtonColor:table)
Sets the custom buttons and their colors this is the default order : customButtons = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#"}
This works the same as button labels, but it takes a color code to color the button label in that position. customButtonColor = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
keypad(address:string, button:number , button_label:string)
This signal is queued by the keypad when button pressed by a player.
address
is the address of the keypad
button
is the id of the button
button_label
is the label of the button
-- https://github.com/PC-Logix/OpenSecurity/wiki/Keypad
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