Part 1: Where to place the script block?
To capture the entire page, for example, like help-help (maybe you want to capture F1?), You put your script block in the <head> inside the script. But if you want to capture the DOM element, then you need to execute the code after the DOM element (because the script is interpreted as found, if the DOM element does not exist yet, the selection mechanism cannot find it. If it does not make sense to say something , and the article will be found).
But here’s something for you: Good javascript programmers today recommend downloading all javascript at the end of the page . The only things that you may need to download at the head of the document are libraries such as jQuery, because they are widely cached, especially if you use the jQuery version for CDN, since this usually does not affect the load time.
So this answers the question, “where do I put the code in <head> ?”: None. In the end.
Now, regarding the actual capture of a keystroke, do it in three parts:
Part 2: Capturing all keyboard events in a window:
<html> <head> <title>blah blah</title> <meta "woot, yay StackOverflow!"> </head> <body> <h1>all the headers</h1> <div>all the divs</div> <footer>All the ... ... footers?</footer> <script> function keyListener(event){ </script> </body> </html>
Part 3: Capturing all keyboard events for a specific item
This line: var el = window; //we identify the element we want to target a listener on var el = window; //we identify the element we want to target a listener on may also be var el = document.getElementByTagName('input'); or some other document selector. The example still works the same.
Part 4: the “elegant” solution
var KeypressFunctions = []; KeypressFunctions['T'.charCodeAt(0)] = function _keypressT() {
What does all this do?
KeypressFunctions is an array that we can fill with various values, but have them in a sense for human reading. Each index into the array is executed as 'T'.charCodeAt(0) , which gives the character code ( event.which || event.keyCode look familiar?) For the position of the index in the array for which we are adding a function. Thus, in this case, our array has only two defined index values, 84 (T) and 116 (t). We could write it as KeypressFunctions[84] = function ... but it is less readable by a person, at the expense of a human-readable person longer. Always write the code for yourself first, the car is often smarter than you give it credit. Do not try to beat it with logic, but do not code extra if-else blocks when you can be a little elegant.
<sub> r! I forgot to explain something else!
The reason for _keypressT and _keypressT is that when it is called as an anonymous function or as part of a freeze frame (it will be one day), you can define a function. It’s really a convenient practice to get used to, so that all potentially anonymous functions still get a “name”, even if they have their own name elsewhere. Once again, good javascript mentors offer things that help people ;-).
G! I forgot to explain something else!
Note that you can just as easily:
function doThing() //some pre-defined function before our code var KeypressFunctions = []; KeypressFunctions['T'.charCodeAt(0)] = doThing KeypressFunctions['t'.charCodeAt(0)] = doThing
and then doThing is executed for T or t. Please note that we just passed the function name and we did not try to run the doThing() function (this is a huge difference and a big hint if you are going to do such things)
I can’t believe that I forgot about it!
Part 5: jQuery:
Since today the emphasis is on jQuery, here is a block that you can put in your application after loading the jQuery library (head, body, footer, etc.):
<script> function doTheThingsOnKeypress(event){ //do things here! We've covered this before, but this time it simplified KeypressFunctions[event.which].call(); } $(document).on('keypress','selector',doTheThingsOnKeypress); // you could even pass arbitrary data to the keypress handler, if you wanted: $(document).on('keypress','selector',{/* arbitrary object here! */},doTheThingsOnKeypress); //this object is accessible through the event as data: event.data </script>
If you intend to use KeypressFunctions as before, make sure they are really defined before.