Jump to content




using the keyboard in computercraft


13 replies to this topic

#1 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 01 April 2013 - 07: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

Posted Image

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!



#2 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 01 April 2013 - 07: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

#3 Mailmanq!

  • Members
  • 123 posts
  • LocationI am omnipresent... DUH

Posted 01 April 2013 - 10:14 AM

You should add something on the keys API in this

#4 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 01 April 2013 - 10:20 AM

How is this useful at all?

#5 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 01 April 2013 - 02:46 PM

View PostSammich Lord, on 01 April 2013 - 10:20 AM, said:

How is this useful at all?
for people new to the way computercraft works

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 April 2013 - 02:52 PM

View Postmailmanq, on 01 April 2013 - 10:14 AM, said:

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

#7 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 04 April 2013 - 06:02 AM

most tekkit players wont be able to run alot of programs, because 70% of programs use the function term.isColor

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 04 April 2013 - 04:42 PM

View PostPixelToast, on 04 April 2013 - 06:02 AM, said:

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 )


#9 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 04 April 2013 - 05: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

#10 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 04 April 2013 - 05:24 PM

View PostNeverCast, on 04 April 2013 - 05:03 PM, said:

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

#11 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 04 April 2013 - 05:39 PM

That was the etc, smart-arse :P

#12 TariqCrazymanTc0

  • Members
  • 9 posts

Posted 22 April 2013 - 01:10 AM

This looks really usefull to a noob like me and plus it shows all the Keys ID

#13 RiokuTheSlayer

  • Members
  • 8 posts
  • LocationIn a Superflat world making turtles play games.

Posted 24 April 2013 - 11:43 AM

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.

#14 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 24 April 2013 - 11:53 AM

View PostRiokuTheSlayer, on 24 April 2013 - 11:43 AM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users