1

Hi this is actually a pretty simple question but not sure if there’s a simple answer.

But is it possible for a webpage to trigger a key press on a machine. Like hitting the A key or maybe the shift?

bymem
  • 79
  • 1
  • 8
  • This is not duplicate of the other question because what I want is the page to being able to press buttons, let’s say then windows button so the start menu of windows pops up. – bymem Mar 17 '19 at 18:41

1 Answers1

2

Assuming you're referring to triggering DOM events on a web page, you can use the Event object. A detailed description of the API can be found here here.

A simple example:

var clickEvent = new Event('click');
var elem = document.getElementById('event-target');

elem.dispatchEvent(clickEvent);

There is also the KeyboardEvent class for triggering keyboard events. You can read about it here. It's first parameter is the type of event, and the second is an object describing the event. Example usage:

var aKeyDownEvent = new KeyboardEvent('keydown', {key: 'a'});
var elem = document.getElementById('event-target');

elem.dispatchEvent(aKeyDownEvent);

Keep in mind there isn't universal browser support for these features.

robinsax
  • 1,195
  • 5
  • 9
  • Can you add more detail that the duplicate question doesn't already show in the answers? Dispatching events is good, but you haven't touched on dispatching a keyboard event – Sterling Archer Mar 17 '19 at 02:41
  • You're right, i'll edit – robinsax Mar 17 '19 at 02:44
  • But I’m not sure if you understood the question correctly then. Because I want the page to be able to press buttons, let’s say then windows button so the start menu of windows pops up. – bymem Mar 17 '19 at 18:40
  • You can't do that, that would be a huge security issue – robinsax Mar 18 '19 at 16:30