shortcutblocker/scripts/shortcutblocker.js

100 lines
4.2 KiB
JavaScript

// Yep, that's it. Just 5 lines of code. That's my module. Not more, not less...
// Thank you for coming to my ted talk
// Define a new setting which can be stored and retrieved
//This hook allows us to only register the settings AFTER the settings are available. (Cant have a sword without a grip...)
Hooks.on("init", async function() {
const heldKeys = [];
//This registers the settings
game.settings.register('shortcutblocker', 'ctrlblock', {
name: 'Blocked CTRL Combos', //Kinda obvious, eh?
hint: 'Comma sepparated list of keys, which are disallowed to be used with CTRL. Example: "F5, R, W" This would allow CTRL + F5, CTRL + R and CTRL + W.', // Some descriptive text.
scope: 'client', // "world" = sync to db, "client" = local storage
config: true, // false if you dont want it to show in module config
type: String, // Number, Boolean, String, Object
default: "t, r, f5",
onChange: value => { // "value =>" means, that the value will be passed through.... I think
debounceReload()
}
}),
//SPIEL DEN SELBEN SONG NOCHMAL!
game.settings.register('shortcutblocker', 'altblock', {
name: 'Blocked ALT Combos',
hint: 'Comma sepparated list of key names. Example: "F4". That would block "ALT + F4"',
scope: 'client',
config: true,
type: String,
default: "f12",
onChange: value => {
debounceReload()
}
}),
// There probably is a smoother way of doing this, but I cannot be bothered to google for that
game.settings.register('shortcutblocker', 'shiftblock', {
name: 'Blocked SHIFT Combos',
hint: 'Comma sepparated list of keys, which are allowed to be used with SHIFT. Example: "F5, R, W" This would allow SHIFT + F5',
scope: 'client',
config: true,
type: String,
default: "f5",
onChange: value => {
debounceReload()
}
}),
// Fire this event, once, ANY key is pressed.
window.addEventListener("keydown", (e) => {
if (heldKeys.includes(e.key)) {
} else {
heldKeys.push(e.key);
//Get the current set variables
const ctrlButtons = game.settings.get('shortcutblocker', 'ctrlblock');
const altButtons = game.settings.get('shortcutblocker', 'altblock');
const shiftButtons = game.settings.get('shortcutblocker', 'shiftblock');
//Create Replace any spaces.
const shiftButtonSpaceless = shiftButtons.replace(/\s/g, '')
const altButtonSpaceless = altButtons.replace(/\s/g, '')
const ctrlButtonSpaceless = ctrlButtons.replace(/\s/g, '')
// Create the arrays
const shiftButtonArray = shiftButtonSpaceless.split(',');
const altButtonArray = altButtonSpaceless.split(',');
const ctrlButtonArray = ctrlButtonSpaceless.split(',');
// Now. Look, if it's a keyboard combination.
// e.ctrlKey is obviously the Control key
// e.altKey is obviously the Alt Key
// e.key binds directly to whatever key is currently beeing pressed
// !isNan("Something") tests, if "Something" is a number. [NaN, Not a Number ;)]
if (e.ctrlKey && ctrlButtonArray.includes(e.key) || e.altKey && altButtonArray.includes(e.key)|| e.shiftKey && shiftButtonArray.includes(e.key)){
console.log("shortcutblocke: Catched keystroke! Aborting!");
// Disables the shortcuts in the browser...
// gonna be honest, didn't read the docs... copy, paste and pray.
// https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
}
}
});
// This event just helps us get rid of non-held keys.
window.addEventListener("keyup", (e) => {
var index = heldKeys.indexOf(e.key);
if (index !== -1) {
heldKeys.splice(index, 1);
}
});
// This works for Chrome. It works for Mozilla... I guess? I dunno why you read this but ye :)
});