简体   繁体   English

Javascript:按下键盘时触发按钮

[英]Javascript: triggering button on keydown

I'm building a musical e-learning thing, and wish to trigger an input button using the keyboard, which will play an assigned note. 我正在构建一个音乐电子学习工具,并希望使用键盘触发一个输入按钮,它将弹奏一个分配的音符。 Just like a piano! 就像钢琴一样! Except this one will make your ears bleed, but that's beside the point. 除了这一点,您的耳朵会流血,但这是重点。

The onclick() itself works when pressing the button manually, but binding to the keyboard is proving difficult. 手动按下按钮时,onclick()本身可以工作,但事实证明绑定到键盘很困难。

HTML: HTML:

<input type="button" id="pgC" value="C" onClick="pgC()">
<input type="button" id="pgCsharp" value="C#" onClick="pgCsharp()">

Script to bind to keyboard: 绑定到键盘的脚本:

$(function() {
$(document).keydown(function(e) {
    switch (e.keyCode) {
    case 65:
        $('#pgC')[0].onclick();
        break;
    case 87:
        $('#pgCsharp')[0].onclick();
        break;
    }
 });
});

Which I attempted to modify from this: Binding Keyboard to Onclick Event 我尝试从中进行以下修改:将键盘绑定到Onclick事件

My understanding is that $('#pgC')[0].onclick(); 我的理解是$('#pgC')[0].onclick(); should find the assigned id, and trigger the onclick based on which key is pressed, in this case a lower case "a". 应该找到分配的ID,并根据所按下的键(在这种情况下为小写的“ a”)触发onclick。

Any and all help is greatly appreciated! 任何帮助都将不胜感激! :D :D

Here is an answer that handles the simple case, but you need to take into account other stuff, eg what if a user wants to play a chord etc. 这是处理简单情况的答案,但是您需要考虑其他因素,例如,如果用户想要弹奏和弦等。

You can see the simple demo here and edit it to fit your needs, http://plnkr.co/edit/ZfcHdM?p=preview . 您可以在http://plnkr.co/edit/ZfcHdM?p=preview中查看简单的演示并对其进行编辑以适合您的需求。

However, before going down the road your currently starting on, just look around on GitHub or the Net. 但是,在开始之前,请先在GitHub或Net上浏览一下。 I have a feeling someone has already done what you're trying to accomplish. 我觉得有人已经完成了您要完成的任务。

Here's a snippet. 这是一个片段。

(function() {
  var KeyboardSimulator = (function () {
    function KeyboardSimulator() {
    }

    KeyboardSimulator.prototype.keyPressed = function (key) {
        // Add some logic to check if the particular key is registered.
        console.log('The musician pressed the "' + key + '" key.');
    };

    return KeyboardSimulator;
  })();

  $(document).ready(function() {
    var simulator = Object.create(KeyboardSimulator.prototype);
    var intrumentSimulatorContext = $('#instrumentSimulator');

    intrumentSimulatorContext.on('click', '.key', function(e) {
      var key = $(this).val();

      e.preventDefault();
      simulator.keyPressed(key);
    });

    $(document.body).on('keydown', function(e) {
      var key = String.fromCharCode(e.keyCode).toUpperCase(),
        keyMap = {
          // keyboard key -> musical note
          'A': 'C',
          'W': 'CSharp',
          'S': 'D'
        }

      if (key in keyMap) {
        $('#pg' + keyMap[key]).trigger('click');
      }

    })
  });
})();

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

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