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

event?

Started by CreeperWiz, 10 March 2015 - 11:21 PM
CreeperWiz #1
Posted 11 March 2015 - 12:21 AM
How do i use the pullevent thing for getting keyboard input! examples would be helpful.
Edited on 10 March 2015 - 11:35 PM
Lupus590 #2
Posted 11 March 2015 - 12:34 AM
It's very similar to the normal pull event if I recall correctly. the only difference is the normal one reacts to terminate events

for reference here is the bios.lua for CC 1.73 ~line 130

function os.pullEventRaw( sFilter )
	return coroutine.yield( sFilter )
end
function os.pullEvent( sFilter )
	local eventData = { os.pullEventRaw( sFilter ) }
	if eventData[1] == "terminate" then
		error( "Terminated", 0 )
	end
	return unpack( eventData )
end

reading the above code, I can't see why you can't use the same inputs/outputs that you would use for the normal pull event
Edited on 10 March 2015 - 11:36 PM
Quintuple Agent #3
Posted 11 March 2015 - 12:34 AM
Well, you would most likely be using os.pullEvent instead of os.pullEventRaw (however raw is almost exactly the same except it can catch the Terminate event)
But if you wanted to get a keyboard key you could go about it like so

local eve,key=os.pullEvent("key")
if key==keys.backspace then --Would trigger if the user hit the backspace key
doBackspaceStuff()
end
I would recommend looking at the keyAPI for the available key variables, or the key Event for a list of which keys return which numbers.

If you wanted to get a little more complex you could do

local eve,key=os.pullEvent()
if eve=="key" then  --Will trigger everytime the user presses a key
if key==keys.backspace then
  doBackspaceStuff()
else
  otherKeystuff()
end
elseif eve=="char" then  --Will trigger whenever the user presses a key that is printable (a-z,0-9,!@#$%,etc.)
print("I just pressed "..key)
end
They char event is thrown if you press a key that is printable, however the key event is always thrown.
Meaning, if I press X both a key and char event are created, but if i press backspace, only a key event is created.
Keep in mind that if you use pullEventRaw, terminate will go though any filter, so if you have os.pullEventRaw("key"), terminate will still pass as an event.
Edited on 10 March 2015 - 11:35 PM
CreeperWiz #4
Posted 11 March 2015 - 12:37 AM
Well, you would most likely be using os.pullEvent instead of os.pullEventRaw (however raw is almost exactly the same except it can catch the Terminate event)
But if you wanted to get a keyboard key you could go about it like so

local eve,key=os.pullEvent("key")
if key==keys.backspace then --Would trigger if the user hit the backspace key
doBackspaceStuff()
end
I would recommend looking at the keyAPI for the available key variables, or the key Event for a list of which keys return which numbers.

If you wanted to get a little more complex you could do

local eve,key=os.pullEvent()
if eve=="key" then  --Will trigger everytime the user presses a key
if key==keys.backspace then
  doBackspaceStuff()
else
  otherKeystuff()
end
elseif eve=="char" then  --Will trigger whenever the user presses a key that is printable (a-z,0-9,!@#$%,etc.)
print("I just pressed "..key)
end
They char event is thrown if you press a key that is printable, however the key event is always thrown.
Meaning, if I press X both a key and char event are created, but if i press backspace, only a key event is created.
Keep in mind that if you use pullEventRaw, terminate will go though any filter, so if you have os.pullEventRaw("key"), terminate will still pass as an event.
It's very similar to the normal pull event if I recall correctly. the only difference is the normal one reacts to terminate events

for reference here is the bios.lua for CC 1.73 ~line 130

function os.pullEventRaw( sFilter )
	return coroutine.yield( sFilter )
end
function os.pullEvent( sFilter )
	local eventData = { os.pullEventRaw( sFilter ) }
	if eventData[1] == "terminate" then
		error( "Terminated", 0 )
	end
	return unpack( eventData )
end

reading the above code, I can't see why you can't use the same inputs/outputs that you would use for the normal pull event
Thank you guys so much that was very helpful!!
Lupus590 #5
Posted 11 March 2015 - 12:40 AM
This may help too, http://www.computercraft.info/forums2/index.php?/topic/14683-event-basics/
CreeperWiz #6
Posted 13 March 2015 - 02:37 AM
Thank you.