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

Yielding for a key press prints the key afterwards

Started by RadiationAlert, 03 June 2015 - 03:30 AM
RadiationAlert #1
Posted 03 June 2015 - 05:30 AM
I have a simple press designated key to continue. The program does just that but then types that key afterwards. It's very annoying when my program exits and prints a key after. Is there a way around this?
Creator #2
Posted 03 June 2015 - 05:44 AM
Can you post your code?
RadiationAlert #3
Posted 03 June 2015 - 05:46 AM

if (openModem ()) then
    print ("Press [S] to send data, or any other key to cancel");
    event, key = os.pullEvent ("key");
    if (key == keys.s) then
        rednet.send (ID, File, "TFP:");
        print ("Data sent successfully");
    else
        print ("Cancelled");
    end
end
After pressing the s key, it ends the program, which it should, then the s key comes up on the computer.

Not sure why that came up. Must have been a formatting error between my text editor and the online text box.
Edited on 03 June 2015 - 03:58 AM
Creator #4
Posted 03 June 2015 - 05:47 AM
Maybe in [code.] and [/code.] tags. Without the dots.
Bomb Bloke #5
Posted 03 June 2015 - 06:02 AM
When you press a character key, two events get queued - first a key event, then a char event. Your code pulls the key event but leaves the char event in the queue, so the shell ends up pulling and printing that.

There are a number of ways around this; by the looks of the code you've posted, you'll probably just want to clear the entire event queue before your script ends. For eg, add this to the end of it:

local myEvent = tostring({})  -- Generate a random event name.
os.queueEvent(myEvent)  -- Add it to the end of the queue.
os.pullEvent(myEvent)  -- Pull events from the front of the queue until the custom one is reached.
RadiationAlert #6
Posted 03 June 2015 - 06:04 AM
I actually read that on the wiki looking for answers and did not think about how the events key and char fire in order.

:: EDIT - That worked. Thanks!
Edited on 03 June 2015 - 04:06 AM