node ffi ffi.Library往电脑窗口的任意光标处输入内容(user32.dll)window电脑
类似键盘输入法的效果
我node项目和electron项目遇到需求:控件上输入任意的文字,在其他的窗口显示。
已解决!
前提:
这是user32.dll 的API
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows
electron 使用 node-ffi 调用 C++ 动态链接库(DLL)
引入各种环境和架包毕竟你要调系统的C++的内容肯定需要编译的
npm install --global --production windows-build-tools
npm install -g node-gyp
装Python v2.7; 不能装>=3版本,否则编译不通过
npm install ffi
npm install ref
npm install electron-rebuild
npm run rebuild 操作即可完成electron的重编译。
cd进入node_modules/ffi目录执行如下
node-gyp rebuild --target=2.0.4 --arch=x64 --dist-url=https://npm.taobao.org/mirrors/atom/
遇到问题:
一开始我是这么写的。有问题,进去确认是发出去了消息,但是记事本、word、 资源管理器、等没有收到。所以没显示出来。
const ffi=require("ffi");
const ref = require('ref')
const path = require('path');
setTimeout(()=>{
//let user32 = path.resolve('resources/dll/user32.dll');
const Dlls = ffi.Library('user32.dll', {
'GetActiveWindow':['int',[]],
'GetFocus':['int',[]],
'ReleaseCapture':['bool',[]],
'GetForegroundWindow':['int',[]],
'SetForegroundWindow':['bool',['int']],
'SetActiveWindow':['bool',['int']],
'UpdateWindow':['bool',['int']],
'BringWindowToTop':['int',['int']],
'SetFocus':['int',['int']],
'ShowWindow':['int', ['int','int']],
'keybd_event':['void',['int'