How do I write a command to press a key? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How do I write a command to press a key?

How do I make a key press? For example make the key “a” press, with a JavaScript code?

11th Feb 2020, 12:27 AM
Ginfio
Ginfio - avatar
8 Answers
11th Feb 2020, 12:58 AM
Kevin ★
+ 5
Calviղ Your code looks like if you're pressing a key but it's an illusion.
11th Feb 2020, 1:30 AM
Kevin ★
+ 3
<input id="in1" type="text" /> <script> const in1 = document.getElementById('in1'); in1.value = in1.value + 'a'; </script>
11th Feb 2020, 1:00 AM
Calviղ
Calviղ - avatar
+ 2
What do you mean by "press itself"? What's the purpose of doing that?
11th Feb 2020, 2:20 AM
Calviղ
Calviղ - avatar
+ 2
Ginfio Just update the input with value property
11th Feb 2020, 3:17 AM
Calviղ
Calviղ - avatar
+ 1
Ok, it looks like I should explain a little further... On this typing game called "Nitro Type", I'm trying to see if I could attempt to make an auto typer because there isn't a working one at the moment. When you go to race (to type), the text you are gonna type comes up. For example if the first letter is a "D", you press the key "D", if the next letter is a "c", you type "c", ... and so on. So, first I need to know how I can make that "D" (for example) press itself. ... the website: https://www.nitrotype.com/race
11th Feb 2020, 1:44 AM
Ginfio
Ginfio - avatar
+ 1
Calviղ I mean like: there’s an input and you want to type “b”, you don’t actually physically press the key, but somehow with some code type b in the input. Auto typer.
11th Feb 2020, 2:31 AM
Ginfio
Ginfio - avatar
+ 1
try updating the input with value property as you are doing but with delay, like using timer so that you can get the visual effect of typing
11th Feb 2020, 2:58 PM
Shweta
Shweta - avatar