gpt4 book ai didi

c# - 使用输入模拟器

转载 作者:行者123 更新时间:2023-11-30 18:23:03 24 4
gpt4 key购买 nike

尝试使用InputSimulator模拟键盘输入。除了使用 sim.Keyboard.ModifiedKeyStroke 模拟 ASCII 字符输入外,一切正常。

我尝试使用以下两种不同的方式模拟 Alt down + numpad1 + numpad4 + numpad7 + Alt up:

    sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, new[] { VirtualKeyCode.NUMPAD1, VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD7});

    sim.Keyboard.KeyDown(VirtualKeyCode.LMENU);
sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD1);
sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD1);
sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD4);
sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD4);
sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD7);
sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD7);
sim.Keyboard.KeyUp(VirtualKeyCode.LMENU);

两者都不起作用。我尝试在控制台中打印出按键状态,真实按键和模拟按键都给出相同的结果:

   LMenu key down
NumPad1 key down
NumPad1 key up
NumPad4 key down
NumPad4 key up
NumPad7 key down
NumPad7 key up
LMenu key up

我认为应该是库的一些问题:issue 1 .有人可以帮我吗?还有其他方法吗?

更新 1

我发现“Alt+Tab”在 Win8 中也不起作用。我认为这可能是同样的原因,所以我先尝试解决这个问题。事实证明它们是两个不同的问题:

  1. 要使“Alt+Tab”正常工作,我需要在“app.manifest”中设置uiAccess=true,并使用测试数字签名对“.exe”文件进行签名;

  2. 模拟输入ASCII字符还是不行。

最佳答案

keybd_eventMapVirtualKey 一起使用,或者如果使用 SendInput 添加到 INPUT 的 Scan MapVirtualKey

这是两个例子,

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace kbd_events_example
{
static class Program
{
#pragma warning disable 649
internal struct INPUT
{
public UInt32 Type;
public KEYBOARDMOUSEHARDWARE Data;
}
[StructLayout(LayoutKind.Explicit)]
//This is KEYBOARD-MOUSE-HARDWARE union INPUT won't work if you remove MOUSE or HARDWARE
internal struct KEYBOARDMOUSEHARDWARE
{
[FieldOffset(0)]
public KEYBDINPUT Keyboard;
[FieldOffset(0)]
public HARDWAREINPUT Hardware;
[FieldOffset(0)]
public MOUSEINPUT Mouse;
}
internal struct KEYBDINPUT
{
public UInt16 Vk;
public UInt16 Scan;
public UInt32 Flags;
public UInt32 Time;
public IntPtr ExtraInfo;
}
internal struct MOUSEINPUT
{
public Int32 X;
public Int32 Y;
public UInt32 MouseData;
public UInt32 Flags;
public UInt32 Time;
public IntPtr ExtraInfo;
}
internal struct HARDWAREINPUT
{
public UInt32 Msg;
public UInt16 ParamL;
public UInt16 ParamH;
}
#pragma warning restore 649
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint extraInfo);
[DllImport("user32.dll", SetLastError = true)]
static extern int MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);
enum VK
{
MENU = 0x12,
NUMPAD0 = 0x60,
NUMPAD1 = 0x61,
NUMPAD2 = 0x62,
NUMPAD3 = 0x63,
NUMPAD4 = 0x64,
NUMPAD5 = 0x65,
NUMPAD6 = 0x66,
NUMPAD7 = 0x67,
NUMPAD8 = 0x68,
NUMPAD9 = 0x69
}
const uint KEYEVENTF_KEYUP = 0x0002;
public const int INPUT_KEYBOARD = 1;
[STAThread]
static void Main()
{
Thread.Sleep(5000); //wait 5 seconds, so you can focus the Notepad
keybd_event((int)VK.MENU, (byte)MapVirtualKey((uint)VK.MENU, 0), 0, 0); //Alt Press
keybd_event((int)VK.NUMPAD1, (byte)MapVirtualKey((uint)VK.NUMPAD1, 0), 0, 0); // N1 Press
keybd_event((int)VK.NUMPAD1, (byte)MapVirtualKey((uint)VK.NUMPAD1, 0), KEYEVENTF_KEYUP, 0); // N1 Release
keybd_event((int)VK.MENU, (byte)MapVirtualKey((uint)VK.MENU, 0), KEYEVENTF_KEYUP, 0); // Alt Release
Thread.Sleep(2000); //wait 2 seconds
INPUT[] inputs = new INPUT[] {
new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.MENU, Flags = 0, Scan = (ushort)MapVirtualKey((uint)VK.MENU, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.NUMPAD2, Flags = 0, Scan = (ushort)MapVirtualKey((uint)VK.NUMPAD2, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.NUMPAD2, Flags = 2, Scan = (ushort)MapVirtualKey((uint)VK.NUMPAD2, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.MENU, Flags = 2, Scan = (ushort)MapVirtualKey((uint)VK.MENU, 0), ExtraInfo = IntPtr.Zero, Time = 0}}}
};
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
}
}
}

对于InputSimulator,您需要使用SendInputkeybd_eventMapVirtualKey编写自己的类来处理输入...

关于c# - 使用输入模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32861108/

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