Skip to content

Commit

Permalink
Catch errors in keycodes (#21)
Browse files Browse the repository at this point in the history
* Catch errors in keycodes

 While loading a converted keymap that contains unknown codes,
 loading stops for the remaining keycodes and an error is printed
 on the console.

 This commit extracts the lookup into a function which prints a
 warning if the code is unknown.

 - print a warning if keyboard code is unknown
 - gracefully handle a lookup failure by continuing to process keyboard
   codes

* restore keycode values
  • Loading branch information
yanfali authored and jackhumbert committed Mar 30, 2018
1 parent 21bb06b commit 8c3acb6
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ $(document).ready(() => {

function loadDefault() {
// hard-coding planck as the only default right now
if (keyboard.includes("planck")) {
$.get('keymaps/planck_default.json', function(
data
) {
if (keyboard.includes('planck')) {
$.get('keymaps/planck_default.json', function(data) {
console.log(data);
reset_keymap();

Expand All @@ -87,10 +85,8 @@ $(document).ready(() => {
render_layout($('#layout').val());
});
} else {
$('#status').append(
'\n* No default for this keyboard... yet!'
);
}
$('#status').append('\n* No default for this keyboard... yet!');
}
}

// Implementation goes here
Expand Down Expand Up @@ -476,6 +472,14 @@ $(document).ready(() => {
document.body.removeChild(element);
}

function lookupKeycode(searchTerm) {
var found = keycodes.find(({ code }) => code === searchTerm);
if (found === undefined) {
console.warn('Unknown keycode', searchTerm);
}
return found;
}

//Function that takes in a keymap loops over it and fills populates the keymap variable
function load_converted_keymap(converted_keymap) {
//Empty the keymap variable
Expand All @@ -485,6 +489,7 @@ $(document).ready(() => {
$.each(converted_keymap, function(_layer /*, keys*/) {
//Add layer object for every layer that exists
keymap[_layer] = {};
var metadata;
//Loop over each keycode in the layer
$.each(converted_keymap[_layer], function(key, keycode) {
//Check if the keycode is a complex/combo keycode ie. contains ()
Expand All @@ -497,32 +502,48 @@ $(document).ready(() => {

//Check whether it is a layer switching code or combo keycode
if (internal.includes('KC')) {
keycode = maincode + '(kc)';
metadata = lookupKeycode(internal);
if (metadata === undefined) {
return;
}
var internalkeycode = {
name: keycodes.find(x => x.code === internal).name,
name: metadata.name,
code: internal,
type: keycodes.find(x => x.code === internal).type
type: metadata.type
};
keycode = maincode + '(kc)';
metadata = lookupKeycode(keycode);
if (metadata === undefined) {
return;
}
keymap[_layer][key] = {
name: keycodes.find(x => x.code === keycode).name,
name: metadata.name,
code: keycode,
type: keycodes.find(x => x.code === keycode).type,
type: metadata.type,
contents: internalkeycode
};
} else {
keycode = maincode + '(layer)';
metadata = lookupKeycode(keycode);
if (metadata === undefined) {
return;
}
keymap[_layer][key] = {
name: keycodes.find(x => x.code === keycode).name,
name: metadata.name,
code: keycode,
type: keycodes.find(x => x.code === keycode).type,
type: metadata.type,
layer: internal
};
}
} else {
metadata = lookupKeycode(keycode);
if (metadata === undefined) {
return;
}
keymap[_layer][key] = {
name: keycodes.find(x => x.code === keycode).name,
name: metadata.name,
code: keycode,
type: keycodes.find(x => x.code === keycode).type
type: metadata.type
};
}
});
Expand Down Expand Up @@ -1155,7 +1176,6 @@ $(document).ready(() => {
{ name: 'Vol -', code: 'KC_VOLD', title: 'Volume Down' },
{ name: 'Vol +', code: 'KC_VOLU', title: 'Volume Up' },
{ name: 'Play', code: 'KC_MPLY', title: 'Play/Pause' }

];
}
});

0 comments on commit 8c3acb6

Please sign in to comment.