Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace robotjs by nutjs #2295

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"notarize": {
"teamId": "F8AH6NHVY5"
},

"icon": "icons/icon.png"
},
"linux": {
Expand Down Expand Up @@ -92,7 +91,7 @@
"request": "^2.88.2",
"request-progress": "^3.0.0",
"ulid": "^2.3.0",
"@hurdlegroup/robotjs": "^0.11.4"
"@nut-tree/nut-js": "^4.0.0"
},
"devDependencies": {
"@electron/notarize": "^2.1.0",
Expand Down
35 changes: 20 additions & 15 deletions electron/utils/selectedText.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { clipboard, globalShortcut } from "electron";
import { keyTap, keys } from "@hurdlegroup/robotjs";
import { clipboard, globalShortcut } from 'electron'
import { keyboard, Key } from '@nut-tree/nut-js'

/**
* Gets selected text by synthesizing the keyboard shortcut
* "CommandOrControl+c" then reading text from the clipboard
*/
export const getSelectedText = async () => {
const currentClipboardContent = clipboard.readText(); // preserve clipboard content
clipboard.clear();
keyTap("c" as keys, process.platform === "darwin" ? "command" : "control");
await new Promise((resolve) => setTimeout(resolve, 200)); // add a delay before checking clipboard
const selectedText = clipboard.readText();
clipboard.writeText(currentClipboardContent);
return selectedText;
};
const currentClipboardContent = clipboard.readText() // preserve clipboard content
clipboard.clear()
const hotkeys: Key[] = [
process.platform === 'darwin' ? Key.LeftCmd : Key.LeftControl,
Key.C,
]
await keyboard.pressKey(...hotkeys)
await keyboard.releaseKey(...hotkeys)
await new Promise((resolve) => setTimeout(resolve, 200)) // add a delay before checking clipboard
const selectedText = clipboard.readText()
clipboard.writeText(currentClipboardContent)
return selectedText
}

/**
* Registers a global shortcut of `accelerator`. The `callback` is called
Expand All @@ -26,14 +31,14 @@ export const registerShortcut = (
callback: (selectedText: string) => void
) => {
return globalShortcut.register(accelerator, async () => {
callback(await getSelectedText());
});
};
callback(await getSelectedText())
})
}

/**
* Unregisters a global shortcut of `accelerator` and
* is equivalent to electron.globalShortcut.unregister
*/
export const unregisterShortcut = (accelerator: Electron.Accelerator) => {
globalShortcut.unregister(accelerator);
};
globalShortcut.unregister(accelerator)
}
Loading