0

Trying to find a way to simulate a keyboard input as seen here (c#) but in javascript:

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

// inputsim instance
public static InputSimulator inp_sim = new InputSimulator();

SetForegroundWindow(FindWindowByCaption(IntPtr.Zero, "Untitled - Notepad"));

inp_sim.Keyboard.KeyPress(VirtualKeyCode.VK_A);

Haven't been able to find a way to set keypress events to other windows in javascript so if anyone could point me in the right direction, would be much appreciated :)

  • If you want such functionality for a js executed in a normal browser, then you out of luck here. If you have some other js environment, such electron, node js and so on, then you can use native interoperability fetures provided by such a js hosts to call functions mentioned in the question. – Serg Nov 07 '21 at 19:28
  • If u need to do some web automation, take a look at headless browsers such as `puppeteer` and its C# wrapper `puppeteer-sharp` : https://github.com/hardkoded/puppeteer-sharp – TheMah Nov 07 '21 at 19:30

0 Answers0