简体   繁体   English

如何在按键按下时对多个按键进行编码? (JavaScript的)

[英]how to code multiple keys on keydown? (javascript)

I'm trying to have each key, correspond to different images when pressed. 我试图让每个按键在按下时对应于不同的图像。 (Ex: When "A" is pressed = image1 appears, When "S" is pressed = image2 appears, and so on.) (例如:按下“ A” =出现image1,按下“ S” =出现image2,依此类推。)

I was able to get the "S" Key to work, but not the A. I'm trying to do this for the whole row on the keyboard ("A","S","D","F","G","H","J","K","L") 我可以使用“ S”键,但不能使用A。我正在尝试对键盘上的整个行(“ A”,“ S”,“ D”,“ F”,“ G”, “H”, “J”, “K”, “L”)

*PS. * PS。 Im still a beginner :) 我还是一个初学者:)

This is my code: 这是我的代码:

<img src="images/giphy.gif" width="800" height="400" alt=""/>

<script>
  document.addEventListener("keydown", keyDownBody, false);

  function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==65) {
      myFunction();
    }
  }

function myFunction() {
var x = document.createElement("IMG");
x.setAttribute("src", "images/giphy1.gif")
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}

function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==83) {
      myFunction();
    }
  }

function myFunction() {
var x = document.createElement("IMG");
x.setAttribute("src", "images/sun.gif")
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}
</script>

You have declared the same function multiple times. 您已多次声明相同的函数。 You could merge them to make it work, and also make your code more readable. 您可以合并它们以使其工作,还可以使您的代码更具可读性。 An example: 一个例子:

document.addEventListener("keydown", keyDownBody, false);

function keyDownBody(e) {
  var keyCode = e.keyCode;
  switch (keyCode) {
    case 65:
        loadImage("images/giphy1.gif", "The Pulpit Rock");
        break;
    case 83:
        loadImage("images/sun.gif", "The Pulpit Rock");
        break;

    // Etc.
  }
}

function loadImage(uri, alt) {
    var x = document.createElement("IMG");
    x.setAttribute("src", uri)
    x.setAttribute("width", "800");
    x.setAttribute("height", "400");
    x.setAttribute("alt", alt);
    document.body.appendChild(x);
}

This way you can add as many keys as you wish. 这样,您可以根据需要添加任意多个键。

Alternatively, you could load the images dynamically if you put them in an array: 另外,如果将图像放入数组中,则可以动态加载图像:

var images = [{
      uri: 'images/giphy1.gif',
      alt: 'A',
    }, {
      uri: 'images/sun.gif',
      alt: 'Some text',
    }, 
    // Etc.
];

function keyDownBody(e) {
  var keyCode = e.keyCode;

  var index = keyCode - 65;
  if (typeof images[index] !== 'undefined')
  {
    var image = images[index];
    loadImage(image.uri, image.alt);
  }
}

 var keys_to_div = []; keys_to_div[65] = "1"; // A key keys_to_div[66] = "2"; // B key window.onkeydown = function(e) { if (keys_to_div[e.keyCode]) document.getElementById(keys_to_div[e.keyCode]).style.display = "block"; } window.onkeyup = function(e) { if (keys_to_div[e.keyCode]) document.getElementById(keys_to_div[e.keyCode]).style.display = "none"; } 
 img { width: 50px; height: 50px; display: none; } 
 <img src="http://images4.fanpop.com/image/photos/22100000/The-letter-A-the-alphabet-22186936-2560-2560.jpg" id="1"> <img src="http://www.drodd.com/images15/letter-b23.gif" id="2"> 

You just need to add more conditions 您只需要添加更多条件

function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==83) {
      myFunction("images/giphy1.gif");
    }

    if(keyCode==85) {
      myFunction("images/giphy2.gif");
    }

    if(keyCode==87) {
      myFunction("images/giphy3.gif");
    }
  }

function myFunction(imageName) {
  var x = document.createElement("IMG");
  x.setAttribute("src", imageName)
  x.setAttribute("width", "800");
  x.setAttribute("height", "400");
  x.setAttribute("alt", "The Pulpit Rock");
  document.body.appendChild(x);
}

Use same keydown EventListener to all the keys like 对所有键使用相同的keydown EventListener

document.addEventListener("keydown", keyDownBody, false);

  function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==65) {
      myFunction("images/giphy1.gif");
    }
    else if(keyCode==83) {
      myFunction("images/sun.gif");
    }
  }

function myFunction(t) {
var x = document.createElement("IMG");
x.setAttribute("src", t)
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM