keydown + keyup events for specific keys

后端 未结 2 405
小蘑菇
小蘑菇 2020-12-14 04:22

I\'m trying to make the background color change when certain keys are held down. For example, when the \'r\' key is being held down, the background should be red. When the \

2条回答
  •  再見小時候
    2020-12-14 04:41

    
    
    $().ready(function() {
      $('body').on("keyup keydown", function() {
        if(e.keyCode == 114 || e.keyCode = 121) {
          $(this).toggleClass("key" + e.keyCode)
        }
      })
    })
    
    
    

    Now just match the css rules with your css classes

提交回复
热议问题