api SendInput发送一个字母可以吗?

事理 2010-10-07 04:13:13
如发送一个A字母。下面代码

Send("aaa123aaa");字母的就发送不成功,数字确可以发送成功。如果可以,该怎么修改呢?


[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public ushort wParamL;
public ushort wParamH;
}

[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}

[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, INPUT pInputs, int cbSize);

public static void Send(string message)
{
for (int i = 0; i < message.Length; i++)
{
INPUT input = new INPUT();
input.type = 1;
input.ki.wVk = (ushort)message[i];
input.ki.dwFlags = 1;
SendInput(1, input, Marshal.SizeOf(input));//keydown

INPUT input1 = new INPUT();
input1.type = 1;
input1.ki.wVk = (ushort)message[i];
input1.ki.dwFlags = 0x0002;
SendInput(1, input1, Marshal.SizeOf(input1));//keyup
}
}
...全文
210 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
事理 2010-10-07
  • 打赏
  • 举报
回复
自己实现了,都没几个人回答,分都只有给一个人。
事理 2010-10-07
  • 打赏
  • 举报
回复
不能使用SendKeys和SendMessage,我就想学习一下SendInput这个API

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧