Added android workaround

Also I accidentally converted all the tabs to spaces, thus the large diff. I think this will be the standard from now on
This commit is contained in:
FlamedDogo99 2024-06-03 13:53:22 -06:00
parent 22fb12d51a
commit aed6d14c13

View File

@ -30,20 +30,56 @@ window.sprintLock = false;
var previousTouchX = null; var previousTouchX = null;
var previousTouchY = null; var previousTouchY = null;
var startTouchX = null; var startTouchX = null;
// Ignores any keydown event that doesn't have the isValid parameter.
const proto = EventTarget.prototype;
const _addEventListener = EventTarget.prototype.addEventListener;
Object.defineProperty(proto, "addEventListener", {
value: function (type, fn, ...rest) {
_addEventListener.call(this, type, function(...args) {
if(type === 'keydown' && (!args[0].isValid)) {
return;
}
return fn.apply(this, args);
}, ...rest);
}
});
//Allows keyboard to type in input
const _preventDefault = Event.prototype.preventDefault;
Object.defineProperty(Event.prototype, "preventDefault", {
value: function () {
if(document.activeElement.id != "hiddenInput") {
_preventDefault.call(this);
}
}
});
// Key and mouse events // Key and mouse events
function keyEvent(name, state) { function keyEvent(name, state) {
const keyName = name.toUpperCase().charCodeAt(0) const keyName = name.toUpperCase().charCodeAt(0);
window.dispatchEvent(new KeyboardEvent(state, { let evt = new KeyboardEvent(state, {
key: name, key: name,
keyCode: keyName, keyCode: keyName,
which: keyName which: keyName
})); });
evt.isValid = true; // Disables fix for bad keyboard input
window.dispatchEvent(evt);
} }
function shiftKeyEvent(state) { function shiftKeyEvent(state) {
window.dispatchEvent(new KeyboardEvent(state, { let evt = new KeyboardEvent(state, {
key: "Shift",
keyCode: 16, keyCode: 16,
which: 16 which: 16
})); });
evt.isValid = true; // Disables fix for bad keyboard input
window.dispatchEvent(evt);
}
function deleteKeyEvent(state) {
let evt = new KeyboardEvent(state, {
key: "Backspace",
keyCode: 8,
which: 8
});
evt.isValid = true; // Disables fix for bad keyboard input
window.dispatchEvent(evt);
} }
function mouseEvent(number, state, canvas) { function mouseEvent(number, state, canvas) {
canvas.dispatchEvent(new PointerEvent(state, {"button": number})) canvas.dispatchEvent(new PointerEvent(state, {"button": number}))
@ -325,6 +361,26 @@ function insertCanvasElements() {
let hiddenInput = document.createElement('input', true); let hiddenInput = document.createElement('input', true);
hiddenInput.id = "hiddenInput" hiddenInput.id = "hiddenInput"
hiddenInput.style.cssText = "opacity:0;z-index:-99999"; hiddenInput.style.cssText = "opacity:0;z-index:-99999";
hiddenInput.value = " " //Allows delete to be detected before input is changed
hiddenInput.addEventListener("input", function(e) {
hiddenInput.value = " "; // We need a character to detect deleting
if(e.inputType == 'insertText') {
let inputData = e.data.charAt(0);
let isShift = (inputData.toLowerCase() != inputData);
if(isShift) {
shiftKeyEvent("keydown");
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
shiftKeyEvent("keyup");
} else {
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
}
} else if (e.inputType == 'deleteContentForward' || e.inputType == 'deleteContentBackward') {
deleteKeyEvent("keydown");
deleteKeyEvent("keyup")
}
}, false);
document.body.appendChild(hiddenInput); document.body.appendChild(hiddenInput);
let keyboardButton = createTouchButton("keyboardButton", "inMenu"); let keyboardButton = createTouchButton("keyboardButton", "inMenu");
keyboardButton.style.cssText = "top: 0vh; margin: auto; left: 8vh; right:0vh; width: 8vh; height: 8vh;" keyboardButton.style.cssText = "top: 0vh; margin: auto; left: 8vh; right:0vh; width: 8vh; height: 8vh;"