简体   繁体   English

按下某个键时如何触发按钮?

[英]how to trigger a button when a certain key is pressed?

          ...
          <div className="wrap">
          <div id="tiles" className="columns">
          <div id="ti">
       <button id ="Q" className='drum-pad'>Q</button>
       <button className='drum-pad'>W</button>
       <button className='drum-pad'>A</button>
       <button className='drum-pad'>S</button>
       <button className='drum-pad'>D</button>
       <button className='drum-pad'>S</button>
       <button className='drum-pad'>Z</button>
       <button className='drum-pad'>X</button>
       <button className='drum-pad'>C</button>

       </div>
       </div>
         ...

Hello i would like to triger the first button when the Q key is pressed and so on with WASD etc, but i cant finc a way to do that can you help me please?您好,我想在按下 Q 键时触发第一个按钮,以此类推,使用 WASD 等,但我无法找到这样做的方法,您能帮帮我吗?

What you asking for is trigger a button when you press Q key.你要求的是当你按下 Q 键时触发一个按钮。 You could directly trigger a function when Q pressed instead of triggering a button but here is the code :您可以在按下 Q 而不是触发按钮时直接触发功能,但代码如下:

 var qkey = document.getElementById("Q"); //get the button element function triggerQ(){ //trigger a click on the button qkey.click(); } function log(){ console.log("Q pressed"); //log a message on console } window.addEventListener('keydown',function(event) { //check if Q pressed if (event.key == "q"){ triggerQ(); } });
 <input type="button" id="Q" value="Q" onclick="log()">

You might have an handler for the keydown event that when triggered will loop through all the available .drum-pad elements and compare the typed character against each one of them clicking which one has its innerText matching the pressed key:您可能有一个 keydown 事件的处理程序,当触发该事件时,它将遍历所有可用的.drum-pad元素,并将键入的字符与每个单击的字符进行比较,单击哪个字符的 innerText 与按下的键匹配:

 /** * This part only adds a click event handler to all the .drum-pad * that will print out on console which pad was just clicked * (the event gets fired both when the button is actually clicked with mouse * ..and when the corresponding character gets pressed) */ //when the page is loaded document.addEventListener('DOMContentLoaded', () =>{ //for each element having the drum-pad class document.querySelectorAll('.drum-pad').forEach((o, i)=>{ //adds the handler for the click event for the current element in the loop o.addEventListener('click', (event)=>{ //print out on console which pad triggered the click event console.log(`Clicked Pad: ${event.target.innerText}`); }); }); }); //when the event keydown gets fired on window (the page in the browser) window.addEventListener('keydown', function(event) { //get all the elements having the drum-pad class const pads = document.querySelectorAll('.drum-pad'); //for each on of them for(let pad of pads){ //get the innerText of the current element in the loop const keypad = pad.innerText; //if the pressed key is equal to the current element in the loop if (event.key.toLowerCase() == keypad.toLowerCase()){ //toggle the active status on the current button pad.classList.toggle('active'); //fire the click event on the element pad.click(); //wait 100ms and then toggle again the active status for the current button setTimeout( ()=>{pad.classList.toggle('active')}, 100); //exit the event handler return; } } })
 button:active, button.active { box-shadow: box-shadow: 7px 6px 28px 1px rgba(0, 0, 0, 0.24); transform: translateY(4px); }
 <button class='drum-pad'>Q</button> <button class='drum-pad'>W</button> <button class='drum-pad'>A</button> <button class='drum-pad'>S</button> <button class='drum-pad'>D</button> <button class='drum-pad'>S</button> <button class='drum-pad'>Z</button> <button class='drum-pad'>X</button> <button class='drum-pad'>C</button>

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

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