Skip to content

Instantly share code, notes, and snippets.

@dclamage
Last active November 12, 2021 07:50
Show Gist options
  • Save dclamage/bb16a3a5d4dd3c1ebbd5cbd1a7ea7cce to your computer and use it in GitHub Desktop.
Save dclamage/bb16a3a5d4dd3c1ebbd5cbd1a7ea7cce to your computer and use it in GitHub Desktop.
Changes WASD and shift-WASD to do the same as arrow keys.
// ==UserScript==
// @name Fpuzzles-WASD
// @namespace http://tampermonkey.net/
// @version 1.3
// @description Changes wasd to move between cells
// @author Rangsk
// @match https://*.f-puzzles.com/*
// @match https://f-puzzles.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
const doShim = function() {
'use strict';
const prevOnKeyDown = document.onkeydown;
document.onkeydown = function(event) {
if (!event.key ||
!['w', 'a', 's', 'd'].includes(event.key.toLowerCase()) ||
disableInputs ||
popup ||
testPaused() ||
toolCosmetics.includes(currentTool) ||
event.ctrlKey ||
event.altKey ||
event.metaKey) {
prevOnKeyDown(event);
return;
}
event.preventDefault();
if (selection.length) {
var newCell;
const key = event.key.toLowerCase();
if (key === 'w') {
newCell = grid[(selection[selection.length - 1].i - 1 + size) % size][selection[selection.length - 1].j];
} else if (key === 'd') {
newCell = grid[selection[selection.length - 1].i][(selection[selection.length - 1].j + 1 + size) % size];
} else if (key === 's') {
newCell = grid[(selection[selection.length - 1].i + 1 + size) % size][selection[selection.length - 1].j];
} else {
newCell = grid[selection[selection.length - 1].i][(selection[selection.length - 1].j - 1 + size) % size];
}
if (event.shiftKey) {
newCell.select();
selection.push(selection.splice(selection.indexOf(newCell), 1)[0]);
} else {
selection = [newCell];
}
} else {
selection.push(grid[0][0]);
}
}
}
let intervalId = setInterval(() => {
if (!document.onkeydown) {
return;
}
clearInterval(intervalId);
doShim();
}, 16);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment