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

raw key events laying over?

Started by murky white, 15 June 2013 - 12:46 PM
murky white #1
Posted 15 June 2013 - 02:46 PM
when i use os.pullEvent("key") in a program, it seems like the last key grabbed will also be entered into the shell when the program exits.

so if i run a program like this:
local ev, k = os.pullEvent("key")
print(k)

it will also type whatever key i press into the shell if it is a visible character. it doesn't seem to happen with arrow keys or return. is there any way to prevent this?
MysticT #2
Posted 15 June 2013 - 02:55 PM
Well, the problem is that there's a "char" event after the "key" event for printable keys. So, you are pulling the "key" event, but the corresponding "char" event is left in the queue, and the shell pulls it and writes it to the console. It shouldn't afect programs, since you will probably pull the char event too. So there's no need to fix this "problem".
GopherAtl #3
Posted 15 June 2013 - 03:09 PM
you can prevent those dangling chars from getting typed on program exit by adding this right before you exit the program:


os.queueEvent("dummy")
os.pullEvent()

The dummy event is added just to make sure there's something in the queue, otherwise it might end up waiting for a real event. If there's a char event waiting, the pullEvent will grab it; if there was not, it'll get the dummy.