Home  >  Article  >  Web Front-end  >  Operation function code for jquery shortcut dynamically binding keyboard events_jquery

Operation function code for jquery shortcut dynamically binding keyboard events_jquery

WBOY
WBOYOriginal
2016-05-16 17:19:47819browse
Copy code The code is as follows:

(function($)
{
$.extend ({

key_fn:[], //Storage function corresponding to bound characters
key_code:[], //Storage character
key_bind:function(ch,callback){
var KeyCode = {a:65,b:66,c:67,d:68,e:69,f:70,g:71,h:72,i:73,j:74,k:75,l: 76,m:77,n:78,o:79,p:80,q:81,r:82,s:83,t:84,u:85,v:86,w:87,x:88, y:89,z:90};

if(KeyCode.hasOwnProperty(ch)){
$.key_fn.push(callback);
$.key_code.push(ch);
//Only the first time you need to add an event
if($.key_fn.length == 1){

$(document).keypress(function(e){
var e = event || window.event;
var k = e.keyCode || e.which;

for(var i =0 ; i < $.key_fn.length ;i ){

// - 32 compatible with lowercase
if(k-32 == KeyCode[$.key_code[i]] || k == KeyCode[$.key_code[i]] ){
log(' pressed bound key ' k);
$.key_fn[i]();
break;
}
}

});
}
}else
{
alert('Binding events can only be letters');
}
}

});
})(jQuery);

can be used as follows:
Copy the code The code is as follows:

$. key_bind('f',set_table_full_screen);
$.key_bind('r',reloadthis);

Sometimes we need to add some shortcuts to the application, and write one below every time The code can be conveniently bound to a certain keyboard and the corresponding operation function through key_bin.
Copy code The code is as follows:

$(document).keypress(function(){} )
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn