Hi , I am looking for different ways to script a single keydown to act like a multiple keydown.
When i press a key i want it to be as if i pressed ten times.
Thanx .
I am the most basic beginner.
Hi , I am looking for different ways to script a single keydown to act like a multiple keydown.
When i press a key i want it to be as if i pressed ten times.
Thanx .
I am the most basic beginner.
You can’t reliable simulate keypresses from javascript.
You can listen for events when keys are pressed and change an inputs value though, which sounds like what you are asking.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style media="screen">
input {
width: 100%
}
</style>
</head>
<body>
<input>
<script type="text/javascript">
var input = document.querySelector('input')
input.addEventListener('keydown', function(event) {
var key = String.fromCharCode(event.keyCode);
if (/[a-zA-Z0-9-_ ]/.test(key)) {
var tenChars = Array(10).join(event.key)
input.value = input.value + tenChars
}
}, false)
</script>
</body>
</html>
The ugly bit in the middle /[a-zA-Z0-9-_ ]/.test(key)
just checks that the character is alphanumeric key if it repeats it, and this example will break when you try and add text to the middle of the input but it’s a start at what you’re describing.
For what purpose are you wanting to press a key 10 times for every 1. I ask for there can be some situations where a known good solution can be applied.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.