Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
MaximilianVINCENT

event.listen() not working as expected

Question

I've spent a few hours now trying to figure this one out, I need another pair of eyes, if you please.

 

In learning about how events work, I wrote a simple program: http://pastebin.com/HfBtgxhz
 

With a little play, I got it working so pressing the space bar will exit. Yeah! So I used the same set up in my current program (which will be posted when this last problem is fixed).

 

And nada, testing shows it does not run QuittingTime() when any key is pressed. I don't see a difference in the code.

 

Why does the first program work but the second program does not?

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

What I would like is something simple:

 

char = next char from keyboard buffer

if char == " " then quittingTime() end

 

or a background process that looks for the space bar being pressed and then runs quittingTime(). (Faster response time to user input, yeah!)

 

or understand how you meant for it to be done.

 

What I did do:

I tried os.sleep() and event.pull() right before if hasDisplay then updateScreen() end, near the end of my program.

 

os.sleep(0) does not work.

os.sleep(0.1) very rarely works. When it does work, it is many seconds after pressing a key or many keys that it runs quittingTime.

Skip this, going to event.pull directly.

 

event.pull(0.1,"key_up") is getting the enter key from starting the program, so that won't work.

event.pull(0,"key_down") does not work at all.

event.pull(0.1,"key_down") works very, very poorly.

event.pull(0.25,"key_down") works but loops around 3-6 more times before realizing I had hit a key.

event.pull(1,"key_down") works on the first try.

 

I do not like adding another second's delay to the main loop, this program is slow enough already (especially when pulling in cobblestone).

But it works, so 'oh well'. Thank you for the help, Sangar!

 

The way I see how this works:

event.pull(0, "key_down") goes though the table of signals with key_down as the key and returns its value. if not null or zero seconds have passed, get back to what I was doing. Obviously wrong, so I tried various ways to look at the event.pull function but all of them were wrong as well, print(string.dump(event.pull)) was just weird.

How do I dig deeper? I really want to understand how signals and events work (processes too, but later).

 

 

I have also learned the importance of event.ignore() at the end of your program. I had to restart the robot because every time I hit the space bar it would run through the quittingTime function, even while in the shell or running edit (probably because they were both always trying to get key_down events).  It seemed to work better outside of my program than in it!

Link to post
Share on other sites
  • 0

I don't see you actually reading any input anywhere else in your program, so I'd suggest replacing

  if hasDisplay then updateScreen() end
end

with

  if hasDisplay then updateScreen() end
  if select(4, event.pull(0, "key_down")) == keyboard.keys.space then
    quittingTime() -- minus the char check, so no args needed
    break
  end -- or even make the if condition the loop condition and call quittingTime after the loop
end

Which looks more cryptic than it is because of the select; that just trims the tuple to the elements 4 and after, so it basically gets the key code for the comparison. If you're more comfortable with assigning that to a variable first, that works as well, of course.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.