I don't follow. What script trigger do you use, on what object?
Are those 3 lines the complete script that gets run by the trigger?
trap for a keyboard input like X or have a barcode with a payload of '000000' and if that is true Exit Script (true) or whatever other logic you need ot get out of the loop
All the three3 lines are attached to the trigger on the portal row BarcodeID with OnObjectExit.
Another option is to deal with the scanning the other way around.
It appears that you have a script that waits for the barcode to be scanned in. As an option, you could use a OnLayoutKeystroke layout trigger to initiate a script when the barcode reader reads a barcode. This script would then create a new record, if needed, insert the data from the reader and commit the record. This way, you are always ready to cash out.
Example file attached