自製簡易jquery鍵盤監聽外掛程式

來源:互聯網
上載者:User

標籤:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><script>/** * 簡易鍵盤監聽事件 * @author shine.s */var KeyListener = (function(){var _debug = false;var _ups = {};var _mods = {‘shift‘: ‘shiftKey‘,‘ctrl‘: ‘ctrlKey‘,‘alt‘: ‘altKey‘,}var KEYS = {‘backspace‘: 8,        ‘tab‘: 9,        ‘enter‘: 13, ‘return‘: 13,        ‘pause‘: 19,        ‘caps‘: 20, ‘capslock‘: 20,        ‘escape‘: 27, ‘esc‘: 27,        ‘space‘: 32,        ‘pgup‘: 33, ‘pageup‘: 33,        ‘pgdown‘: 34, ‘pagedown‘: 34,        ‘end‘: 35,        ‘home‘: 36,        ‘ins‘: 45, ‘insert‘: 45,        ‘del‘: 46, ‘delete‘: 46,        ‘left‘: 37,        ‘up‘: 38,        ‘right‘: 39,        ‘down‘: 40,        ‘*‘: 106,        ‘+‘: 107, ‘plus‘: 107,        ‘-‘: 109, ‘minus‘: 109,        ‘;‘: 186,        ‘=‘: 187,        ‘,‘: 188,        ‘.‘: 190,        ‘/‘: 191,        ‘`‘: 192,        ‘[‘: 219,        ‘\\‘: 220,        ‘]‘: 221,        "‘": 222}var i;    //小鍵盤    for (i = 0; i < 10; i++) {        KEYS[‘num-‘ + i] = i + 96;    }    //數字    for (i = 0; i < 10; i++) {        KEYS[i.toString()] = i + 48;    }    // f1-f24    for (i = 1; i < 25; i++) {        KEYS[‘f‘ + i] = i + 111;    }    //字母    for (i = 65; i < 91; i++) {        KEYS[String.fromCharCode(i).toLowerCase()] = i;    }function addListener(keycode, callBack) {if(typeof(keycode) == ‘object‘) {$.each(keycode, function(k,v){_ups[k] = v;})} else {_ups[keycode] = callBack;}}function proccess(e) {keycode = e.which ? e.which : e.keyCode;if(_debug) {console.log("Now: "+keycode);}$.each(_ups, function(k,v) {var kArr = k.split(‘+‘);flag = true;if(kArr.length>1) {for(var i=0; i<kArr.length-1; i++) {var spec = kArr[i];if(_mods[spec]) {var flag = flag && e[_mods[spec]];}}var k = kArr[kArr.length-1];}if(KEYS[k] == ‘undefined‘) {return true;}if(flag && keycode == KEYS[k]) {return v();}})}if(_ups) {$(document).keyup(proccess);}function debug() {_debug = true;}return {debug: debug,addListener: addListener,};})();//test-demo$(function(){KeyListener.addListener(‘c‘, c);KeyListener.addListener({‘left‘: left,‘right‘: function(){right(1, 6);},‘shift+alt+f‘: d,‘shift+e‘: e,});function left() {console.log(‘test‘);}function right(a, b) {console.log(a+b);}function c() {console.log(‘c‘);}function d() {console.log(‘D‘);}function e() {console.log(‘e‘);}})</script>



自製簡易jquery鍵盤監聽外掛程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.