問題描述
我已經參與了幾個 Electron 項目,我正在嘗試弄清楚如何在應用程序處于后臺時偵聽任何按鍵或跟蹤鼠標移動.我正在使用 menubar 插件,所以應用程序仍在后臺運行.
I've been getting into a few Electron projects and I am trying to figure out how you could listen for any keypresses or and track mouse movement when the app is in the background. I am using the menubar plugin so the app is still running in the background.
我嘗試使用 global-shortcut API,但它看起來僅適用于快捷方式,您無法注冊單個按鍵.我還查看了 API,但尚未找到應用程序主屏幕之外的擊鍵和鼠標移動事件.
I tried using the global-shortcut API but it looks like that is for shortcuts only and you can't register individual keystrokes. I've also looked over the API and have yet to find an event for keystrokes and mouse movements outside the app's main screen.
推薦答案
這超出了電子應用程序的正常用例范圍.為了做到這一點,您需要開發一個本地模塊來調用操作系統的相應本地函數.
This is outside of the realm of normal use-cases for an electron application. In order to do this you would need to develop a native module that calls into the corresponding native functions of the operating system.
例如在 Windows 上:C++ Win32 鍵盤事件
For example on windows: C++ Win32 keyboard events
為節點制作原生插件:https://nodejs.org/api/addons.html
僅供參考,原生插件非常難.
FYI, Native addons are very hard.
這篇關于在 Electron 應用程序之外監聽鍵盤事件和鼠標移動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!