# onKeyPress little problem (javascript)

**URL:** <https://forum.kirupa.com/t/onkeypress-little-problem-javascript/238692>\
**Category:** web dev\
**Created:** [September 13, 2007, 10:25pm UTC](https://forum.kirupa.com/t/onkeypress-little-problem-javascript/238692 "2007-09-13T22:25:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielKirupin](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@danielKirupin](https://forum.kirupa.com/u/danielKirupin)\
**Post date:** [September 13, 2007, 10:25pm UTC](https://forum.kirupa.com/t/onkeypress-little-problem-javascript/238692/1 "2007-09-13T22:25:36Z")

</div>

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:

```auto
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

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

```

outside the body tag and still have it work?

Any ideas ?
