If I try DEBUG_VAR(on_esc,40); I get a number 207856.813
That's fine because we now know your on_esc pointer gets not overwritten/ set to NULL. The problem apparently is your keyboard or some other hardware/ driver issue. Have you tried to execute your code on some other PC (for example as an executable)?
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual
I have noted it works better when the computer is restarted, but seems to only work for short amount of time.. I guess im kinda re-leaved its not 3dgs :?