-1

This way works but it strikes me as working too hard.

Question: How do you determine which key has been pressed?

;(function() {
    var Variables = {}
    Variables.slash = false
    $('[name=myName]').keypress(keypress)
    function keypress(myEvent) {
        if (myEvent.which === 47) {
            Variables.slash = true
        }
    }
    $('[name=myName]').keyup(keyup)
    function keyup(myEvent) {
        if (Variables.slash) {
           Variables.slash = false

        }
    }
})()
Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
  • You would like to know if the slash got pressed? Or whats the meaning ob the Variables-object and the slash property? – berkyl Jun 22 '15 at 07:28
  • Yes, this snippet looks like it's more complicated than it needs to be. I just need to know when the slash has been pressed. – Phillip Senn Jun 22 '15 at 07:29

3 Answers3

1

It can be simplified, by following steps:

1

The myEvent variable will contain the ASCII code of the key that have been pressed. The ASCII code of slash is 47. (See here)

Ahs N
  • 8,233
  • 1
  • 28
  • 33
0

From a previous question:

"Clear" JavaScript:

<script type="text/javascript">
        function myKeyPress(e){

            var keynum;

            if(window.event){ // IE                 
                keynum = e.keyCode;
            }
            else if(e.which){ // Netscape/Firefox/Opera                 
                keynum = e.which;
            }

            alert(String.fromCharCode(keynum));
        }
</script>


<form>
    <input type="text" onkeypress="return myKeyPress(event)" />
</form>

JQuery:

$(document).keypress(function(event){
    alert(String.fromCharCode(event.which)); 
 })
Community
  • 1
  • 1
berkyl
  • 431
  • 4
  • 20