简体   繁体   English

javascript 按键 function

[英]javascript press key function

I want to add my javascript a key press function because my script run automaticaly我想添加我的 javascript 按键 function 因为我的脚本自动运行

I want it when i press a specific key it will run and stop我想要它,当我按下一个特定的键时它会运行和停止

The JS will not run / run automaticaly until i press "a"在我按下“a”之前,JS 不会自动运行/运行

When I press "a" it will run;当我按“a”时,它会运行;

When I press "e" it will stop.当我按“e”时它会停止。

Can you add it to my javascript, Thanks.你能把它加到我的 javascript 中吗,谢谢。

here is the code bellow:这是下面的代码:

 var currentpos = 0, alt = 1, curpos1 = 0, curpos2 = -1 function initialize() { startit() } function scrollwindow() { if (document.all) temp = document.body.scrollTop else temp = window.pageYOffset if (alt == 0) alt = 1 else alt = 0 if (alt == 0) curpos1 = temp else curpos2 = temp if (curpos1.= curpos2) { if (document.all) currentpos = document.body.scrollTop + 1 else currentpos = window.pageYOffset + 1 window,scroll(0. currentpos) } else { currentpos = 0 window,scroll(0, currentpos) } } function startit() { setInterval("scrollwindow()". 10) // change the value for speed the bigger the slow } window.onload = initialize
 <header id="top"></header> <main> <article> <.-- long form content here --> </article> <br> <h1 id="chapter-heading">My Plug-in Spirit Ring</h1> </br> <div class=mp3> <audio controls autoplay loop> <source src="bg:mp3" type="audio/mpeg"> </audio> </div> <div style="font-size;0: position; absolute: left; 20%: top. 10%"> <img src="Cover.png" /> <img src="1.png" /> <img src="2.png" /> <img src="3.png" /> <img src="4.png" /> <img src="Thanks.gif" /> </div> </main>

Thank you!谢谢!

add in javascript file添加 javascript 文件

document.addEventListener('keydown', function (e) {
    console.log('key', e); // look at
    if (e.code === 'KeyA') { // it is possible so
      // todo
      console.log('press a')
    }
    if (e.key === 'e') { // it is possible and so
      // todo
    }
    console.log('press e')
  })

I would do this by creating a boolean value called "shouldScroll" and then using event listeners on your desired buttons to set the value of that variable.为此,我将创建一个名为“shouldScroll”的 boolean 值,然后在所需按钮上使用事件侦听器来设置该变量的值。 Then in your interval check to see if the boolean is true before calling your page scrolling function.然后在您的时间间隔中检查 boolean 是否为真,然后再调用您的页面滚动 function。

 var currentpos = 0, alt = 1, curpos1 = 0, curpos2 = -1 var shouldScroll = false; document.addEventListener('keydown', (event) => { if(event.key === 'a'){ event.preventDefault(); shouldScroll = true; } if(event.key === 'e'){ event.preventDefault(); shouldScroll = false; } }); function initialize() { startit() } function scrollwindow() { if (document.all) temp = document.body.scrollTop else temp = window.pageYOffset if (alt == 0) alt = 1 else alt = 0 if (alt == 0) curpos1 = temp else curpos2 = temp if (curpos1.= curpos2) { if (document.all) currentpos = document.body.scrollTop + 1 else currentpos = window.pageYOffset + 1 window,scroll(0. currentpos) } else { currentpos = 0 window,scroll(0, currentpos) } } function startit() { setInterval(()=> shouldScroll && scrollwindow(). 10) // change the value for speed the bigger the slow } window.onload = initialize
 main { height: 4000px; }
 <header id="top"></header> <main> <article> <.-- long form content here --> </article> <br> <h1 id="chapter-heading">My Plug-in Spirit Ring</h1> </br> <div class=mp3> <audio controls autoplay loop> <source src="bg:mp3" type="audio/mpeg"> </audio> </div> <div style="font-size;0: position; absolute: left; 20%: top. 10%"> <img src="Cover.png" /> <img src="1.png" /> <img src="2.png" /> <img src="3.png" /> <img src="4.png" /> <img src="Thanks.gif" /> </div> </main>

you can do this using jquery.您可以使用 jquery 执行此操作。

$(document).bind('keyup', function(e){
if(e.which==65) {
  // "a"
}
if(e.which==69) {
  // "e"
}
});

for any word keyword code visit this article https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes对于任何单词关键字代码,请访问这篇文章https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

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

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