0

I want to press any key on keyboard pro-grammatically using jquery. I have tried very hard but not successful.

HTML CODE

<input type="tel" name="org_contact_no" id="org_contact_no" maxlength="13" class="form-control">

JS CODE

FIRST ATTEMPT

setTimeout(function () {
    $('#org_contact_no').trigger( "click");
}, 1000);

SECOND ATTEMPT

setTimeout(function() {
document.addEventListener('keypress', (event) => {
    console.log('Meta Key?', event.metaKey);
    console.log('Key Code', event.which);
});

document.dispatchEvent(new KeyboardEvent('keypress', {
    metaKey: true, // On Windows, this is "window" key
    keyCode: 44 // This is keyCode of comma on Windows-US
}))}, 1000);

Still don't have any luck.

I know its not a button. It's an input type="tel". I just want to press any key using 1 sec or XX sec.

Niteen
  • 121
  • 13
  • Does this answer your question? [Is it possible to simulate key press events programmatically?](https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically) – Nijeesh Joshy Nov 15 '19 at 10:17
  • 1
    This is an X/Y question, as you're asking about your attempted solution rather than the problem you're trying to solve. Faking keyboard events is rarely a good idea. What exactly are you trying to achieve? – Rory McCrossan Nov 15 '19 at 10:18
  • @Nijeesh shared link is not worked for me. Thanks – Niteen Nov 15 '19 at 10:31
  • @Rory I'm going to simulate the key event programmatically. So I have tried lots of variations. Those variations not worked as expected so I post it on here. Thanks – Niteen Nov 15 '19 at 10:33
  • 1
    I understand that, I'm asking you *why*, because it's rarely a good idea. – Rory McCrossan Nov 15 '19 at 10:35
  • @Rory To simulate whole excel sheet on web browser and there are lots of text boxes and complex calculations. Calculations based on the previous textbox triggered or not. – Niteen Nov 15 '19 at 10:41
  • Possible duplicate: https://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery – Kalimah Nov 15 '19 at 11:27
  • @KalimahApps Sorry to say but this answer not worked for me. Thanks – Niteen Nov 15 '19 at 11:40

1 Answers1

0

you can trigger the "keypress" event of an input using this code example. I hope this will help you. You can learn more from here and see whether your browser supports this.

const log = document.getElementById('log');

document.addEventListener('keypress', logKey);

function logKey(e) {
    log.textContent += ` ${e.code}`;
}
<input type="text" placeholder="click here and type">
<p id="log"></p>

Following method is a better way to trigger the keypress event since the previous one is not recommended you can use the onkeypres event

document.getElementById("txtInput").onkeypress = function (e) {
                e = e || window.event;

                document.getElementById("pId").textContent += "keyCode " + e.keyCode + ", ";
                //use e.key to get the key
};
<input  id = "txtInput" type="text">
<p id ="pId"></p>
  • Note the deprecated message at the beginning of that document: "This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time." – Phil Cooper Nov 15 '19 at 10:55
  • Yes document.keypress is no longer a good method therefore I added another method to my post. – Sachintha Nayanajith Nov 15 '19 at 17:52
  • @SachinthaNayanajith Thanks for your efforts, but I think you have to read the question again. I don't want to invoke key board key event. I want to press keyboard key pro-grammatically, i.e. On page load event OR after triggering something I want to press keyboard key without human interface (pro-grammatically) after 10x second or 20x seconds. Again thanks for your help and efforts – Niteen Nov 18 '19 at 05:14