Java script Key event Piano
- <html>
- <HEAD>
- <title> Test 01 - Bluetooth Keybord Piano </title>
- <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS">
- <meta name="viewport" content="width=device-width" />
- <meta name="apple-mobile-web-app-capable" content="yes" />
- <script type="text/javascript">
- document.onkeydown = function(e) {
- var shift, ctrl, iNote;
- //alert('Start'); //Test *************************
- // Mozilla(Firefox, NN) and Opera
- if (e != null) {
- //alert('Mozilla'); //Test *************************
- keycode = e.which;
- ctrl = typeof e.modifiers == 'undefined' ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK;
- shift = typeof e.modifiers == 'undefined' ? e.shiftKey : e.modifiers & Event.SHIFT_MASK;
- // イベントの上位伝播を防止
- e.preventDefault();
- e.stopPropagation();
- // Internet Explorer
- } else {
- //alert('IE'); //Test *************************
- keycode = event.keyCode;
- ctrl = event.ctrlKey;
- shift = event.shiftKey;
- // イベントの上位伝播を防止
- event.returnValue = false;
- event.cancelBubble = true;
- }
- iNote = 99;
- // キーコードの文字を取得
- keychar = String.fromCharCode(keycode).toUpperCase();
- // Key to Note -------------------------
- if (keychar == "Z") {
- iNote = -11.99;
- } else if (keychar == "S") {
- iNote = -11;
- } else if (keychar == "X") {
- iNote = -10;
- } else if (keychar == "D") {
- iNote = -9;
- } else if (keychar == "C") {
- iNote = -8;
- } else if (keychar == "V") {
- iNote = -7;
- } else if (keychar == "G") {
- iNote = -6;
- } else if (keychar == "B") {
- iNote = -5;
- } else if (keychar == "H") {
- iNote = -4;
- } else if (keychar == "N") {
- iNote = -3;
- } else if (keychar == "J") {
- iNote = -2;
- } else if (keychar == "M") {
- iNote = -1;
- } else if (keychar == "," || keychar == "." || keychar == "Q") {
- iNote = 0;
- } else if (keychar == "2") {
- iNote = 1;
- } else if (keychar == "W") {
- iNote = 2;
- } else if (keychar == "3") {
- iNote = 3;
- } else if (keychar == "E") {
- iNote = 4;
- } else if (keychar == "R") {
- iNote = 5;
- } else if (keychar == "5") {
- iNote = 6;
- } else if (keychar == "T") {
- iNote = 7;
- } else if (keychar == "6") {
- iNote = 8;
- } else if (keychar == "Y") {
- iNote = 9;
- } else if (keychar == "7") {
- iNote = 10;
- } else if (keychar == "U") {
- iNote = 11;
- } else if (keychar == "I") {
- iNote = 12;
- } else if (keychar == "9") {
- iNote = 13;
- } else if (keychar == "O") {
- iNote = 14;
- } else if (keychar == "0") {
- iNote = 15;
- } else if (keychar == "P") {
- iNote = 16;
- } else if (keychar == "@" || keychar == "[") {
- iNote = 17;
- }
- // Pitch -> Play
- //alert('Play'); //Test *************************
- if (iNote != 99) {
- fnRatePlus(2^(iNote/12));
- fnPlay();
- }
- }
- document.onkeyup = function(e) {
- //alert('Stop'); //Test *************************
- fnStop();
- }
- function fnPlay(){
- document.getElementById('audio').play();
- }
- function fnRatePlus(pbRate){
- document.getElementById('audio').playbackRate = pbRate;
- }
- function fnStop(){
- document.getElementById('audio').pause();
- document.getElementById('audio').currentTime = 0;
- }
- function fnPause(){
- document.getElementById('audio').pause();
- }
- </script>
- </HEAD>
- <body>
- <audio id="audio" controls>
- <source src="ding.wav" type="audio/wav">
- </audio>
- Press keys
- </body>
- </html>