The problem is one of synchronization. Your code is read as:
when a key is pressed, set
isPressed
to oneshow the value of
isPressed
The the first instruction just says what will happen at a later point in time and it is completed. The second instruction follows immediately, before any keys are pressed, so it shows 0. If you were to do a third instruction:
window.setTimeout(function(){console.log(isPressed)}, 3000);
and press a key right after the page load (before 3 seconds pass), you will see a log with isPressed
set to 1.
If you have additional logic that needs to be executed after a key is pressed, you need to place it at the comment "Do your stuff here".