Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use key scancode instead of key character #124

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/js/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var Keycodes = {
var Keymap = {}
for (var id in Layouts) {
Keymap[id] = buildKeymapFromLayout(Layouts[id]);
console.log(Keymap[id])
}

function buildKeymapFromLayout(rows) {
Expand Down
30 changes: 25 additions & 5 deletions src/js/synth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,29 @@ const synth = new Synth()
// A S D F G H J K L ; ' \
// Z X C V B N M , . /
//
const layout = [
"Digit1 Digit2 Digit3 Digit4 Digit5 Digit6 Digit7 Digit8 Digit9 Digit0 Minus Equal",
"KeyQ KeyW KeyE KeyR KeyT KeyY KeyU KeyI KeyO KeyP BracketLeft BracketRight",
"KeyA KeyS KeyD KeyF KeyG KeyH KeyJ KeyK KeyL Semicolon Quote Backquote",
"KeyZ KeyX KeyC KeyV KeyB KeyN KeyM Comma Period Slash",
]
function buildKeymapFromLayout(rows) {
var map = {}
for (var r = rows.length - 1; r >= 0; r--) {
var row = rows[r].split(" ");
var rowId = rows.length - r - 2;
for (var c = 0; c < row.length; c++) {
var keycode = row[c];
map[keycode] = [rowId, c];
}
}
return map;
}
const keymap = buildKeymapFromLayout(layout)
function keycode_to_midinote(keycode) {
// get row/col vals from the keymap
var key = synth.keymap[keycode];

var key = keymap[keycode];

if (R.isNil(key)) {
// return false if there is no note assigned to this key
Expand Down Expand Up @@ -61,11 +81,11 @@ document.addEventListener("keydown", function (event) {
}

// bail, if a modifier is pressed alongside the key
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey || event.repeat) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna add the change from this line to the repo and possibly refactor this line into a function

return false;
}

const midiNote = keycode_to_midinote(event.which); // midi note number 0-127
const midiNote = keycode_to_midinote(event.code); // midi note number 0-127
const velocity = 100

if (midiNote !== false) {
Expand All @@ -77,10 +97,10 @@ document.addEventListener("keydown", function (event) {
// KEYUP -- capture keyboard input
document.addEventListener("keyup", function (event) {
// bail, if a modifier is pressed alongside the key
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey || event.repeat) {
return false;
}
const midiNote = keycode_to_midinote(event.which)
const midiNote = keycode_to_midinote(event.code)
if (midiNote !== false) {
event.preventDefault();
synth.noteOff(midiNote);
Expand Down