This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Beatsleigher's profile picture

Getting key events in CC 1.3

Started by Beatsleigher, 05 January 2014 - 01:31 AM
Beatsleigher #1
Posted 05 January 2014 - 02:31 AM
Hey there,
I'm currently working on an OS for my Tekkit Classic server, which users can optionally install and make full use of server programs, like email, chats, and other programs for example for checking out the rules, if they have to.

I'm currently working on the OS selection screen, where users can select whether they want to boot up to CraftOS or BeastOS (I know, it sounds like I'm a kid if you judge by the name, but the server's called CreepyBeast Tekkit, and CreepyOS sounds even weirder.).

I'm trying to use the keys API, but for some reason, I always get: startup:63: attempt to call index ? (a nil value)
and then the program exits.
I'm guessing this is because CC 1.3 doesn't have the constants found in the keys API.

How can I work around this and still get the keys? I'm looking at the keyboard layout, found on the wiki, but I'd like to be sure before I just frustrate myself.

This is the code I'm using:

-- BeastOS OS Selection
-- (c) Beatsleigher 2014

--[[ Disable Program Termination ]]--
os.pullEvent = os.pullEventRaw

--[[ Variables ]]--
local width, height = term.getSize()
local selectedIndex = 1

--[[ Handlers ]]--
local function BeastOS()
    print("Please wait. Booting BeastOS.")
end

local function CraftOS()

end

--[[ Tables ]]--
osSelection = {
    [1] = { text = "Boot BeastOS", handler = BeastOS },
    [2] = { text = "Boot CraftOS", handler = CraftOS }
 }

--[[ Print Methods ]]--
local function printCentre(_height, data)
    xPos = width / 2 - string.len(data) / 2
    term.setCursorPos(xPos, _height)
    print(data)
    term.setCursorPos(1, _height + 1)
end

local function printTopRight(data)
    xPos = width - string.len(data)
    term.setCursorPos(xPos, 1)
    print(data)
    term.setCursorPos(1, 2)
end

local function printHeader()
    printCentre(1, "BEASTOS OS SELECTION MENU")
    printTopRight("V1,0")
end


local function printMenu()
    curCursorHeight = 3

    for i = 1, #osSelection do
	    if osSelection[i] == osSelection[selectedIndex] then
		    printCentre(curCursorHeight, ">> "..osSelection[i].text.." <<")
		    curCursorHeight = curCursorHeight + 1
	    else
		    printCentre(curCursorHeight, osSelection[i].text)
		    curCursorHeight = curCursorHeight + 1
	    end
    end
end

--[[ Other Handlers ]]--
local function onKeyPressed(key, menu)
    if key == 28 then
	    onItemSelected(osSelection)
    elseif key == 200 then
	    if selectedIndex > 1 then
		    selectedIndex = selectedIndex - 1
	    end
    elseif key == 208 then
	    if selectedIndex < #osSelection then
		    selectedIndex = selectedIndex + 1
	    end
    end
end

local function onItemSelected(menu)
    menu[selectedIndex].handler()
end

--[[ Program Entry Point ]]--
term.clear()
printHeader()
printMenu()

event, key = os.pullEvent("key")
onKeyPressed(key, osSelection)



Thanks in advance for your help! :)/>
Edited on 05 January 2014 - 01:46 AM
tesla1889 #2
Posted 05 January 2014 - 04:47 AM
is this the exact code you're using?
because if so, it's complaining about
if key == 28 then
for some reason

also, wouldn't you want to wrap the last two lines in a while true loop?
LBPHacker #3
Posted 05 January 2014 - 06:55 AM
The problem is actually on line 64: onItemSelected is not defined. Localize it before defining onKeyPressed, define it after defining onKeyPressed.
-- localize onItemSelected so Lua will know what onItemSelected is while building the bytecode for onKeyPressed
local onItemSelected

local function onKeyPressed(...)
	-- code here
end

function onItemSelected(...)
	-- code here
end
Note that now the definition of onItemSelected mustn't localize it again.
Edited on 05 January 2014 - 05:58 AM
ElvishJerricco #4
Posted 05 January 2014 - 08:46 AM
The keys API was introduced in CC 1.4. You can copy the keys file from the rom of a modern version and put it on your computers and os.loadAPI on it.
LBPHacker #5
Posted 05 January 2014 - 09:26 AM
The keys API was introduced in CC 1.4. You can copy the keys file from the rom of a modern version and put it on your computers and os.loadAPI on it.
If the OP was right, the code would contain the "keys" word somewhere, since actually using the keys API is the only way to make it throw errors. The code does not contain the "keys" word, so it has nothing to do with the keys API. As I mentioned above, it's the undefined symbol "onItemSelected" that causes the error.
Edited on 05 January 2014 - 08:27 AM
ElvishJerricco #6
Posted 05 January 2014 - 10:47 AM
The keys API was introduced in CC 1.4. You can copy the keys file from the rom of a modern version and put it on your computers and os.loadAPI on it.
If the OP was right, the code would contain the "keys" word somewhere, since actually using the keys API is the only way to make it throw errors. The code does not contain the "keys" word, so it has nothing to do with the keys API. As I mentioned above, it's the undefined symbol "onItemSelected" that causes the error.

Right. I'm just letting him know how to get the keys API and why it's not there. As for the actual error, it's like you said: the parser for Lua doesn't have the local "onItemSelected" when the code calls it so it looks for the global.
LBPHacker #7
Posted 05 January 2014 - 10:55 AM
-snip-
Oh, okay. I thought you were trying to help the OP to fix the (non-existent) issue with the keys API. It was just a coincidence, I can see that now.