-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// the number following the note name corresponds to the relative octave of the key | ||
const KEYMAP = { | ||
'C1': 65, | ||
'C#1': 87, | ||
'D1': 83, | ||
'D#1': 69, | ||
'E1': 68, | ||
'F1': 70, | ||
'F#1': 84, | ||
'G1': 71, | ||
'G#1': 89, | ||
'A1': 72, | ||
'A#1': 85, | ||
'B1': 74, | ||
'C2': 75, | ||
'C#2': 79, | ||
'D2': 76, | ||
'D#2': 80, | ||
'E2': 186, | ||
'F2': 222, | ||
}; | ||
|
||
|
||
function keybind(note, trigger, release) { | ||
const code = KEYMAP[note]; | ||
|
||
document.addEventListener('keydown', onKeyDown.bind(null, code, trigger)); | ||
document.addEventListener('keyup', onKeyUp.bind(null, code, release)); | ||
} | ||
|
||
const keys = {}; | ||
|
||
function onKeyDown(code, cb, e) { | ||
const keyCode = e.which || e.keyCode; | ||
const notAlreadyPlaying = !keys[keyCode]; | ||
|
||
if(keyCode === code && notAlreadyPlaying){ | ||
keys[keyCode] = true; | ||
cb(); | ||
} | ||
} | ||
|
||
function onKeyUp(code, cb, e) { | ||
const keyCode = e.which || e.keyCode; | ||
const alreadyPlaying = keys[keyCode]; | ||
|
||
if(keyCode === code && alreadyPlaying){ | ||
keys[keyCode] = false; | ||
cb(); | ||
} | ||
} | ||
|
||
export default keybind; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters