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 = 'መ';
break;
case 87:
text = 'ሙ';
break;
case 69:
text = 'ሚ';
break;
case 82:
text = 'ማ';
break;
case 84:
text = 'ሜ';
break;
case 89:
text = 'ም';
break;
case 85:
text = 'ሞ';
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.
- when you press m then quickly, o it should generate x;
- 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 = 'መ';
break;
case 87:
text = 'ሙ';
break;
case 69:
text = 'ሚ';
break;
case 82:
text = 'ማ';
break;
case 84:
text = 'ሜ';
break;
case 89:
text = 'ም';
break;
case 85:
text = 'ሞ';
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.
- when you press m then quickly, o it should generate x;
- 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, theno
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
3 Answers
Reset to default 3Sounds 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>