Electron中的键盘鼠标操作(Windows环境下)

官网

ROBOTJS官网: https://robotjs.io/docs/
ROBOTJS源码: https://github.com/octalmage/robotjs
NodeJS地址:https://nodejs.org/en/download/
Electron版本: https://electronjs.org/releases/stable

环境设置

Node版本不要用最新版本 用v10.16.3即可

更新npm的包镜像源

1
2
npm config set registry https://registry.npm.taobao.org
npm config list

还原默认配置

1
npm config set registry https://registry.npmjs.org

注意:要用管理员身份运行CMD

1
2
npm install -g --production windows-build-tools
npm install -g node-gyp

如果windows-build-tools删除下面的文件夹(根据自己系统的情况查找)

1
C:\Users\Jian\AppData\Roaming\npm\node_modules\windows-build-tools

重新安装

设置python路径

1
npm config set python C:\Users\Jian\.windows-build-tools\python27\python.exe

构建

下载编译依赖

添加robotjs依赖为Git仓库

1
2
3
"dependencies": {
"robotjs": "git+https://github.com/octalmage/robotjs.git"
}

安装依赖

1
npm install

打开项目下的node_modules下的robotjs文件夹

如果没有生成/build/Release/robotjs.node 就执行下面的操作

下载依赖

1
cnpm install

编译

1
node-gyp rebuild --python C:\Users\Jian\.windows-build-tools\python27\python.exe

构建的robotjs.node的Node版本如果和Electron中的Node版本不一样的话就需要执行下面的rebuild操作

Rebuild方式一(官方)

查看系统的Node版本

1
node -v

获取Electron内部的node版本

在代码中打印信息

1
console.info(process.versions);

结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ares: "1.15.0"
brotli: "1.0.7"
chrome: "76.0.3809.146"
electron: "6.0.12"
http_parser: "2.8.0"
icu: "64.2"
llhttp: "1.1.3"
modules: "73"
napi: "4"
nghttp2: "1.38.0"
node: "12.4.0"
openssl: "1.1.0"
unicode: "12.1"
uv: "1.29.1"
v8: "7.6.303.31-electron.0"
zlib: "1.2.11"

在Electron中使用需要重新构建

1
npm rebuild --runtime=electron --target=1.1.3 --disturl=https://atom.io/download/atom-shell --abi=48

其中的

Target should be the Electron version, and abi should be the abi version of the included Node.

所以

1
electron: "6.0.12"

推断出

1
--target=6.0.12

node和abi的对应关系:参见

但是

1
node: "12.4.0"

并没有在对应表里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{
"10.15.3": {
"node_abi": 64,
"v8": "6.8"
},
"11.0.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.1.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.2.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.3.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.4.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.5.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.6.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.7.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.8.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.9.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.10.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.10.1": {
"node_abi": 67,
"v8": "7.0"
},
"11.11.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.12.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.13.0": {
"node_abi": 67,
"v8": "7.0"
},
"11.14.0": {
"node_abi": 67,
"v8": "7.0"
},
"12.0.0": {
"node_abi": 72,
"v8": "7.4"
}
}

但是我们发现每一个大的node版本对应的node_abi的版本都是一样的

所以

1
--abi=72

整体的命令就是

1
npm rebuild --runtime=electron --target=6.0.12 --disturl=https://atom.io/download/atom-shell --abi=72

当然也可以配置到 package.json

1
"rebuild": "npm rebuild --runtime=electron --target=6.0.12 --disturl=https://atom.io/download/atom-shell --abi=72"

至此 RobotJS就能正常使用了

Rebuild方式二(推荐)

安装electron-rebuild插件,能够方便遍历node-modules中所有需要rebuild的库进行重编译。

1
npm install electron-rebuild --save

在package.json中配置快捷方式

1
2
3
"scripts": {
"rebuild": ".\\node_modules\\.bin\\electron-rebuild.cmd"
}

或者

推荐 下面的方法能使用淘宝的镜像,防止构建时下载依赖失败

1
2
3
"scripts": {
"rebuild": "electron-rebuild -d=http://npm.taobao.org/mirrors/atom-shell -v 5.0.11 -m=./"
}

-v 为Electron的版本号

之后执行

1
npm run rebuild

操作即可完成electron的重编译。