英文:
How can I use JavaScript to emulate Alt+Numpad functionality in Windows?
问题
window.addEventListener("DOMContentLoaded", function() {
var confirmButton = document.getElementById("Confirm");
var input = document.getElementById("input");
var output = document.getElementById("output");
confirmButton.addEventListener("click", clickConfirmButton);
function clickConfirmButton() {
var inputValue = input.value;
if (/^\d+$/.test(inputValue)) {
var keyCode = parseInt(inputValue);
var char = String.fromCharCode(keyCode);
output.value = char;
} else {
alert("Please enter a valid number.");
}
}
})
英文:
In Windows, we can press Alt+Numpad
to enter characters.
For example, Alt 244
is ⌠
.
I want to use javascript to realize this function.
By entering a number (like 244
) in the input
textbox and clicking the button, the character would occur in the output
textbox.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
window.addEventListener("DOMContentLoaded", function() {
var confirmButton = document.getElementById("Confirm");
var input = document.getElementById("input");
var output = document.getElementById("output");
confirmButton.addEventListener("click", clickConfirmButton);
function clickConfirmButton() {
// Please write here
}
})
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Alt+Numpad</title>
</head>
<body>
<div style="text-align: center">
<div><label for="input">Input: </label><input style="height: 30px; font-size: 20px;" id="input"></div>
<div><label for="output">Output: </label><input style="height: 30px; font-size: 20px;" id="output"></div>
<div><button id="Confirm">Confirm</button></div>
</div>
</body>
</html>
<!-- end snippet -->
I've tried use KeyBoardEvent
and dispatchEvent
but there's nothing in the output textbox.
Please write only the javascript code and no jquery, html or css.
答案1
得分: 2
I guess the answer is here: https://stackoverflow.com/questions/13636820/convert-unicode-characters-to-extended-ascii
Also it's more interesting to update the output immediately and prevent typing characters but numbers in the input:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const input = document.getElementById("input");
const output = document.getElementById("output");
input.addEventListener("input",
() => output.value = input.value ? String.fromCharCode(input.value) : ''
);
input.addEventListener("beforeinput",
e => e.data && (/\d+/.test(e.data) || e.preventDefault())
);
<!-- language: lang-html -->
<div style="text-align: center">
<div><label for="input">Input: </label><input style="height: 30px; font-size: 20px;" id="input"></div>
<div><label for="output">Output: </label><input style="height: 30px; font-size: 20px;" id="output"></div>
</div>
<!-- end snippet -->
英文:
I guess the answer is here: https://stackoverflow.com/questions/13636820/convert-unicode-characters-to-extended-ascii
Also it's more interesting to update the output immediately and prevent typing characters but numbers in the input:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const input = document.getElementById("input");
const output = document.getElementById("output");
input.addEventListener("input",
() => output.value = input.value ? String.fromCharCode(input.value) : ''
);
input.addEventListener("beforeinput",
e => e.data && (/\d+/.test(e.data) || e.preventDefault())
);
<!-- language: lang-html -->
<div style="text-align: center">
<div><label for="input">Input: </label><input style="height: 30px; font-size: 20px;" id="input"></div>
<div><label for="output">Output: </label><input style="height: 30px; font-size: 20px;" id="output"></div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论