This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Question on a command
Started by KPitt45, 24 March 2013 - 02:57 PMPosted 24 March 2013 - 03:57 PM
I started coding yesterday, so I am very new to it. I am trying to get a hang of it and wanted to know if there is any command that would exit the program you are in just by pressing enter. For context: I have been trying to make a database type program of different commads just on my ComputerCraft computer so I don't need to keep searching, so I have it so you start program "Command Database" then it asks for a term and uses "if then" and "elseifs" so if you search say "write" it will print "Commands: Print, Write, term.write. Surround following string with quotes in parenthesis." I have all that coded, however is there a command I could put into the code so when I press ENTER the program will end? Sorry for the lengthy post, just wanted to be as clear as possible. Thanks. ~KPitt
Posted 24 March 2013 - 04:03 PM
there is no code instruction that actually does it per-say, but we can make code to do it
there is a key binding of CTRL+T to terminate the program…
Making a listener (that listens for a given amount of time)
there is a key binding of CTRL+T to terminate the program…
Making a listener (that listens for a given amount of time)
local function listenForEnter( timeout )
os.startTimer( timeout )
while true do
local event, param = os.pullEvent()
if event == 'timer' then
return -- allows the program to keep running
elseif event == 'key' and param == keys.enter then
error() -- exits the running program
-- you could also do os.queueEvent('terminate') here, and it would quit the next time a read() or sleep, or anything that yields is used
end
end
end
Posted 24 March 2013 - 04:10 PM
Thanks, as I said, I'm very new at this and I've got the basics but I wasn't sure how to go about that. Thanks ~KPitt
Posted 24 March 2013 - 04:11 PM
no problems. :)/>
Posted 24 March 2013 - 04:19 PM
Now since I'm a noob, I don't know whether this is the actual code or if there are things I need to insert for myselft, sorry for any inconvenience. ~KPitt
there is no code instruction that actually does it per-say, but we can make code to do it
there is a key binding of CTRL+T to terminate the program…
Making a listener (that listens for a given amount of time)local function listenForEnter( timeout ) os.startTimer( timeout ) while true do local event, param = os.pullEvent() if event == 'timer' then return -- allows the program to keep running elseif event == 'key' and param == keys.enter then error() -- exits the running program -- you could also do os.queueEvent('terminate') here, and it would quit the next time a read() or sleep, or anything that yields is used end end end
Posted 24 March 2013 - 04:24 PM
can you post up your code in a pastebin link and I can tell you what you need to do to get it to work.
Posted 24 March 2013 - 04:40 PM
Ok I'm sorry the code is wicked short, and potentially flawed, as I said I started yesterday. lol. And I forget how to use pastebin. This is essentially what I have and its probably way off but I just started it and haven't worked out any bugs, lol… ANY :P/>
print("Welcome to the Commands Database")
local input = ""
term.write("Search Commans Here: ")
read() = input
if (input == "write") then
print("Commands: print, write, term.write. Followed by string in quotes in parenthesis.")
elseif (input == "loops") then
print("Commands: if/then/else/elseif, while/do.")
end
end
print("Welcome to the Commands Database")
local input = ""
term.write("Search Commans Here: ")
read() = input
if (input == "write") then
print("Commands: print, write, term.write. Followed by string in quotes in parenthesis.")
elseif (input == "loops") then
print("Commands: if/then/else/elseif, while/do.")
end
end
Posted 24 March 2013 - 04:52 PM
Oh thats a little harder to add one for enter, but you could just add a 'quit' if statement for exiting the program
Posted 24 March 2013 - 04:54 PM
sure anything, just curious
Posted 25 March 2013 - 09:08 AM
I figured out a way to make it so you press enter to begin a program. I trashed the old program and began making a flashcard one. When you start theprogram this runs:
local begin = "1"
while (begin ~= "") do
term.clear()
term.setCursorPos( 1, 1)
print("To Begin Press Enter")
begin = read()
end
That seems to work.
local begin = "1"
while (begin ~= "") do
term.clear()
term.setCursorPos( 1, 1)
print("To Begin Press Enter")
begin = read()
end
That seems to work.
Posted 25 March 2013 - 03:11 PM
hmmm interesting approach.
simpler approach, where only enter can be pressed.
EDIT: Oh also please use [code][/code] tags around your code :)/>
simpler approach, where only enter can be pressed.
while true do
local e, p = os.pullEventRaw('key')
if p == keys.enter then
break
end
end
EDIT: Oh also please use [code][/code] tags around your code :)/>