Changing threshold in a loop

Hello! I've already added two trackbars responsible for changing the lowest and highest possible threshold level in Canny's detecting edges algorithm. What's more, I'd like it to be able to read another pictures in the loop and adjust threshold levels for them aswell.

Here is the code (w/o edgedetecting function, it's simply grayscale->blur->Canny->blur->draw edges->fit the smallest rectangle):

https://pastebin.com/cmh8GUvh

Unfortunately, I've come across two hindrances:

1. (lines 57 - 72) Is the while-loop the only way to pass changing values of data.minTbV/data.maxTbV into the trackbar?
2. I do change pictures by pressing ESC (line 59), but it does not really work properly due to the time that is set inside of waitKey(). Is there any better way? Or how could I use right arrow for going forward and left for getting back?