1

So guys this is a very simple question but I couldnt find a good answer. I just want to automate a keypress event for the key 1 with intervals on a web page.

Whats the simplest way to simulate a keypress event?

Is there such a thing like document.keypress()?

Ozan Yılmaz
  • 110
  • 1
  • 10

2 Answers2

0

You can do it as follows -

// Setup the eventListener to log whenever a key press occurs
window.addEventListener('keydown', function(event) {
  console.log('key pressed - ', event.key);
});

// create a keyboard press event
var event = new KeyboardEvent('keydown', {
  'key': '1'
});

// call / simulate the event every 1000s using dispathEvent method
setInterval(() => window.dispatchEvent(event), 1000);

In the above demo code, I have just set an eventListener over the whole window which would just console log - key pressed whenever a key is pressed.

Below it is just creating an event using new KeyboardEvent and then calling it or you can say simulating a keyboard press event every 1 second using dispatchEvent inside the setInterval function

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • how can I change keydown with keypress instead? I want to stroke key 1 instead of any key. – Ozan Yılmaz Sep 05 '20 at 22:01
  • @OzanYılmaz Also, check these links for more info - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyboardEvent, https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically/19883789#19883789 – Abhishek Bhagate Sep 05 '20 at 22:17
  • @OzanYılmaz Also, reading your comment on other answer, I feel that this is probably not the optimal way to do what you are trying to achieve. Instead, you can directly write a `setInterval` function and inside that write the logic(moving to next profile in your case) . That would be much better than simulating key presses. This simulation does comes with its own risk of breaking browser security, etc which is why I am not recommending this approach – Abhishek Bhagate Sep 05 '20 at 22:21
  • I will definetely write a setInterval function, but for that I need a callback function,and callback function is either a mouse click or key press. I tried mouse click, it didnt work, so I thought instead I could do key press call back function. So far with no luck. – Ozan Yılmaz Sep 05 '20 at 22:24
  • @OzanYılmaz Callback function shouldn't be mouse click or keypresd. Instead write the logic that gets executed by your code whenever a keypress or mouse click occurs. And as I mentioned already, simulating this key press isn't a bad practice and comes with downside – Abhishek Bhagate Sep 05 '20 at 22:31
  • the action triggered after mouse click or key press 1 is sending a http request.It complicates the issue. You can think of it as tinder. Lets say there are 10000 people in my area, instead of liking every one of them one by one, I want to automate the process. Key press 1 likes the profile. I will like someone new every 10 secs by automation. – Ozan Yılmaz Sep 05 '20 at 22:38
0

Are you looking for something like this? You can capture actual keyboard input with this as well.

I would recommend that you use a keyup event instead.

document.addEventListener('keyup', e => {
  // User is in a textbox don't continue
  if(['textarea', 'input'].includes(document.activeElement.tagName.toLowerCase())) {
    console.log('in textarea')
    return;
  }

  // The user is not in a textbox resume navigation.
  if (e.key === 'd') {
    console.log('next profile')
  } else if (e.key === 'a') {
    console.log('previous profile')
  } else {
    console.log('invalid key')
  }
})

setInterval(() => {
  let evt = new KeyboardEvent('keyup', {key: 'd'})
  document.dispatchEvent(evt)
}, 2000)
<input type="text">
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338