Jump to content




Calling events: 'key' and 'char'


  • You cannot reply to this topic
3 replies to this topic

#1 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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 -.-

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users