Comments (11)
Hi @DanielTran3 π
The focus test on CI runs just fine, so far I haven't noticed this behaviour.
There are some scenarios where the OS does mitigate focusing a window, but without further info I can't say anything about it.
from nut.js.
It seems to have an issue if when I use nodejs to open a window using spawn()
, then trying to focus on it after the window is up
from nut.js.
Ok, over the last couple of days I read more about focus rules on Windows than I initially anticipated, but I think I spotted the problem and also might already have a possible solution. Will need to investigate a bit.
from nut.js.
@DanielTran3 Please give @nut-tree/nut-js@next
a try
from nut.js.
Hmmm, looks like it's still having difficulties focusing. I spawn a Revit window as the application and have it poll to focus, but looks like it still doesn't focus.
I pulled @nut-tree/nut-js@next
and tried on version: ^4.0.1-next.20240318211618
Could not find Revit instance, starting one...
Window Found
Focusing on Revit window
Focus: false
Focus: false
Focus: false
Focus: false
Focus: false
from nut.js.
Also, looks like if I manually focus on the window, it will not say that it has focused, where as on v4.0.0 this was the case
from nut.js.
@DanielTran3 My own tests regarding window focus have passed the expectations I had given the context of this issue. Please provide the application youβre working with and the exact code how itβs launched so I can investigate properly.
from nut.js.
I am working in VS Code, using Node v21.1.0 in a Playwright project using Typescript.
This is the code I'm using to launch Autodesk Revit and to try to focus on that window based on the title of the application
let sleepTime = 1000;
keyboard.config.autoDelayMs = 10
/**
* Find a window based on the title of the window
* @param tries The number of attempts to find the window (spaced apart by the default 1s)
* @param titles A list containing strings that show up in the application title. It will
* search for substrings from this list
* @returns The window if it finds one, null otherwise
*/
async function findWindow(tries: number, ...titles: string[]): Promise<screen.Window> {
while (tries != 0) {
let matches = 0;
let windows = await getWindows();
for (let window of windows) {
let windowTitle = await window.title;
for (let title of titles) {
if (windowTitle.includes(title)) {
matches += 1;
}
else {
break;
}
}
if (matches == titles.length) {
console.log("Window Found");
return Promise.resolve(window);
}
}
tries--;
await sleep(sleepTime);
}
return Promise.resolve(null);
}
/**
* Attempts to focus on a window
* @param tries The number of attempts to find the window (spaced apart by the default 1s)
* @param window The window to focus on
* @returns True/false if the window has been focused on
*/
async function focusWindow(tries: number, window: screen.Window): Promise<Boolean> {
await sleep(sleepTime);
let focused = await window.focus();
while (!focused && tries != 0) {
tries--;
await sleep(sleepTime);
focused = await window.focus();
console.log("Focus: %s", focused);
}
return Promise.resolve(focused);
}
/**
* Launches the specified project in the specified version of Revit
* @param revitVersion The version of Revit to launch (from the revit-versions.json file)
* @param revitProject The Revit project to open (from the revit-projects.json file)
* @param detach True will keep the Revit application open after the test finishes, false will close it
* @returns Returns the Revit window
*/
export async function launchRevit(revitVersion: RevitVersion, revitProject: RevitProject, detach: boolean = false): Promise<screen.Window> {
try {
// Find the Revit window and if it doesn't exist, then launch Revit
let revit = await findWindow(3, revitVersion.name, revitProject.revitFilename);
if (revit == null) {
console.log("Could not find Revit instance, starting one...");
spawn(revitVersion.path, [revitProject.path], {detached: detach});
revit = await findWindow(60, revitVersion.name, revitProject.revitFilename);
}
// Focus on the Revit window
console.log("Focusing on Revit window");
let windowFocused = await focusWindow(30, revit)
if (!windowFocused) {
return Promise.resolve(null);
}
revit = await getActiveWindow()
return Promise.resolve(revit)
} catch (e) {
console.log(e)
}
return Promise.resolve(null);
}
from nut.js.
@DanielTran3 Judging from your code snippet and given the fact that my test setup looks almost the same, I can't tell why you're running into problems without a detailed investigation.
If you're interested, feel free to reach out to [email protected]
from nut.js.
I am working in VS Code, using Node v21.1.0 in a Playwright project using Typescript.
This is the code I'm using to launch Autodesk Revit and to try to focus on that window based on the title of the application
let sleepTime = 1000; keyboard.config.autoDelayMs = 10 /** * Find a window based on the title of the window * @param tries The number of attempts to find the window (spaced apart by the default 1s) * @param titles A list containing strings that show up in the application title. It will * search for substrings from this list * @returns The window if it finds one, null otherwise */ async function findWindow(tries: number, ...titles: string[]): Promise<screen.Window> { while (tries != 0) { let matches = 0; let windows = await getWindows(); for (let window of windows) { let windowTitle = await window.title; for (let title of titles) { if (windowTitle.includes(title)) { matches += 1; } else { break; } } if (matches == titles.length) { console.log("Window Found"); return Promise.resolve(window); } } tries--; await sleep(sleepTime); } return Promise.resolve(null); } /** * Attempts to focus on a window * @param tries The number of attempts to find the window (spaced apart by the default 1s) * @param window The window to focus on * @returns True/false if the window has been focused on */ async function focusWindow(tries: number, window: screen.Window): Promise<Boolean> { await sleep(sleepTime); let focused = await window.focus(); while (!focused && tries != 0) { tries--; await sleep(sleepTime); focused = await window.focus(); console.log("Focus: %s", focused); } return Promise.resolve(focused); } /** * Launches the specified project in the specified version of Revit * @param revitVersion The version of Revit to launch (from the revit-versions.json file) * @param revitProject The Revit project to open (from the revit-projects.json file) * @param detach True will keep the Revit application open after the test finishes, false will close it * @returns Returns the Revit window */ export async function launchRevit(revitVersion: RevitVersion, revitProject: RevitProject, detach: boolean = false): Promise<screen.Window> { try { // Find the Revit window and if it doesn't exist, then launch Revit let revit = await findWindow(3, revitVersion.name, revitProject.revitFilename); if (revit == null) { console.log("Could not find Revit instance, starting one..."); spawn(revitVersion.path, [revitProject.path], {detached: detach}); revit = await findWindow(60, revitVersion.name, revitProject.revitFilename); } // Focus on the Revit window console.log("Focusing on Revit window"); let windowFocused = await focusWindow(30, revit) if (!windowFocused) { return Promise.resolve(null); } revit = await getActiveWindow() return Promise.resolve(revit) } catch (e) { console.log(e) } return Promise.resolve(null); }
Were you able to find a solution to this? Works fine on mac but fails to focus a previous window on Windows.
For now, I'm Alt+Tab'ing to switch the previous app but this is not a long term solution.
from nut.js.
All I can add here is that during my tests I didn't notice any problems:
https://share.cleanshot.com/tnYyNrSB
from nut.js.
Related Issues (20)
- Automatically repeat keystrokes for `keyboard.pressKey(...)`
- TypeError when loading nut.js in Electron main process HOT 6
- Update Window tutorial
- Nutjs does not output correctly on window when using boardkey.type () to output Chinese HOT 1
- Package no longer supported [email protected] HOT 1
- Hi From CodeVideo - we're interested in writing a "human-like mouse movement" plugin for nut-js! HOT 2
- OCR says it failed to detect text but seemed to detect it with a high confidence best match HOT 4
- Create a custom plugin example
- 32Bit Electron build create some error HOT 1
- 32 bit UnSupported platfrom how to solve this like prob. HOT 1
- Error: Failed to capture screen HOT 8
- libnut.node: cannot open shared object file: No such file or directory HOT 11
- nl-matcher performance low when run on Electron.
- Not Installing on MAc OS HOT 10
- Minimum/Maximum Screen Dimensions Error HOT 1
- Clarification on number of users in subscription. HOT 4
- window.getElements() return type incorrect
- not word install HOT 1
- npm install fails with 404 error not found in registry HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nut.js.