如何使用node.js ffi将输入发送到窗口 我的目的是将文本和键输入到前景或后台的窗口中(如果可能的话)
答案 0 :(得分:0)
您可以尝试其中一种功能。
我收集了这三种用于个人用途的关键模拟方法,因此它有点混乱。但既然你的问题还没有得到任何答案,我认为最好不要回答任何问题。
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