Windows Foreground Love API

0.6.1 · active · verified Wed Apr 22

This package provides a Node.js API wrapper for the Windows API function `AllowSetForegroundWindow`. It enables a process to allow other specified processes, or all processes, to set its foreground window, which is critical for managing window focus behavior in desktop applications, automation scripts, or Electron projects on Windows. The current stable version is 0.6.1, with releases typically occurring as needed for bug fixes, performance enhancements, or build system updates, rather than a fixed schedule. A key differentiator is its direct implementation via Node-API, providing low-level, reliable control over Windows foreground window permissions. It's explicitly designed for the Windows environment, providing no functionality on other operating systems. The package includes comprehensive TypeScript typings, enhancing developer experience and compile-time safety for TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to use `windows-foreground-love` to grant foreground activation permission, both for a specific process (the current one) and globally for all processes, highlighting its Windows-specific behavior.

const { allowSetForegroundWindow } = require('windows-foreground-love');

// Example: Allow the current process to set its own foreground window.
// This is primarily for demonstrating the API, as a process usually
// has foreground rights over its own windows.
const currentPid = process.pid;
console.log(`Attempting to grant foreground rights for PID ${currentPid} (current process)...`);
try {
  const resultCurrent = allowSetForegroundWindow(currentPid);
  console.log(`Result for current PID (${currentPid}): ${resultCurrent}`);
  // On non-Windows, this will return false and do nothing.
  // On Windows, it returns true if successful.
} catch (error) {
  console.error(`Error granting foreground rights for current PID: ${error.message}`);
}

// Example: Allow *all* processes to set the foreground window.
// Use with caution as this can have security implications.
console.log('\nAttempting to allow all processes to set the foreground window...');
try {
  const resultAll = allowSetForegroundWindow();
  console.log(`Result for all processes: ${resultAll}`);
  // On non-Windows, this will return false and do nothing.
} catch (error) {
  console.error(`Error allowing all processes foreground rights: ${error.message}`);
}

view raw JSON →