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

Calling events: 'key' and 'char'

Started by Engineer, 24 March 2013 - 05:23 AM
Engineer #1
Posted 24 March 2013 - 06:23 AM
Hello!

I have this code (much more, this is basic):

local evt = { os.pullEvent() }
if evt[1] == "char" then
   --do things with the char
elseif evt[1] == "key" then
   --do things with keys like enter, shift
end

If I push the button 'b', then it will theoretical use the char event. If it is not a char but a key then it will execute the key event right?
But to my testings with this code it will fire both events, is there a way to work around that?

Thanks in advance,

-Engineer
theoriginalbit #2
Posted 24 March 2013 - 06:28 AM
both events fire. however when you check to see if the 'key' pressed is enter or shift or whatever, the 'b' will not match, so the 'key' event for them will go ignored.

alternatively you can do this… but i really don't recommend it. just handle the 'key' event better.

if evt[1] == 'char' then
  os.pullEvent() -- takes the next event off the queue, which is the key event.
elseif ......... etc
Engineer #3
Posted 24 March 2013 - 07:18 AM
Banned for being helpfull.. oops wrong topic.. ( I mean this! )

But is this an idea, maybe its better then doing yours (probably not, but trying is good):

local evt, p1 = os.pullEvent()
if evt == "char" or evt == "key" then
	while true do
	if evt == "char" then
	 --do stuff
	   break
	 elseif evt == "key" then
	 -- do stuff
	 break
	 end
	 end
end

Cant test, need to walk the dog -.-
MysticT #4
Posted 24 March 2013 - 08:09 AM
No need to add anything, just handle the events correctly and it should work. Also, I think the key event fires before the char, so waiting for an event after char wouldn't work.