What is key press prevention principle?

hello. This time, I am curious about the principle of the code, so I leave a question.
If you write the code like this, if you hold down the key, the rectangle will keep growing.

    if keyCode(escapeKey)
        exit = true;

    elseif keyCode(upKey) 
        R_size = R_size + randi(50, 1);

    elseif keyCode(downKey)
        R_size = R_size - randi(50, 1);
    end

However, if you write the code like this, even if you keep holding down the key, the rectangle grows only once and doesn’t grow any more. How is this possible?

    if keyIsDown == 0 % 키가 눌리지 않았을 경우
        keep = 1;
    end
    
    if keyCode(escapeKey)
        exit = true;

    elseif keyCode(upKey) && keep == 1
        R_size = R_size + randi(50, 1);
        keep = 0;

    elseif keyCode(downKey) && keep == 1
        R_size = R_size - randi(50, 1);
        keep = 0;
    end