Skip to content

[Nodejs][cross-platform] 鼠标键盘模拟输入&事件监听、光标位置获取、获取剪贴板文件或文本、获取已安装应用列表和图标、获取活跃&聚焦窗口信息、获取文件管理器实时路径 | Mouse and keyboard simulation input & event listening, cursor position acquisition, get clipboard file or text, get installed application list and icon, get active &focus window information, get file manager real-time path

License

rubickCenter/rubick-native

Repository files navigation

Rubick Native

封装常用跨平台能力

// 安装
pnpm add rubick-native
yarn add rubick-native
npm install rubick-native

贡献

  1. Fork & Clone 本项目
  2. 通过 import { func } from "addon" 调用 Rust 函数
  3. 提起 PR

API

1. Keyborad & Mouse ✅

  1. mouseClick: 鼠标点击
  2. mouseDown: 鼠标按下
  3. mouseUp: 鼠标抬起
  4. mouseLocaion: 获取鼠标位置
出参 eg
{ x: 485, y: 449 }
  1. mouseMove: 鼠标移动
  2. mouseScrollX: 鼠标滚轮左右滑动(正值向右,负值向左)
  3. mouseScrollY: 鼠标滚轮上下滑动(正值向下,负值向上)
  4. sendKeyboardSimulation: 模拟键盘输入
入参 eg
// - 为按下, - 为抬起;所有可用键 https://github.com/enigo-rs/enigo/blob/master/src/keycodes.rs
 {+CTRL}a{-CTRL}{+SHIFT}Hello World{-SHIFT}
  1. onInputEvent: 键鼠事件监听
入参 eg
(event: MouseKeyBoardEvent) => void;
  • callback: function 监听输入事件的函数
event object eg
{
  "time": { "secs_since_epoch": 1695999163, "nanos_since_epoch": 631148700 }, // 触发时间
  "name": "a", // 输入内容
  "event": { 
    "type":"KeyPress", // 事件类型
    "value": "KeyA" // 事件值
  } 
}
  1. grabInputEvent 键鼠事件监听&拦截

比 onInputEvent 多了拦截键鼠事件的功能,但是在 MacOS 下需要被授予 Accessibility 权限此 API 才能工作,故无拦截需求建议使用 onInputEvent

入参 eg
(event: MouseKeyBoardEvent) => boolean;
  • callback: function 监听输入事件的函数, 返回是否将事件发送给系统

2. Clipboard ✅

可以获取剪贴板中复制的内容:

getClipboardContent()

出参 eg
// type: file
{
  type: 'file',
  content: [
    'C:/Download/test.txt',
    'C:/Download/mywork',
  ]
}
// type: text
{
  type: 'text',
  content: 'hello world'
}
  • type: 'file' | 'text'
  • content: Array | string

3. 获取系统已安装的 app 🕑(Mac/Linux 未支持)

原理:解析系统中所有存放快捷方式的文件夹,解析快捷方式生成应用列表(win)

getSystemApp()

获取系统安装的 APP 列表

入参 eg
  • callback: (app: App) => void | Promise 用于接收应用信息的回调函数
  • extraPath: string 额外需要检索的文件夹

App object:

{
  "name": "TIM",
  "description": null,
  "execPath": "C:\\Program Files (x86)\\Tencent\\TIM\\Bin\\QQScLauncher.exe",
  "shortCutPath": "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\腾讯软件\\TIM\\TIM.lnk",
  "workingDir": "C:\\Program Files (x86)\\Tencent\\TIM\\Bin"
}
  • name: 应用的名称
  • description: 应用详情
  • execPath: 应用的路径
  • shortCutPath: 应用快捷方式地址
  • workingDir: 应用工作目录

getAppIcon()

获取 APP 的图标

入参 eg
  • path: string windows 平台下填写 exe 文件地址;
出参 eg
  • img: string 图标的 base64 编码

4. 获取当前活跃窗口 ✅

  1. getActiveWin: 获取当前活跃窗口
  2. getOpenWin: 获取所有打开的窗口信息
出参 eg
{
  "id": 13200, // 进程 pid
  "os": "win32", // 运行平台
  "title": "test.ts - rubick-native - Visual Studio Code", // 窗口标题
  "position": { "x": -7, "y": -7, "width": 1550, "height": 830 }, // 窗口位置大小
  "info": {
    "processId": 13200, // 进程 pid
    "path": "C:\\Program Files\\Microsoft VS Code\\Code.exe", // 进程地址
    "name": "Visual Studio Code", // 进程名称
    "execName": "Code" // 进程名称
  },
  "usage": { "memory": 111030272 }, // 进程内存占用
  "url": "" // 进程关联 URL
}

5. 获取 finder(macos) 或者 explorer(win) 文件夹当前打开的路径 🕑(Linux 未支持)

  1. getFolderOpenPath
出参 eg
"C://Download/mywork";
  • path: string 返回打开的文件夹路径

About

[Nodejs][cross-platform] 鼠标键盘模拟输入&事件监听、光标位置获取、获取剪贴板文件或文本、获取已安装应用列表和图标、获取活跃&聚焦窗口信息、获取文件管理器实时路径 | Mouse and keyboard simulation input & event listening, cursor position acquisition, get clipboard file or text, get installed application list and icon, get active &focus window information, get file manager real-time path

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published