用shiftkey获取keycode

时间:2014-06-04 05:44:07

标签: javascript jquery arrays

如果用shift键按下

,我想得到keyCode

代表。我按a然后按b。
然后我想将它存储在一个像[shiftkey,65,66]这样的数组中,如果有可能或者我错了,请建议我。

3 个答案:

答案 0 :(得分:1)

你可以试试这个桑杰:

<input id="down" type="text"/>


var your_array = [];

document.onkeydown = function (e) {
  var keyPress;

  if (typeof event !== 'undefined') {
    keyPress = this.value + String.fromCharCode(e.keyCode);
  }
  else if (e) {
    keyPress = e.which;
  }
    if(keyPress == '16'){
    keyPress = 'shift';
    }
  your_array.push(keyPress);
 alert(your_array);
    // returns [shift,65,66];
  return false;   // Prevents the default action
};

答案 1 :(得分:0)

    var a = [];
    var wait = true;
    window.onkeydown = function (e) {
        if (wait) {
            if (a[0] != e.keyCode) { a[0] = e.keyCode;  }
        }
        wait = false;
        window.addEventListener('keyup', key_up, false);
    }
    function key_up(e) {
        a[1] = e.keyCode;
        if (a[0] == a[1]) { a = []; }
        wait = true;
        window.removeEventListener('keyup', key_up, false);
        console.log(a);
        alert(a);
    }

答案 2 :(得分:0)

var pressedKeysSeries = [];
$(document).on('keyup', function (e) { pressedKeysSeries = []; });
$(document).on('keydown', function (e) {
    if (e.shiftKey && pressedKeysSeries.indexOf(e.key) === -1) {
        pressedKeysSeries.push(e.key);
    }
    console.log(pressedKeysSeries);
});