nut-tree / nut.js

Native UI testing / controlling with node
https://nutjs.dev
2.26k stars 128 forks source link

在mac中调用LeftCmd报错 #510

Closed ittking closed 1 year ago

ittking commented 1 year ago

node:36706) UnhandledPromiseRejectionWarning: Error: [nut.js] - Error: Invalid key flag specified. at libnut. [as keyToggle] (/Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+libnut-darwin@2.5.1/node_modules/@nut-tree/libnut-darwin/permissionCheck.js:68:96) at /Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+nut-js@3.1.1/node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-keyboard.class.js:21:28 at new Promise () at KeyboardAction.key (/Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+nut-js@3.1.1/node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-keyboard.class.js:16:16) at /Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+nut-js@3.1.1/node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-keyboard.class.js:63:38 at new Promise () at KeyboardAction.pressKey (/Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+nut-js@3.1.1/node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-keyboard.class.js:59:16) at /Users/zhangwei/Public/projects/aitools/node_modules/.pnpm/@nut-tree+nut-js@3.1.1/node_modules/@nut-tree/nut-js/dist/lib/keyboard.class.js:83:59 (Use Electron --trace-warnings ... to show where the warning was created)

(node:36706) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

ittking commented 1 year ago

我的代码

export async function insertContentToCursor(text) {
  const oldText = clipboard.readText()
  clipboard.writeText(text)
  if (process.platform === 'darwin') {
    await keyboard.pressKey(Key.LeftCmd, Key.V)
    await keyboard.releaseKey(Key.LeftCmd, Key.V)
  } else {
    await keyboard.pressKey(Key.LeftControl, Key.V)
    await keyboard.releaseKey(Key.LeftControl, Key.V)
  }
  clipboard.writeText(oldText)
  return true
}
s1hofmann commented 1 year ago

Hi @ittking 👋

these keys are not yet supported to be used as modifier keys, see https://github.com/nut-tree/nut.js/issues/490

According to your provided snippet you're trying to create a copy/paste workflow.

I'd suggest using Key.LeftSuper. The "Super" key automatically uses the correct key on the respective platform, so you don't have to do this manually.

export async function insertContentToCursor(text) {
  const oldText = clipboard.readText()
  clipboard.writeText(text)

  await keyboard.pressKey(Key.LeftSuper, Key.V)
  await keyboard.releaseKey(Key.LeftSuper, Key.V)

  clipboard.writeText(oldText)
  return true
}