Last active
December 11, 2022 08:10
-
-
Save letsgoawaydev/21227f8da58cfe024c9329c031b72c7e to your computer and use it in GitHub Desktop.
On-Screen Keyboard Code for Mobile Browsers (useful for game dev)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let inputBoxValue = ""; | |
function onScreenKeyboard() { | |
let inputBox = document.createElement("input"); | |
inputBox.id = "inputBox"; | |
inputBox.style = "opacity: 1; border: none; outline: 0; width: 0%; height: 0%; position: absolute; top: 0px; caret-color: transparent; color: transparent;"; | |
inputBox.type = "text"; | |
document.body.appendChild(inputBox); | |
inputBox.focus(); | |
inputBox.onblur = function () { | |
document.body.removeChild(inputBox); | |
}; | |
inputBox.onkeyup = function () { | |
inputBoxValue = inputBox.value; | |
if (evt.keyCode === 13) { | |
inputBox.blur(); | |
} | |
} | |
inputBox.onchange = function () { | |
inputBoxValue = inputBox.value; // We set this so the inputBox.value doesnt become undefined once we remove it | |
} | |
return inputBox; | |
}// You may need to use an async function or requestAnimationFrame or setInterval to | |
// get the current value of inputBoxValue (the text the user writes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment