<script type="text/javascript"> document.body.onkeydown = function(e){ if (e.keyCode == 32) { // add your code here. console.log('Space pressed'); } } window.addEventListener('keydown', function(e) { if (e.keyCode == 32) { // add your code here. console.log('Space pressed2'); } }); </script>