1

This bit of code generates that error in Chrome's JavaScript console & I'm not sure why. After Googling around for people with similar issues, I haven't found any D:

The error message suggests that the issue is with the event.which() but I don't see how I'm using it any differently from expected, other people with similar don't seem to have had problem with it.

$(document).keypress(function(event) {
    switch (event.which()) {            
        case 38 :   keyNorthPressed = true;
                    break;
        case 39 :   keyEastPressed = true;
                    break;
        case 40 :   keySouthPressed = true;
                    break;
        case 41 :   keyWestPressed = true;
                    break;
    }
});

Thanks in advance.

gideonparanoid
  • 156
  • 1
  • 3
  • 12
  • 1
    The error tells you that `event.which` is not a function and a look at [**the documentation**](http://api.jquery.com/event.which/) confirms this. The examples show how to use it. – Felix Kling Feb 23 '13 at 13:12
  • Okay, I was under the impression a `switch` statement did not need a function necessarily, but that a value would work, is that not the case? – gideonparanoid Feb 23 '13 at 13:14
  • 1
    Of course, you can pass any value to `switch`. The problem has nothing to do with `switch`. You are trying to **call** `event.which`, but it is not a function. Just do `switch(event.which) {`. Other example: `var foo = {bar: 5};`. `foo.bar()` would throw the same error since `foo.bar` is a number, not a function. – Felix Kling Feb 23 '13 at 13:15
  • Right, that works. Thank you. I'm interested as to why it tries to call `event.which` rather than, as in the code, `event.which()` then? – gideonparanoid Feb 23 '13 at 13:18
  • I don't understand. Putting `()` after a symbol is always interpreted as a function call. If the symbol does not reference a function you get an error since only functions can be called. It's trying to call `event.which` because you told it to do so by adding `()` after `event.which`. – Felix Kling Feb 23 '13 at 13:19
  • Which function? `event.which` **is not** a function. It's a number. (like I already said in my first comment). And the error explicitly tells you: *"Property 'which' of object # is not a function"*. – Felix Kling Feb 23 '13 at 13:22
  • Okay, I think I understand now, I was assuming `event.which()` was a function. – gideonparanoid Feb 23 '13 at 13:23

1 Answers1

1

The answer here.

There are two possible ways two retrieve the event key code:

event.keyCode or event.which

Your code should be:

$(document).keypress(function(event) {
    var code = event.keyCode || event.which;
    switch(code) {       
        case 38 :   keyNorthPressed = true;
                    break;
        case 39 :   keyEastPressed = true;
                    break;
        case 40 :   keySouthPressed = true;
                    break;
        case 41 :   keyWestPressed = true;
                    break;
    }
});

event.keyCode and event.which are attributes, not methods, you can't call them with a ().

Community
  • 1
  • 1
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29