Stop Propagation of Space-Key

Stop Propagation of Space-Key

Hi there,

as a little side-project, I want to make a search engine that behaves similar to the "Just Type"-Feature of Palm webOS: When the user presses a key somewhere on my site, a box scrolls in containing the typed letters and the search results.
In order to realize that, I bound a "keydown" event to $(document). I have but one problem: the "space" key. It is correctly recognized, the space is concatenated to the search term; but then, the site scrolls down (found out it's the default action when pressing space in the browser). How can I stop this pagescroll (and maybe other standard-actions) from happening, i.e. my event should recognize the keydown, but not the browser?
I tried event.stopPropagation() and event.stopImmediatePropagation(), but somehow it doesn't work. Event when I add "keyup" and "keypress" to the mix (code below). Can you help me out?

Thanks,
Chris

  1. var typeToSearchFrame = $("<div></div>").css({
  2.         "width":"98%",
  3.         "height":"98%",
  4.         "position":"absolute",
  5.         "z-index":999,
  6.         "left":"1%",
  7.         "top":"1%",
  8.         "-moz-border-radius":"10px",
  9.         "background-color":"rgba(120, 120, 120, 0.8)",
  10.         "display":"none"
  11.     }).appendTo($("body"));
  12.     var typeToSearchFrameSearchBox = $("<div></div>").css({
  13.         "width":"92%",
  14.         "margin-left":"1%",
  15.         "margin-top":"1%",
  16.         "padding":"1% 3%",
  17.         "-moz-border-radius":"10px",
  18.         "border":"1px solid black",
  19.         "font-size":"26px"
  20.     }).append($("<span></span>"));
  21.     typeToSearchFrame.append(typeToSearchFrameSearchBox);
  22.     $(document).keydown(function(event) {
  23.         event.stopImmediatePropagation();
  24.         if (typeToSearchFrame.css("display") == "none" && event.keyCode >= 48 &&         
  25.         event.keyCode <= 90) {
  26.             typeToSearchFrame.slideDown("fast");
  27.             typeToSearchFrameSearchBox.find("span").text(
  28.                 typeToSearchFrameSearchBox.find("span").text() +  
  29.                 String.fromCharCode(event.keyCode));
  30.         } else if (typeToSearchFrame.css("display") == "block") {
  31.             if ((event.keyCode > 47 && event.keyCode < 90) || event.keyCode == 32) { // character
  32.                 typeToSearchFrameSearchBox.find("span").text(
  33.                     typeToSearchFrameSearchBox.find("span").text() + 
  34.                     String.fromCharCode(event.keyCode));
  35.                 } else if (event.keyCode == 8) { // backspace
  36.                     typeToSearchFrameSearchBox.find("span").text(
  37.                         typeToSearchFrameSearchBox.find("span").text().substring(0, 
  38.                         typeToSearchFrameSearchBox.find("span").text().length-1));
  39.                 } else if (event.keyCode ==  27) { // escape
  40.                     typeToSearchFrameSearchBox.find("span").text("");
  41.                     typeToSearchFrame.slideUp("fast");
  42.                 }
  43.             }
  44.     }).keyup(function(event) {
  45.         event.stopImmediatePropagation();
  46.     }).keypress(function(event) {
  47.         event.stopImmediatePropagation();
  48.     });