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

[SOLVED] Lua: Checking if input was Enter Key

Started by batchbat, 07 April 2015 - 02:09 AM
batchbat #1
Posted 07 April 2015 - 04:09 AM
Hi all,

I can not find how to do this anywhere, so I've decided to ask here. My code is meant to be a journal of sorts, where you can create and access logs. I wish to stop writing to the log after the ENTER key is pressed, but I've no idea how.

Here is the code in question:

done = false
print("1. Read log")
print("2. Create log")
print("3. List logs")
print("4. Exit")
while true do
	 event, key = os.pullEvent("char")
	 if key == "1" then
		  print("Enter log name:")
		  sFile = read()
		  print(file.read("logs/"..sFile, "a"))
	 return
	 elseif key =="2" then
		  print("Log Name:")
		  sFile = read()
		  print("Input ENTER to finish.")
		  repeat
		  input = read()
		  file.write("logs/"..sFile, "w", input)   --attempt to index ? (a nil value)
		  until done
		  return
	 elseif key == "3" then
		  shell.run("list", "logs")
	 return
	 elseif key == "4" then
		  shell.run("load")
	 else then
		  print("ERROR: Command not recognized")
	 end
end
The returned error is:

logs:28: attempt to index ? (a nil value)

I assume this means it is trying to index the enter key, which is why I wish to filter out the enter key from being written to the file. I assume it would be along the lines of

if input == key28 then   --key28 being Minecraft's enter key id
...
but this will not work. For this reason I turn to you; how is this done?

Thanks in advance!
Edited on 07 April 2015 - 03:42 PM
valithor #2
Posted 07 April 2015 - 05:42 AM
In your code you listen only for char events, while the enter key throws only a key event. Because of this the enter key is never being picked up. You will want to remove the filter from your os.pullEvent, and add another check in your if statements to see which event is being thrown.

For example


evt, key = os.pullEvent()
if evt == "char" and key == somekey then
  -- do stuff
elseif evt =="key" and key == keys.enter then
  -- enter key was pressed so do stuff
end

The error itself is coming from line 28, where you have a then following the else. While elseif requires a then, else does not.
Edited on 07 April 2015 - 03:44 AM
Dragon53535 #3
Posted 07 April 2015 - 04:12 PM
Your problem is that you're using the wrong functions for files.


file.write("logs/"..sFile, "w", input)
file.write does not exist. actually, file does not exist, and so trying to grab the index at write is giving you an error. Since you're trying to index a nil value.

what you seem to be trying to do is this:

local file = fs.open("logs/"..sFile,"w") --#Open in write mode
file.writeLine(input) --#Write input into the file
file.close() --#Close and save the file.



print(file.read("logs/"..sFile, "a"))
this is wrong as well

local file = fs.open("logs/"..sFiles,"r") --# Open it as read mode
local line = file.readLine() --# Grab the first line
file.close()
print(line)
batchbat #4
Posted 07 April 2015 - 05:41 PM
Wow, I had no idea I was so wrong. Thank you both for your help; using your modified code worked!