简体   繁体   English

从电子应用程序发送密钥?

[英]Send keys from an electron application?

The only thing I can find related to this is sendInputEvent . 我唯一找到的与此相关的是sendInputEvent

There's a couple problems with it. 有几个问题。

  1. It seems that it can only be called from the main window thread. 似乎只能从主窗口线程中调用它。 I need this to happen when someone clicks a button in the application. 当有人单击应用程序中的按钮时,我需要发生这种情况。

  2. It doesn't even seem to work at all even from the main window thread. 即使从主窗口线程来看,它似乎也根本不起作用。 Example, this doesn't seem to do anything: 例如,这似乎没有任何作用:

code: 码:

setTimeout(function() {
    win.webContents.sendInputEvent({keyCode: 'Tab', type: 'keyDown', modifiers: ['alt']});
}, 3000);
  1. I want to send some key strokes after the alt-tab as well, and the API says that the function only works if the window is focused. 我也想在alt-tab 之后发送一些按键,并且API表示该功能仅在窗口聚焦时起作用。

It looks like RobotJS might work for this, but it seems a bit heavy-handed for something that is one line in other languages (eg SendKeys.Send ). 看起来RobotJS 可能可以解决此问题,但是对于其他语言中的一行代码(例如SendKeys.Send )来说,似乎有些笨拙。 Also it looks like it requires building it manually. 而且看起来它需要手动构建。 All in all I'm trying to keep my absolute dependency count to a minimum if possible. 总而言之,如果可能的话,我试图将绝对依赖项的数量减到最少。

Any ideas? 有任何想法吗?

I agree RobotJS is a beast for that task and instead I would go for node-key-sender 我同意RobotJS是完成这项任务的野兽,相反,我会去找node-key-sender

I would also not send the ALT-TAB keystroke and instead would use window.hide() or window.minimize . 我也不会发送ALT-TAB按键,而是使用window.hide()window.minimize You can read more here: Electron API docs for minimize() and hide() 您可以在此处阅读更多内容: Electron API文档,用于minimum()和hide()

It has all the features you need and would work even after you unfocus from your electron window. 它具有您需要的所有功能,即使您从电子窗口中散焦后也可以使用。 The only caveat is that it uses Java Runtime behind the scenes 唯一的警告是它在后台使用Java Runtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM