Posted 14 May 2013 - 08:16 PM
Introduction:
Using events to wait for keystrokes can be useful in many situations, such as waiting for a user to type before display new text. In the next few paragraphs I will demonstrate how to make a code like this and provide a few examples
Setting up the code:
Here we will start with a blank program:
In order to start, we need to make an event:
Next we will setup a specific keystroke requirement:
Now lets fill this with code to show some of its uses.
You can also have a code to clear the screen after hitting a key:
How this code works:
In this code we call an event waiting for a key to be pressed. The code will wait for a key press then execute the next line of code. In these examples I used a code requiring you to press a certain key, but you can remove this to simply use a key to perform the next snippet of code.
Thanks,
BlankTitle
P.S. If you find any errors in this code please notify me!
Using events to wait for keystrokes can be useful in many situations, such as waiting for a user to type before display new text. In the next few paragraphs I will demonstrate how to make a code like this and provide a few examples
Setting up the code:
Here we will start with a blank program:
In order to start, we need to make an event:
local event, keystroke = os.pullEvent("char")
Next we will setup a specific keystroke requirement:
if keystroke == "q" then
--code here
else
--code here
end
Now lets fill this with code to show some of its uses.
print("Press Q to continue")
local event, keystroke = os.pullEvent("char")
if keystroke == "q" then
print("You pressed the q key")
else
print("You pressed the wrong key")
term.clear()
end
You can also have a code to clear the screen after hitting a key:
print("Press Q to erase data")
local event, keystroke = os.pullEvent("char")
if keystroke == "q" then
term.clear()
print("Data Erased")
os.shutdown()
else
print("Data Persists")
end
How this code works:
In this code we call an event waiting for a key to be pressed. The code will wait for a key press then execute the next line of code. In these examples I used a code requiring you to press a certain key, but you can remove this to simply use a key to perform the next snippet of code.
Thanks,
BlankTitle
P.S. If you find any errors in this code please notify me!