• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

onKeyPress -> trapping keys

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic