Auto detect keyboard issue

First attempt:
hiddenInput saves the most recent value to window.lastKey
If we detect a keydown event with invalid key, keyCode or which, we enable window.keyboardFix, and redispatch the key from window.lastKey
The keyboard blocking function now checks for window.keyboardFix
This commit is contained in:
FlamedDogo99 2024-06-06 11:26:45 -06:00 committed by GitHub
parent 2ed6365c42
commit f5e74945ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,6 +26,7 @@ if(!isMobile()) {
window.keyboardEnabled = false; window.keyboardEnabled = false;
window.crouchLock = false; window.crouchLock = false;
window.sprintLock = false; window.sprintLock = false;
window.keyboardFix = false
// Used for changing touchmove events to mousemove events // Used for changing touchmove events to mousemove events
var previousTouchX = null; var previousTouchX = null;
var previousTouchY = null; var previousTouchY = null;
@ -41,7 +42,7 @@ Object.defineProperty(EventTarget.prototype, "addEventListener", {
value: function (type, fn, ...rest) { value: function (type, fn, ...rest) {
if(type == 'keydown') { if(type == 'keydown') {
_addEventListener.call(this, type, function(...args) { _addEventListener.call(this, type, function(...args) {
if(!args[0].isValid) { if(!args[0].isValid && window.keyboardFix) {
return; return;
} }
return fn.apply(this, args); return fn.apply(this, args);
@ -59,6 +60,36 @@ Event.prototype.preventDefault = function() {
this._preventDefault(); this._preventDefault();
} }
} }
function hiddenInputHandler (e) {
let inputData = e.data.charAt(0)
window.lastKey = inputData
hiddenInput.value = " "; // We need a character to detect deleting
if(window.keyboardFix) {
if(e.inputType == 'insertText') {
let inputData = e.data.charAt(0);
let isShift = (inputData.toLowerCase() != inputData);
if(isShift) {
keyEvent("shift", "keydown")
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
keyEvent("shift", "keyup")
} else {
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
}
} else if (e.inputType == 'deleteContentForward' || e.inputType == 'deleteContentBackward') {
keyEvent("backspace", "keydown")
keyEvent("backspace", "keyup")
}
}
}
window.addEventListener("keydown", function(e) {
if((e.key == null || e.keyCode == null || e.which = null) && !window.keyboardFix) {
window.keyboardFix = true;
keyEvent(window.lastKey, "keydown")
keyEvent(window.lastKey, "keyup")
}
}, false);
// Key and mouse events // Key and mouse events
// Note: the client must have the key, keyCode, and which parameters defined or it will crash // Note: the client must have the key, keyCode, and which parameters defined or it will crash
// Note: for text inputs, the client only reads from the "key" paramater // Note: for text inputs, the client only reads from the "key" paramater
@ -364,25 +395,7 @@ function insertCanvasElements() {
// We are hiding the text input behind button because opacity was causing problems. // We are hiding the text input behind button because opacity was causing problems.
hiddenInput.style.cssText = "position:absolute;top: 0vh; margin: auto; left: 8vh; right:0vh; width: 8vh; height: 8vh;font-size:20px;z-index:-10;color: transparent;text-shadow: 0 0 0 black;"; hiddenInput.style.cssText = "position:absolute;top: 0vh; margin: auto; left: 8vh; right:0vh; width: 8vh; height: 8vh;font-size:20px;z-index:-10;color: transparent;text-shadow: 0 0 0 black;";
hiddenInput.value = " " //Allows delete to be detected before input is changed hiddenInput.value = " " //Allows delete to be detected before input is changed
hiddenInput.addEventListener("input", function(e) { hiddenInput.addEventListener("input", hiddenInputHandler(e), false);
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) {
keyEvent("shift", "keydown")
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
keyEvent("shift", "keyup")
} else {
keyEvent(inputData, "keydown");
keyEvent(inputData, "keyup");
}
} else if (e.inputType == 'deleteContentForward' || e.inputType == 'deleteContentBackward') {
keyEvent("backspace", "keydown")
keyEvent("backspace", "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;"