onKeyPress little problem (javascript)

I have this code which works Perfect. When a user presses the “P” key in the keyboard it triggers a message saying that you just pressed the P key:

function doKey($key) {
  if ($key == 112) {
     alert("You pressed key p" + $key);
  } 
}
-->
</script>
</head>
<body onKeyPress="doKey(window.event.keyCode)">
</body>

Now, what I want to do is simply remove the onKeyPress from the body tag, and try to move it outside the html and into the javascript code area. I want to have an unobstructive markup but I cant make it work. How would I move the

onKeyPress="doKey(window.event.keyCode)"

outside the body tag and still have it work?

Any ideas ?