This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

onKeyPress -> trapping keys

 
David Hall
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to capture when the user presses the space bar. I have the basics of the code worked out (see below); however, I have a slight problem. There's a text box on this page and I do NOT want my event to fire if they are currently typing in the text box. Any ideas? Thanks so much for your help!
<script type="text/javascript" language="JavaScript">
document.onkeypress=keypress;
function keypress(e) {

var tmp=0;

if (navigator.appName == "Microsoft Internet Explorer")
tmp = window.event.keyCode;
else
tmp = e.which;
// if space bar hit
if (tmp == 32) {
alert(tmp);
return;
}
}
</script>
 
Tim McDonald
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
I've just been investigating a simular 'key-press' for the enter key. Substitute the 'space' key and you should be in business.
Netscape browsers require you to "capture" the event before you can do anything with it where Internet Explorer does not. Also Netscape uses a little bit different way to pass the event to the function.
Here is some code that works in Netscape 4+, Netscape 6 and Internet Explorer 5 browsers. It detects When a key has been pressed and then displays the key and its ascii code. You will also notice that I place focus to the window after the alert, this is because Netscape 6 requires that so that you can detect the next key. Hope this helps and good luck.
 
Neil M
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add an onFocus() and onBlur() to your text field:
onFocus="cursorInField(true)" onBlur="cursorInField(false)"
then the function:
function cursorInField(isIt) {
spaceBarCaptureDisabled = isIt;
}
then in your capture key function, check for that variable:
if (!spaceBarCaptureDisabled) {
// run space bar commands here
}

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic