gpt4 book ai didi

c# - 不能发送单键功能到远程桌面

转载 作者:可可西里 更新时间:2023-11-01 08:29:52 27 4
gpt4 key购买 nike

在深入研究网络后,这是我的代码,不幸的是它没有以大写形式发送 key :/

MapVirtualKey 实现:

    const uint MAPVK_VK_TO_VSC = 0x00;
const uint MAPVK_VSC_TO_VK = 0x01;
const uint MAPVK_VK_TO_CHAR = 0x02;
const uint MAPVK_VSC_TO_VK_EX = 0x03;
const uint MAPVK_VK_TO_VSC_EX = 0x04;

[DllImport("user32.dll")]
public static extern int MapVirtualKey(uint uCode, uint uMapType);

SendInput 实现:

        struct INPUT
{
public UInt32 Type;
public MOUSEKEYBDHARDWAREINPUT Data;

  [StructLayout(LayoutKind.Explicit)]
struct MOUSEKEYBDHARDWAREINPUT
{
[FieldOffset(0)]
public MOUSEINPUT Mouse;

[FieldOffset(0)]
public KEYBDINPUT Keyboard;

[FieldOffset(0)]
public HARDWAREINPUT Hardware;
}


[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);

现在介绍方法:

此方法将 key 作为字符串发送,在远程桌面上工作正常:

        public static void SimulateTextEntry(string text)
{
if (text.Length > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");

var chars = UTF8Encoding.ASCII.GetBytes(text);
var len = chars.Length;
INPUT[] inputList = new INPUT[len * 2];
for (int x = 0; x < len; x++)
{
UInt16 scanCode = chars[x];

var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = 0;
down.Data.Keyboard.Scan = scanCode;
down.Data.Keyboard.Flags = (UInt32)KeyboardFlag.UNICODE;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = 0;
up.Data.Keyboard.Scan = scanCode;
up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE);
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;

// Handle extended keys:
// If the scan code is preceded by a prefix byte that has the value 0xE0 (224),
// we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property.
if ((scanCode & 0xFF00) == 0xE000)
{
down.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
}

inputList[2*x] = down;
inputList[2*x + 1] = up;

}

var numberOfSuccessfulSimulatedInputs = SendInput((UInt32)len*2, inputList, Marshal.SizeOf(typeof(INPUT)));
}

该方法用于按下一个键,它的原始设置为:

    down.Data.Keyboard.Scan = 0;

但我尝试使用 mapvirtualkey,所以请注意更改:

KeyPress:

public static void SimulateKeyPress(VirtualKeyCode keyCode)
{
var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = (UInt16)keyCode;
// down.Data.Keyboard.Scan = 0;
ushort mapvirtualkeyresult = (ushort)(MapVirtualKey((UInt16)keyCode, MAPVK_VK_TO_CHAR));
down.Data.Keyboard.Scan = mapvirtualkeyresult;
down.Data.Keyboard.Flags = 0;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = (UInt16)keyCode;
//up.Data.Keyboard.Scan = 0;
up.Data.Keyboard.Scan = (ushort)(MapVirtualKey((UInt16)keyCode, MAPVK_VK_TO_CHAR));
up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP;
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;

INPUT[] inputList = new INPUT[2];
inputList[0] = down;
inputList[1] = up;

var numberOfSuccessfulSimulatedInputs = SendInput(2, inputList, Marshal.SizeOf(typeof(INPUT)));
if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key press simulation for {0} was not successful.", keyCode));
}

按键:

     public static void SimulateKeyDown(VirtualKeyCode keyCode)
{
var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = (UInt16)keyCode;
down.Data.Keyboard.Scan = (ushort)(MapVirtualKey((UInt16)keyCode, MAPVK_VK_TO_CHAR));
down.Data.Keyboard.Flags = 0;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;

INPUT[] inputList = new INPUT[1];
inputList[0] = down;

var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT)));
if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key down simulation for {0} was not successful.", keyCode));
}

KeyUp:

  public static void SimulateKeyUp(VirtualKeyCode keyCode)
{
var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = (UInt16)keyCode;
up.Data.Keyboard.Scan = (ushort)(MapVirtualKey((UInt16)keyCode, MAPVK_VK_TO_CHAR));
up.Data.Keyboard.Flags = (UInt32)KeyboardFlag.KEYUP;
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;

INPUT[] inputList = new INPUT[1];
inputList[0] = up;

var numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT)));
if (numberOfSuccessfulSimulatedInputs == 0) throw new Exception(string.Format("The key up simulation for {0} was not successful.", keyCode));
}

对于测试:

     int i=0;
while (i<10)
{
Thread.Sleep(5000);
InputSimulator.SimulateTextEntry("text");
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_A);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_B);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_C);
InputSimulator.SimulateKeyDown(VirtualKeyCode.LSHIFT);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_A);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_B);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_C);
InputSimulator.SimulateKeyUp(VirtualKeyCode.LSHIFT);
i++;
}

在我的控制台窗口中,我看到:文本abcABC文本abcABC等等……

在 rdc 控制台窗口中,我只看到:文本文本等等……

好像单键不起作用!

非常感谢任何帮助!提前谢谢,尼维。

最佳答案

不要使用简单的 System.Windows.Forms.SendKeys.SendWait("textabc") 它会起作用

关于c# - 不能发送单键功能到远程桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3554171/

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