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

using the keyboard in computercraft

Started by LuaEclipser, 01 April 2013 - 06:23 AM
LuaEclipser #1
Posted 01 April 2013 - 08:23 AM
in this tutorial, i will show you
  • how to use it
  • How We use it in computercraft
  • why we should use it
1. How to 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!
sjele #2
Posted 01 April 2013 - 08:29 AM
"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."

I belive you have a type in there. You start off with key id 200, but end with 208 :P/>
Mailmanq! #3
Posted 01 April 2013 - 11:14 AM
You should add something on the keys API in this
Sammich Lord #4
Posted 01 April 2013 - 11:20 AM
How is this useful at all?
LuaEclipser #5
Posted 01 April 2013 - 03:46 PM
How is this useful at all?
for people new to the way computercraft works
theoriginalbit #6
Posted 01 April 2013 - 03:52 PM
You should add something on the keys API in this
Keys API should be mentioned…… but not replace, as the keys api was only added in 1.4 iirc… so most Tekkit players don't have the api :P/>
PixelToast #7
Posted 04 April 2013 - 07:02 AM
most tekkit players wont be able to run alot of programs, because 70% of programs use the function term.isColor
theoriginalbit #8
Posted 04 April 2013 - 05:42 PM
most tekkit players wont be able to run alot of programs, because 70% of programs use the function term.isColor
Most I've seen use the

if term.isColor and term.isColor() then
I myself lately have become lazy and if I'm changing the colour just once or twice just do this

pcall( term.setTextColor, colors.blue )
NeverCast #9
Posted 04 April 2013 - 06:03 PM
I always initialize variables at the top of my program to prevent this

term.isColor = term.isColor or function() return false end
term.setTextColor = term.setTextColor or function() end
.. etc

Fixes all issues
Dlcruz129 #10
Posted 04 April 2013 - 06:24 PM
I always initialize variables at the top of my program to prevent this

term.isColor = term.isColor or function() return false end
term.setTextColor = term.setTextColor or function() end
.. etc

Fixes all issues

Except for term.setBackgroundColor() :P/>
NeverCast #11
Posted 04 April 2013 - 06:39 PM
That was the etc, smart-arse :P/>
TariqCrazymanTc0 #12
Posted 22 April 2013 - 02:10 AM
This looks really usefull to a noob like me and plus it shows all the Keys ID
RiokuTheSlayer #13
Posted 24 April 2013 - 12:43 PM
I thought you could just use the letters…like using:

local event,param1 =  os.pullEvent ("char")

and then use param1 as the variable(or string…..still learning the language) for the key,which can return as whatever button you press.
It uses this on the wiki: http://computercraft.info/wiki/Os.pullEvent

Unless that does something different,and i missed it.
Bubba #14
Posted 24 April 2013 - 12:53 PM
I thought you could just use the letters…like using:

local event,param1 =  os.pullEvent ("char")

and then use param1 as the variable(or string…..still learning the language) for the key,which can return as whatever button you press.
It uses this on the wiki: http://computercraft...ki/Os.pullEvent

Unless that does something different,and i missed it.

Essentially, two events ("char" and "key") are thrown on key presses so long as the key pressed is an actual character. But if it's a key such as ctrl, there is no char event thrown.