最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - Detecting key press sequence in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have above script, CheckFiddle or below

<script type="text/javascript">
    function check(e){
        var text = e.keyCode ? e.keyCode : e.charCode;
                 
         switch(text){
         case 81:
            text = '&#4632;';
            break;
        case 87:
            text = '&#4633;';
            break;
        case 69:
            text = '&#4634;';
            break;
        case 82:
            text = '&#4635;';
            break;
        case 84:
            text = '&#4636;';
            break;
        case 89:
            text = '&#4637;';
            break;
        case 85:
            text = '&#4638;';
            break;
}
    
    if(text == 8){
        
        var str = document.getElementById("out").innerHTML;
        var foo = str.substring(0, str.length -1);
        document.getElementById("out").innerHTML = foo; 
    }else {
        document.getElementById("out").innerHTML += text;
    }

    }
    
</script>
<input  type='text'  onkeyup='check(event);' id='in' />
    
<div id='out' ></div>

Which changes only some of the qwerty letters to another unicodes as they get typed. meaning, each letter gets converted to another letter, but the problem is, there are some letters that can only be created with a bination of two key strokes, together or separately. i.e.

  1. when you press m then quickly, o it should generate x;
  2. or when you press shift + p it, it should generate y

The problem, here is that the code only recognized one letter per stroke. I tried using:

if(text == 77+79){  // this is for m + o
text 'x';
}

or even for the shift + p which should output z. I the above argument it inside, but it is not working.

I have above script, CheckFiddle or below

<script type="text/javascript">
    function check(e){
        var text = e.keyCode ? e.keyCode : e.charCode;
                 
         switch(text){
         case 81:
            text = '&#4632;';
            break;
        case 87:
            text = '&#4633;';
            break;
        case 69:
            text = '&#4634;';
            break;
        case 82:
            text = '&#4635;';
            break;
        case 84:
            text = '&#4636;';
            break;
        case 89:
            text = '&#4637;';
            break;
        case 85:
            text = '&#4638;';
            break;
}
    
    if(text == 8){
        
        var str = document.getElementById("out").innerHTML;
        var foo = str.substring(0, str.length -1);
        document.getElementById("out").innerHTML = foo; 
    }else {
        document.getElementById("out").innerHTML += text;
    }

    }
    
</script>
<input  type='text'  onkeyup='check(event);' id='in' />
    
<div id='out' ></div>

Which changes only some of the qwerty letters to another unicodes as they get typed. meaning, each letter gets converted to another letter, but the problem is, there are some letters that can only be created with a bination of two key strokes, together or separately. i.e.

  1. when you press m then quickly, o it should generate x;
  2. or when you press shift + p it, it should generate y

The problem, here is that the code only recognized one letter per stroke. I tried using:

if(text == 77+79){  // this is for m + o
text 'x';
}

or even for the shift + p which should output z. I the above argument it inside, but it is not working.

Share Improve this question edited Apr 1, 2022 at 21:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 9, 2013 at 2:38 user1236473user1236473 2
  • 2 Already posted try this stackoverflow./questions/7479307/… – JDGuide Commented May 9, 2013 at 2:51
  • 1 Detecting a sequence of key presses (m is pressed and released, then o is pressed => x) would be different to detecting a key bination (shift + p simultaneously). How to do these two are very different questions. The latter seems already answered as @JDeveloper has pointed out; you should ask the former separately (it may already be answered too). – doppelgreener Commented May 9, 2013 at 2:55
Add a ment  | 

3 Answers 3

Reset to default 3

Sounds like you want to capture "abnormal" key bos. And for that, I think you'll need to trap and record keyup and keydown.

You want something like this, but not necessarily this exactly ...

var keysdown = {};
var lastkey = 0;

element.onkeyup = function(evt) {
  var e = evt || window.event;
  keysdown[e.keyCode ? e.keyCode : e.charCode] = true;
}

element.onkeyup = function(evt) {
  var e = evt || window.event;
  var code = e.keyCode ? e.keyCode : e.charCode;
  keysdown[code] = false;
  switch (code) {
    // for cases wherein you need to detect keyA + keyB
    case 77:
      if (keysdown[79]) {
        // x
      } else {
        // m
      }
      break;
    // for cases wherein you need to detect sequence A, B
    case B:
       if (lastkey == A) {
         // do A,B
       } else {
         // do B
       }
       break;
  }
  lastkey = code;
}

have you tried this?:

if(text == 77 && text == 79){
    text 'x';
}

In this example there are binations with 2 keys like ac and cd but you could have 3 or more binations like agk

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title>Example</title>
    <style type="text/css">
        td {
            width: 20px;
            height: 20px;
            text-align: center;
            vertical-align: middle;
        }
    </style>
    <script type="text/javascript" src="jquery-1.9.0.js"></script>
</head>

<body>

    <input type="text" />

    <script type="text/javascript">
        //IE 8 and below doesn't have addEventLisener but all other majon browser have it
        if (Element.prototype.addEventListener === undefined) {
            Element.prototype.addEventListener = function (eventName, callback) {
                var me = this;
                this.attachEvent("on" + eventName, function () {
                    callback.call(me, window.event);
                });
            }
        }
        var myApp = {
            multiChar: [
                [65, 67],//ac
                [67, 68] //cd
            ],
            prefChar: [0, 0], //Index of mutichar match
            replacers: ["مرحبا", "وداعا"], //replace multichar matches (ac with مرحبا) 
            checkCode: function (e) {
                var i = 0, inp;
                //IE probably doesn't have shiftkey or implements it differently
                if (e.shiftKey) {
                    //check stuff with shift
                    console.log("with shift", e.keyCode);
                    //If a match found then reset prefChar
                    prefChar = [0, 0];
                    return;
                }
                for (i = 0; i < myApp.multiChar.length; i++) {
                    if (e.keyCode !== myApp.multiChar[i][myApp.prefChar[i]]) {
                        myApp.prefChar[i] = (e.keyCode === myApp.multiChar[i][0]) ? 1 : 0
                        continue
                    }
                    myApp.prefChar[i]++;
                    if (myApp.prefChar[i] === myApp.multiChar[i].length) {
                        // found a multichar match
                        console.log(myApp.replacers[i]);
                        inp = document.body.getElementsByTagName("input")[0];
                        inp.value = inp.value.substr(
                            0, inp.value.length - myApp.multiChar[i].length) +
                            myApp.replacers[i];
                        myApp.prefChar[i] = 0;
                        return;
                    }
                }
            }
        }
        document.body.getElementsByTagName("input")[0]
            .addEventListener("keyup", myApp.checkCode);
    </script>


</body>

</html>
发布评论

评论列表(0)

  1. 暂无评论