<script type= "Text/javascript" > (function () {/** *dqkeys v1.0.0 | (c) 2016www.findme.wang * @params JSON keys monitor keys * @params bool Isorder keys are in order * @params function SUCFUC Completion key callback function * @params function CANCELFUC Complete the callback function after the key cancellation * @author Lidequan */var Dqkeys = Function (KEYS,ISORDER,SUCFUC,CANCELFUC) {//function body return new dqKeys.fn.init (KEYS,ISORDER,SUCFUC,CANCELFUC); } Dqkeys.fn = Dqkeys.prototype = {' Version ': ' 1.0.0 ',//version number ' author ': ' Lidequan ',//author ' Rightkeys ': {}, The listening key {Key:code},code is the ASCII code corresponding to the key ' Curkeys ': [],//The current pressed key ' SUCFUC ': null,//Completion key callback function ' Cancelfuc ': n Ull,//After completion of the key cancellation of the callback function ' Isfinsh ': false,//to determine whether to complete the key ' Isorder ': false,//keys have the corresponding order init:function (Keys,isorder , sucfuc,cancelfuc) {This.rightkeys=keys; THIS.SUCFUC=SUCFUC; THIS.CANCELFUC=CANCELFUC; This.isorder=isorder; return this; }, Listenkeys:function () {//monitor user keyboard operation Var _self=this; _self.addlistener (' keyDown ', function (oevent) {var oevent =oevent | | window.event;if (!_self.arraycontain (_self.curkeys,oevent.keycode)) {if (_self.isorder && _self.getnextkey () = = Oevent.keycode) {_self.curkeys.push (oevent.keycode);} else if (!_self.isorder) {_self.curkeys.push (Oevent.keycode);}} if (_self.checkresult (_self.rightkeys,_self.curkeys)) {if (_self.sucfuc &&!_self.isfinsh) {_self.sucfuc ();} _self.isfinsh=true; } }); _self.addlistener (' KeyUp ', function (oevent) {var oevent =oevent | | window.event;if (_self.checkresult (_self.rightkeys , _self.curkeys) && _self.isfinsh) {//Finish the key, and cancel the event if (_SELF.CANCELFUC) {_self.cancelfuc ();}} _self.curkey=_self.remove (_self.curkeys,oevent.keycode); _self.isfinsh=false; }); }, Arraycontain:function (arr,val) {//determines if an element is included in the array return (Arr.indexof (val) = =-1)? false:true;},checkresult:function (Json,arr) {//Determine if the user pressed all keystrokes for the listener (var i in JSON) {if (Arr.indexof (Json[i]) ==-1) {return false;}} Return True;},remove:function (Arr,val) {//from arrayRemove an element from var index = Arr.indexof (val); if (Index >-1) {Arr.splice (index, 1);} return Arr;},getnextkey:function () {//Get the ASCII code corresponding to the next key for the (Var i in This.right Keys) {if (This.curKeys.indexOf (This.rightkeys[i]) ==-1) {return this.rightkeys[i];}} Return Null;},addlistener:function (Ev,fn,bool) {//Event binding if (Document.attachevent) {document.attachevent (' on ') + EV, FN); }else{Document.addeventlistener (Ev,fn,bool); }}} DqKeys.fn.init.prototype = Dqkeys.fn; Window.dqkeys = window.$$= Dqkeys; })(); 1. Test Dqkeys ({' A ': +, ' B ': 66},true,function () {console.log (' okey ');},function () {console.log (' Cancel ');}). //2. test var dqkeys=new $$ ({' C ': Listenkeys, ' d ': 68},false,function () {console.log (' keys Down ');},function () { Console.log (' keys Cancel '); Dqkeys.listenkeys ();</script>
</pre><pre code_snippet_id= "1841482" snippet_file_name= "blog_20160820_4_218709" name= "code" class= " JavaScript ">
JS Combo key Event Monitor Plugin