If you just use a if condition without using a while loop to repeat the checking, whether the key is pressed is only controlled once in a tiny time frame. Afterwards, the if condition fails and the program moves on without ever looking again at the if. So, what you probably want to do is surrounding the if condition with:
while (!key_pressed(KEY_ESCAPE)) {
}
Now your if condition is checked again and again and again until you press the escape key.