Unexpected inconsistencies with behaviour of a while loop in ImageJ Macro

I’m writing an ImageJ Macro to help with a complex tissue analysis. One function in this macro inputs an image with 3 existing non-overlapping ROIs, waits for the user to manually select a new ROI and confirm their selection by pressing the spacebar, and then works to detect which existing ROI the user’s new ROI overlaps to determine the new ROI’s name, then saves the ROI to measure its area and overlay it over the image.

For example, if the image’s 3 existing ROIs are ROI_1, ROI_2, and ROI_3, and the user’s new ROI overlaps with ROI_2, the new ROI should be saved as ROI_2_NC.

Below is my code, which works for one user-generated ROI.

while (true){ 
    if (isKeyDown("space") == true){ // user presses space after selecting an ROI

        roiManager("add");
        
        for (m = 0; m < 3; m++) { // cycle through the existing 3 ROIs in the image
            roiManager("Select", newArray(m,  roiManager("count")-1)); 
            roiManager("AND"); // look for overlaps
            if (selectionType() != -1) {
                roiManager("Deselect"); 
                lesionname = RoiManager.getName(m);
                roiManager("Select", roiManager("count")-1); // the newest ROI
                roiManager("Rename", lesionname + "_NC");
                roiManager("measure");
                Overlay.addSelection;
                break(); 
            }
            roiManager("Deselect"); // make sure we are starting again from scratch
        }
    }
    break();
    wait(100); // protects against multiple detection of single keystroke
}
print("done");

However, I want to allow the user to select as many ROIs as they wish in this image (hitting spacebar every time) until they choose to exit (by hitting ALT).

I’ve tried to add a requirement for ALT before the break(); command which exits the while loop, as below (new addition in BOLD):

while (true){ 
    if (isKeyDown("space") == true){ // user presses space after selecting an ROI

        roiManager("add");
        
        for (m = 0; m < 3; m++) { // cycle through the existing 3 ROIs in the image
            roiManager("Select", newArray(m,  roiManager("count")-1)); 
            roiManager("AND"); // look for overlaps
            if (selectionType() != -1) {
                roiManager("Deselect"); 
                lesionname = RoiManager.getName(m);
                roiManager("Select", roiManager("count")-1); // the newest ROI
                roiManager("Rename", lesionname + "_NC");
                roiManager("measure");
                Overlay.addSelection;
                break(); 
            }
            roiManager("Deselect"); // make sure we are starting again from scratch
        }
    }
        **if (isKeyDown("alt") == true) { 
        break(); // exits out of while loop
    }**
    wait(100); // protects against multiple detection of single keystroke
}
print("done");

The addition allows me to press spacebar multiple times, but has resulted in some unexpected inconsistencies in the earlier part of my code. Now when I press the spacebar, sometimes the newly-selected ROI gets added multiple times (and when this happens only the last ROI added gets renamed), and sometimes the new ROI either doesn’t get measured, gets measured multiple times, or isn’t added as an overlay. I think pressing the spacebar down longer makes things worse, but I have tried extending the wait time to wait(1000), which does not fully prevent these inconsistencies from occurring and makes the macro feel very laggy when using. I’ve also tried moving the wait command higher up in the code (under the roiManager(“add”); step or after the roiManager(“measure”); step), but this doesn’t seem to help either.

Based on the type of results I’ve been getting, it feels like the errors have to do with the code looping in unintended ways when I ask it to continuously detect the use of the spacebar. I think the computer is either repeating through a loop after one press of SPACE or skipping lines altogether, but I can’t figure out where the issue is and am not quite sure how to debug this.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật