Skip to content

屏幕操作

提供一些针对用户屏幕的操作

utools.screenColorPick(callback)

获取用户选择的颜色,会弹出一个系统取色器

类型定义

ts
function screenColorPick(
  callback: (colors: { hex: string; rgb: string }) => void
): void;
  • callback: 颜色选择后的回调
    • colors: 颜色对象
      • hex: 十六进制颜色值
      • rgb: RGB 颜色值

示例代码

ts
utools.screenColorPick((colors) => {
  const { hex, rgb } = colors;
  console.log(hex, rgb);
});

utools.screenCapture(callback)

屏幕截图,会进入截图模式,用户可以进行截图操作

类型定义

ts
function screenCapture(callback: (data: string) => void): void;

data 为截图的 base64 数据

示例代码

ts
utools.screenCapture((data) => {
  fs.writeFileSync("./test.png", data, "base64");
});

utools.getPrimaryDisplay()

获取主显示器

类型定义

ts
function getPrimaryDisplay(): Display;

提示

在下列获取屏幕对象时,Display 类型定义见 Display

示例代码

ts
const display = utools.getPrimaryDisplay();
console.log(display);

utools.getAllDisplays()

获取所有显示器

类型定义

ts
function getAllDisplays(): Display[];

示例代码

ts
const displays = utools.getAllDisplays();
console.log(displays);

utools.getCursorScreenPoint()

获取鼠标当前位置,为鼠标在系统中的绝对位置

类型定义

ts
function getCursorScreenPoint(): { x: number; y: number };

示例代码

ts
const { x, y } = utools.getCursorScreenPoint();
console.log(x, y);

utools.getDisplayNearestPoint(point)

获取位置所在的显示器

类型定义

ts
function getDisplayNearestPoint(point: { x: number; y: number }): Display;

示例代码

ts
const display = utools.getDisplayNearestPoint({ x: 100, y: 100 });
console.log(display);

utools.getDisplayMatching(rect)

获取矩形所在的显示器

类型定义

ts
function getDisplayMatching(rect: {
  x: number;
  y: number;
  width: number;
  height: number;
}): Display;

示例代码

ts
const display = utools.getDisplayMatching({
  x: 100,
  y: 100,
  width: 200,
  height: 200,
});
console.log(display);

utools.screenToDipPoint(point)

屏幕物理坐标转 DIP 坐标

类型定义

ts
function screenToDipPoint(point: { x: number; y: number }): {
  x: number;
  y: number;
};

示例代码

ts
const dipPoint = utools.screenToDipPoint({ x: 200, y: 200 });
console.log(dipPoint);

utools.dipToScreenPoint(point)

屏幕 DIP 坐标转物理坐标

类型定义

ts
function dipToScreenPoint(point: { x: number; y: number }): {
  x: number;
  y: number;
};

示例代码

ts
const screenPoint = utools.dipToScreenPoint({ x: 200, y: 200 });
console.log(screenPoint);

utools.screenToDipRect(rect)

屏幕物理区域转 DIP 区域

类型定义

ts
function screenToDipRect(rect: {
  x: number;
  y: number;
  width: number;
  height: number;
}): {
  x: number;
  y: number;
  width: number;
  height: number;
};

示例代码

ts
const dipRect = utools.screenToDipRect({ x: 0, y: 0, width: 200, height: 200 });
console.log(dipRect);

utools.dipToScreenRect(rect)

DIP 区域转屏幕物理区域

类型定义

ts
function dipToScreenRect(rect: {
  x: number;
  y: number;
  width: number;
  height: number;
}): {
  x: number;

  y: number;
  width: number;
  height: number;
};

示例代码

ts
const rect = utools.dipToScreenRect({ x: 0, y: 0, width: 200, height: 200 });
console.log(rect);

utools.desktopCaptureSources(options)

获取桌面录屏源

类型定义

ts
function desktopCaptureSources(
  options: DesktopCaptureSourcesOptions
): Promise<DesktopCaptureSource[]>;

用法请参考utools.desktopCaptureSources

示例代码

ts
(async () => {
  const ousrces = await utools.desktopCaptureSources({
    types: ["window", "screen"],
  });
  const stream = await navigator.mediaDevices.getUserMedia({
    audio: false,
    video: {
      mandatory: {
        chromeMediaSource: "desktop",
        chromeMediaSourceId: ousrces[0].id,
        minWidth: 1280,
        maxWidth: 1280,
        minHeight: 720,
        maxHeight: 720,
      },
    },
  });
  const video = document.querySelector("video");
  video.srcObject = stream;
  video.onloadedmetadata = (e) => video.play();
})();