Jump to content




Trouble with events

help lua

5 replies to this topic

#1 modkip

  • New Members
  • 2 posts
  • Locationhell

Posted 02 August 2017 - 08:12 AM

Hi all

Im having some trouble with a script
There's a pice in it that gives the user a [Y/N] prompt,
but after the prompt it still types the character that's chosen.

local event,key = os.pullEvent("key")


if key == 21 then

somescript()

elseif key == 49 then

-- some code --
end


Function somescript()


write("id: ")

id = read() -- This is where the character shows up


-- some more code --



Is there some way to prevent the char-event from firing, or to prevent the character showing up when i use read?

Thanks in advance!

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 02 August 2017 - 11:12 AM

Sure, pull the char event instead of the key event. Then it'll be out of the queue when read starts.

#3 Codinget

  • Members
  • 4 posts

Posted 02 August 2017 - 11:14 AM

Hi

The reason read() gets the char is indeed the char event, so you should consume it before calling read()
You should probably also consume the key_up event

The code now looks like so:

local event, key=os.pullEvent("key")
function somescript()
  local a,b=os.pullEvent("char")
  a,b=os.pullEvent("key_up")
  write("id: ")
  id=read()
end
if key==21 then
  somescript()
elseif key==49 then
  --some code
end

What I did was just add two os.pullEvent() to get rid of the char event and the key_up.

--apparently, I was too slow

#4 modkip

  • New Members
  • 2 posts
  • Locationhell

Posted 02 August 2017 - 11:39 AM

Thanks guys, using a char event instead of a key event solved the issue, and its working great now!

#5 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 02 August 2017 - 11:52 AM

But I believe that there are keys that don't generate char event. Like esc.
EDIT: Make that ctrl. Forgot exit exits gui.

Edited by Wojbie, 02 August 2017 - 01:52 PM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 August 2017 - 01:24 PM

Esc doesn't generate a key event, either, but if you're only interested in y / n, then buttons such as down / shift / Ctrl / etc (which do generate key events without following char events) are of no concern.

Personally I'd go with something along the lines of Codinget's method, as this allows you to easily detect y / n regardless as to whether caps lock is on or not.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users