- how to use it
- How We use it in computercraft
- why we should use it
Think of the keyboard as a array of numbers. when you press a key, it sends an ID to the computer and the computer turns it into what it is.
If you press the up arrow key, it will send the id 200 to your computer. your computer takes the 208, and says "hey, 208 is the down arrow key!" and act accordingly
2. How we use it in Computercraft
computercraft uses keyboard ids almost like a computer (shocker). to use it you must do this
1. pull the event
2. wait for the event
3.act how you tell it
a simple door code that will open when you press "spacebar"
while true do --infinite loop event = {os.pullEvent()} --pulls the all events if event[1] == "key" then --will wait for you to press do a event. it will then test the event if you press a key. if you press a key,it moves on in the code if event[2] == 57 then --if the key you pressed is 57, then it continue the code rs.setOutput("left", true) --opens the door on the left sleep(2) --waits 2 seconds rs.setOutput("left", false) -- closes the door end end end -- ends the program
3. why we should use it
why should we use it? well, it is preference but normally a program will run smother if you dont have to type your thing, then press enter
Those are the basics of key events
Happy Coding!