Skip to content

Instantly share code, notes, and snippets.

@letsgoawaydev
Last active December 11, 2022 08:10
Show Gist options
  • Save letsgoawaydev/21227f8da58cfe024c9329c031b72c7e to your computer and use it in GitHub Desktop.
Save letsgoawaydev/21227f8da58cfe024c9329c031b72c7e to your computer and use it in GitHub Desktop.
On-Screen Keyboard Code for Mobile Browsers (useful for game dev)
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