Java script Key event Piano

格式
Plain text
提交日期
2014-06-26 15:23
Publication Period
Unlimited
  1. <html>
  2. <HEAD>
  3. <title> Test 01 - Bluetooth Keybord Piano </title>
  4. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS">
  5. <meta name="viewport" content="width=device-width" />
  6. <meta name="apple-mobile-web-app-capable" content="yes" />
  7. <script type="text/javascript">
  8. document.onkeydown = function(e) {
  9. var shift, ctrl, iNote;
  10. //alert('Start'); //Test *************************
  11. // Mozilla(Firefox, NN) and Opera
  12. if (e != null) {
  13. //alert('Mozilla'); //Test *************************
  14. keycode = e.which;
  15. ctrl = typeof e.modifiers == 'undefined' ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK;
  16. shift = typeof e.modifiers == 'undefined' ? e.shiftKey : e.modifiers & Event.SHIFT_MASK;
  17. // イベントの上位伝播を防止
  18. e.preventDefault();
  19. e.stopPropagation();
  20. // Internet Explorer
  21. } else {
  22. //alert('IE'); //Test *************************
  23. keycode = event.keyCode;
  24. ctrl = event.ctrlKey;
  25. shift = event.shiftKey;
  26. // イベントの上位伝播を防止
  27. event.returnValue = false;
  28. event.cancelBubble = true;
  29. }
  30. iNote = 99;
  31. // キーコードの文字を取得
  32. keychar = String.fromCharCode(keycode).toUpperCase();
  33. // Key to Note -------------------------
  34. if (keychar == "Z") {
  35. iNote = -11.99;
  36. } else if (keychar == "S") {
  37. iNote = -11;
  38. } else if (keychar == "X") {
  39. iNote = -10;
  40. } else if (keychar == "D") {
  41. iNote = -9;
  42. } else if (keychar == "C") {
  43. iNote = -8;
  44. } else if (keychar == "V") {
  45. iNote = -7;
  46. } else if (keychar == "G") {
  47. iNote = -6;
  48. } else if (keychar == "B") {
  49. iNote = -5;
  50. } else if (keychar == "H") {
  51. iNote = -4;
  52. } else if (keychar == "N") {
  53. iNote = -3;
  54. } else if (keychar == "J") {
  55. iNote = -2;
  56. } else if (keychar == "M") {
  57. iNote = -1;
  58. } else if (keychar == "," || keychar == "." || keychar == "Q") {
  59. iNote = 0;
  60. } else if (keychar == "2") {
  61. iNote = 1;
  62. } else if (keychar == "W") {
  63. iNote = 2;
  64. } else if (keychar == "3") {
  65. iNote = 3;
  66. } else if (keychar == "E") {
  67. iNote = 4;
  68. } else if (keychar == "R") {
  69. iNote = 5;
  70. } else if (keychar == "5") {
  71. iNote = 6;
  72. } else if (keychar == "T") {
  73. iNote = 7;
  74. } else if (keychar == "6") {
  75. iNote = 8;
  76. } else if (keychar == "Y") {
  77. iNote = 9;
  78. } else if (keychar == "7") {
  79. iNote = 10;
  80. } else if (keychar == "U") {
  81. iNote = 11;
  82. } else if (keychar == "I") {
  83. iNote = 12;
  84. } else if (keychar == "9") {
  85. iNote = 13;
  86. } else if (keychar == "O") {
  87. iNote = 14;
  88. } else if (keychar == "0") {
  89. iNote = 15;
  90. } else if (keychar == "P") {
  91. iNote = 16;
  92. } else if (keychar == "@" || keychar == "[") {
  93. iNote = 17;
  94. }
  95. // Pitch -> Play
  96. //alert('Play'); //Test *************************
  97. if (iNote != 99) {
  98. fnRatePlus(2^(iNote/12));
  99. fnPlay();
  100. }
  101. }
  102. document.onkeyup = function(e) {
  103. //alert('Stop'); //Test *************************
  104. fnStop();
  105. }
  106. function fnPlay(){
  107. document.getElementById('audio').play();
  108. }
  109. function fnRatePlus(pbRate){
  110. document.getElementById('audio').playbackRate = pbRate;
  111. }
  112. function fnStop(){
  113. document.getElementById('audio').pause();
  114. document.getElementById('audio').currentTime = 0;
  115. }
  116. function fnPause(){
  117. document.getElementById('audio').pause();
  118. }
  119. </script>
  120. </HEAD>
  121. <body>
  122. <audio id="audio" controls>
  123. <source src="ding.wav" type="audio/wav">
  124. </audio>
  125. Press keys
  126. </body>
  127. </html>
下载 可打印视图

网址

Embed with JavaScript

Embed with iframe

原始文本