2

I have HTML5 app in which I use shortcut keys. In this app I have also chat. When I write in chat window then shortcut keys also fire up. How can I exclude shortcut keys in chat window?

This is how I use onkeydown:

 <body onkeydown="Gui.doKey(arguments[0] || window.event)">

Gui:

var Gui = { 
    doKey: function (event) { 
        var key = event.keyCode || event.charCode; 
        switch (key) { 
            case 83: //s 
                skip(); 
                break; 
            default: 
                console.log(key); 
        }
    }
}
Bartek Kosa
  • 842
  • 1
  • 14
  • 25

3 Answers3

1
var Gui = { 
    doKey: function (event) { 
        if(event.target == <your_div>) 
             return;
        var key = event.keyCode || event.charCode; 
        switch (key) { 
            case 83: //s 
                skip(); 
                break; 
            default: 
                console.log(key); 
        }
    }
}

See Difference between e.target and e.currentTarget

Community
  • 1
  • 1
Jazzepi
  • 5,259
  • 11
  • 55
  • 81
0

Add a class to the markup to determine, that keydowns need to be ignored for it. Make your Gui.doKey method check the source element. If it matches, return before doing anything else.

HTML:

<div class="ignore_keydown chat_window">...</div>

JS:

var Gui = {
    doKey : function (keyboardEvent) {
        // If the class contains the ignore selector, return immediately
        if (keyboardEvent.srcElement.className.indexOf('ignore_keydown') === -1) {
            return false;
        }
        // Original code...
    }
}
Kim Hogeling
  • 641
  • 6
  • 14
-1

Did you try the jquery :not() selector to exclude the div, the div I presume your chat is in? http://api.jquery.com/not-selector/

Kobbe
  • 316
  • 1
  • 13