gpt4 book ai didi

node.js - 如何使用node.js的ffi将输入键发送到窗口

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:26 30 4
gpt4 key购买 nike

如何使用 node.js ffi 将输入发送到窗口?

我的目的是向窗口输入文本和按键,无论它是在前台还是后台(如果可能的话)。

最佳答案

您可以尝试其中一个功能。

我收集了这三种按键模拟方法供个人使用,所以有点乱。但既然你的问题还没有得到任何答案,我认为给出一个困惑的答案总比什么都不回答要好。

import robot from "robotjs";
import keycode from "keycode";
import ffi from "ffi";
import ref from "ref";
import StructType from "ref-struct";
//var ArrayType = require("ref-array");
var arch = require("os").arch();

declare var Buffer;

var user32 = new ffi.Library("user32", {
"GetForegroundWindow": ["int32", []],
"GetWindowTextA": ["int32", ["int32", "string", "int32"]],
//"GetWindowTextW": ["int32", ["int32", ref.refType("string"), "int32"]],
});

export function GetForegroundWindowHandle() {
return user32.GetForegroundWindow();
}
export function GetForegroundWindowText() {
var buffer = new Buffer(256);
buffer.type = ref.types.CString;
var handle = GetForegroundWindowHandle();
let length = user32.GetWindowTextA(handle, buffer, 256);
return buffer.toString().substr(0, length);
}

const WM_KEYDOWN = 0x0100; // key-down
const WM_KEYUP = 0x0101; // key-up
const WM_SYSKEYDOWN = 0x0104; // key-down (for alt-key)
const WM_SYSKEYUP = 0x0105; // key-up (for alt-key)

const extraKeys = {
leftShift: 160,
rightShift: 161,
leftControl: 162,
rightControl: 163,
leftAlt: 164,
rightAlt: 165
};

var intPtr = ref.refType("int");

var Input = StructType({
"type": "int",
"???": "int", // for some reason, the wScan value is only recognized as the wScan value when we add this filler slot
"wVK": "short",
"wScan": "short",
"dwFlags": "int",
"time": "int",
"dwExtraInfo": "int64"
//"dwExtraInfo": "int"
});
var InputPtr = ref.refType(Input);

var user32 = ffi.Library("user32", {
keybd_event: ["void", ["int32", "int32", "int32", "int32"]],
SendMessageA: ["int32", ["long", "int32", "long", "int32"]],
PostMessageA: ["int32", ["long", "int32", "long", "int32"]],
SendInput: ["int", ["int", Input, "int"]],
//SendInput: ["int", ["uint", ArrayType(keyboardInputPtr, 10), "int"]],
//MapVirtualKeyEx: ["uint", ["uint", "uint", intPtr]],

// external use
GetKeyState: ["short", ["int"]], // not really needed, since we track each key ourselves
});

// external use
// ==========

export function GetKeyState(keyCode: number) {
return user32.GetKeyState(keyCode);
}

// general
// ==========

enum keybd_event_flags {
KEYEVENTF_EXTENDEDKEY = 0x0001, // key-down flag (kind of)
KEYEVENTF_KEYUP = 0x0002, // key-up flag
}
export enum SendMessage_Flags {
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_CHAR = 0x0102,
}

export function KeyToggle(keyCode: number, type = "down" as "down" | "up") {
/*if (keyCode == extraKeys.leftControl) user32.PostMessageA(GetForegroundWindowHandle(), messageType, keyCode, 0);
else if (keyCode == extraKeys.leftShift) user32.PostMessageA(GetForegroundWindowHandle(), messageType, keyCode, 0);
else*/

/*if (keyCode == extraKeys.leftControl) return void robot.keyToggle("control", type);
if (keyCode == extraKeys.leftShift) return void robot.keyToggle("shift", type);
if (keyCode == extraKeys.leftAlt) return void robot.keyToggle("alt", type);*/

/*if (keyCode == keycode.codes.ctrl || keyCode == extraKeys.leftShift || keyCode == extraKeys.leftAlt) {
user32.keybd_event(keyCode, 0, type == "down" ? 0 : keybd_event_flags.KEYEVENTF_KEYUP, 0);
return;
}*/

/*if (keyCode == extraKeys.leftControl) keyCode = keycode.codes.ctrl;
else if (keyCode == extraKeys.leftControl) keyCode = keycode.codes.shift;
else if (keyCode == extraKeys.leftAlt) keyCode = keycode.codes.alt;*/

let messageType = type == "down" ? SendMessage_Flags.WM_KEYDOWN : SendMessage_Flags.WM_KEYUP;
//if (keyCode == extraKeys.leftControl || keyCode == extraKeys.leftShift || keyCode == extraKeys.leftAlt) {
if (keyCode == extraKeys.leftAlt) {
messageType = type == "down" ? WM_SYSKEYDOWN : WM_SYSKEYUP;
}

user32.PostMessageA(GetForegroundWindowHandle(), messageType, keyCode, 0);
}
export function KeyTap(keyCode: number, includeCharEvent = false) {
/*IgnoreNextKeyPress(keyCode);
IgnoreNextKeyRelease(keyCode);
robot.keyTap(keyName);*/
KeyToggle(keyCode, "down");
/*if (includeCharEvent) {
user32.PostMessageA(GetForegroundWindowHandle(), SendMessage_Flags.WM_CHAR, keyCode, 0x00390001);
}*/
KeyToggle(keyCode, "up");
}

const INPUT_KEYBOARD = 1;

const KEYEVENTF_EXTENDEDKEY = 0x0001;
const KEYEVENTF_KEYUP = 0x0002;
const KEYEVENTF_UNICODE = 0x0004;
const KEYEVENTF_SCANCODE = 0x0008;

const MAPVK_VK_TO_VSC = 0;

export function ConvertKeyCodeToScanCode(keyCode: number) {
let keys = "**1234567890-=**qwertyuiop[]**asdfghjkl;'`*\\zxcvbnm,./".split("");
return keys.indexOf(String.fromCharCode(keyCode).toLowerCase());
}

//var EntryArray = ArrayType(keyboardInput)
export function KeyToggle2(keyCode: number, type = "down" as "down" | "up", asScanCode = false) {
let entry = new Input();
entry.type = INPUT_KEYBOARD;
entry.time = 0;
entry.dwExtraInfo = 0;

// (virtual) key-code approach (default)
if (!asScanCode) {
entry.dwFlags = type == "down" ? 0 : KEYEVENTF_KEYUP;
entry.wVK = keyCode;
//info.wScan = 0x0200;
entry.wScan = 0;
}
// scan-code approach
else {
//keyCode = 16 + (keyCode - keycode.codes.a);
//let scanCode = user32.MapVirtualKeyEx(keyCode, MAPVK_VK_TO_VSC);
let scanCode = ConvertKeyCodeToScanCode(keyCode);

entry.dwFlags = type == "down" ? KEYEVENTF_SCANCODE : KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
entry.wVK = 0;
entry.wScan = scanCode;
}

/*var array = new EntryArray(1) // by length
array[0] = entry;*/
//var r = user32.SendInput(1, entry.ref(), 28);
let result = user32.SendInput(1, entry, arch === "x64" ? 40 : 28);
console.log(result);
}
export function KeyTap2(keyCode: number, asScanCode = false) {
KeyToggle2(keyCode, "down", asScanCode);
KeyToggle2(keyCode, "up", asScanCode);
}

export function KeyToggle3(keyCode: number, type = "down" as "down" | "up") {
let messageType = type == "down" ? SendMessage_Flags.WM_KEYDOWN : SendMessage_Flags.WM_KEYUP;
//if (keyCode == extraKeys.leftControl || keyCode == extraKeys.leftShift || keyCode == extraKeys.leftAlt) {
if (keyCode == extraKeys.leftAlt) {
messageType = type == "down" ? WM_SYSKEYDOWN : WM_SYSKEYUP;
}

if (type == "down") {
IgnoreNextKeyPress(keyCode);
user32.keybd_event(keyCode, 0, 0, 0);
//user32.keybd_event(keyCode, 0, keybd_event_flags.KEYEVENTF_EXTENDEDKEY, 0);

user32.PostMessageA(GetForegroundWindowHandle(), SendMessage_Flags.WM_KEYDOWN, keyCode, 0);
} else {
IgnoreNextKeyRelease(keyCode);
user32.keybd_event(keyCode, 0, keybd_event_flags.KEYEVENTF_KEYUP, 0);
//user32.keybd_event(keyCode, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

user32.PostMessageA(GetForegroundWindowHandle(), SendMessage_Flags.WM_KEYUP, keyCode, 0);
}
}
export function KeyTap3(keyCode: number) {
KeyToggle3(keyCode, "down");
KeyToggle3(keyCode, "up");
}

再说一次,这真的很困惑,我不记得它们是否都有效。我知道至少 KeyTap2 可以工作(asScanCode = true),因为那是我当前正在使用的。

但是,请注意,KeyTap2 不允许您定位背景窗口 - 只能定位当前具有焦点的任何窗口。其他的似乎可以让您定位后台窗口,尽管我还没有在后台窗口上进行测试。

注意:可以在这里找到此答案的更清晰版本(尽管仅包括 KeyTap2 方法):https://stackoverflow.com/a/50412529/2441655

关于node.js - 如何使用node.js的ffi将输入键发送到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46916722/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com