views:

50

answers:

4

The following code reveals a div on a keypress event of '?' (191). Works perfectly with Google Chrome but refuses to work with Firefox. Any ideas?

$(document).keyup(function (e) {
  if(e.which == 16) isShift=false; }).keydown(function (e) {

  if(e.which == 16) isShift=true;

  if(e.which == 191 && isShift == true) {
    if ($('#keyboard-shortcut-menu').css('display') == 'none') {
      $('#keyboard-shortcut-menu').show();
    } else {
      $('#keyboard-shortcut-menu').hide();
    }

  return false;
}

UPDATE: Figured this one out. Firefox captures '?' char as 0 value. Check out my answer below.

+1  A: 

try with

e.keyCode == XX

oh, and you can use e.shiftKey to test if "shift" key is pressed

$(document).keypress(function(e){
  if(e.keyCode==191 && e.shiftKey)
    $('#keyboard-shortcut-menu').toggle();
  return false;
});
Tarentrulle
tnx, but still broken.
konzepz
fudgey
@fudgey page last changed 2 years ago... it works in FF
Tarentrulle
@Tarentrulle: yes, the page is 2 years old, but try the demo at the bottom while using Firefox, `keypress` + `keyCode` always shows 0.
fudgey
+1  A: 

i think you were missing a ; or } cos this works in FF:

$(function(){
    $(document).keyup(function (e) {
          if(e.which == 16) isShift=false;
    }).keydown(function (e) {

          if(e.which == 16) isShift=true;

          if(e.which == 191 && isShift == true) {
                if ($('#keyboard-shortcut-menu').css('display') == 'none') {
                      $('#keyboard-shortcut-menu').show();
                }
                  else {
                      $('#keyboard-shortcut-menu').hide();
                }

              return false;
            }
      });
  });

try the js fiddle here: http://jsfiddle.net/q3d6S/1/

Patricia
Thanks. I now see the problem. The fiddle you've added also breaks on Firefox. Probably a local error on my installation. I'll test it a bit more on that angle. Thanks!
konzepz
You could use `e.shiftKey` instead of using `e.which == 16`
fudgey
yea, if this code breaks, there is something wrong somewhere else, b/c i ran that in IE, FF and Chrome :)
Patricia
A: 

Try this (demo):

$(document).keyup(function(e){
    if (e.which == 191 && e.shiftKey == true) {
        $('#keyboard-shortcut-menu').toggle();
    }
});
fudgey
A: 

Okay, I got it. I ran this script on Firefox console:

$(document).keydown(function(e) {
  console.log(e.which);
});

It seems Firefox captures the '?' char as value 0, while Google Chrome captures it as 191.

Solved it by adding a conditional code:

if((e.which == 191 && isShift == true) || (e.which == 0 && isShift == true))

Thanks for all the refactoring tips.

konzepz