Keystroke action

I am developing accounting software online. I would like to open a “new reporting document” whenever the user presses the N key, for example. And open the “settings” whenever he presses the S key.

I saw several scripts based on JavaScript and jQuery. But they did not work for sure. Can anybody help me?

I tried this script:

var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { //Enter keycode //Do something } 
+4
source share
4 answers
 $(document).bind('keyup', function(e){ if(e.which==78) { // "n" } if(e.which==83) { // "s" } }); 

To prevent input focusing:

 $("body").on("focus",":input", function(){ $(document).unbind('keyup'); }); $("body").on("blur",":input", function(){ $(document).bind('keyup', function(e){ etc.... }); 

You might want to include the bind function in your own function so as not to duplicate the code. eg:

 function bindKeyup(){ $(document).bind('keyup', function(e){ if(e.which==78) { // "n" } if(e.which==83) { // "s" } }); } $("body").on("focus",":input", function(){ $(document).unbind('keyup'); }); $("body").on("blur",":input", function(){ bindKeyup(); }); 
+4
source

You can cancel keystrokes in jQuery using . keypress () or .keyup () , here is a short example:

 $(document).keyup(function(event) { // the event variable contains the key pressed if(event.which == 78) { // N keycode //Do something } }); 

Here is a list of key codes: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

Update 1

.keyup and .keydown have different consequences - according to comments from @ThomasClayson -: keyup is the best one to use, because pressing a key will repeat if the key is held down. it logs an event for each character inserted. It also does not register modifier keys, such as a shift (although it is not necessary here, it may be something to keep in mind)

Update 2

This is from the dQ jQuery keyup site:

To determine which key was pressed, check the event object that is passed to the handler function. While browsers use different properties to store this information, jQuery normalizes. so that you can reliably use it to extract key code. This code corresponds to a key on the keyboard, including codes for special ones such as arrows.

Affectively means which.event is all you need to determine which key has been used. Thanks @nnnnnn

+2
source

You need to read the .keyCode() attribute of the .keyCode() object. You can interrogate this to find out which key was pressed and act accordingly. I also suggest that you add modifier keys to your shortcuts, such as Shift or Alt , so that when someone innocently types in the input, the panel does not appear. In the example below, I used Shift

 $(document).keyup(function(e) { if (e.shiftKey) { switch(e.keyCode ? e.keyCode : e.which) { case 78: // N pressed myNPressedHandler(); break; case 83: // S pressed mySPressedHandler(); break; } } } 
+1
source
 $(document).bind('keypress', function(e) { var keycode= (e.keyCode ? e.keyCode : e.which); if(keyCode==78) { // "n" }else if(keyCode==83) { // "s" } }); 
0
source

Source: https://habr.com/ru/post/1383408/


All Articles