Home > Article > Web Front-end > JS method to obtain the keys pressed on the keyboard and display them on the page_javascript skills
The example in this article describes the JS method of obtaining the keys pressed on the keyboard and displaying them on the page. Share it with everyone for your reference, the details are as follows:
Please use the keyboard to enter some characters, and these characters will be displayed in the blank space of the web page. Are you not used to the lack of text boxes or text fields? This effect is the application of the document.onkeypress object in JavaScript, which monitors every movement of the keyboard and records which keys are pressed. It’s interesting. Friends who want to study JS, let’s start with these small examples.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-web-show-key-press-codes/
The specific code is as follows:
<html> <head> <title>将按下的按键显示在页面上</title> <script language="javascript"> <!-- str = ""; function showkey(){ asc = event.keyCode; key = String.fromCharCode(asc); str += key; txt.innerHTML = str; } document.onkeypress=showkey; --> </script> </head> <body> 请使用键盘输入文字: <div id="txt"></div> </body> </html>
I hope this article will be helpful to everyone in JavaScript programming.