To the 1st floor and the 2nd floor: It's the sendinput function in win32 Api, Not the send function in the framework class sendkeys.
[DllImport("user32.dll")] public static extern UInt32 SendInput(UInt32 nInputs, INPUT[] pInputs, int cbSize); [DllImport("kernel32.dll")] public static extern int GetTickCount(); [StructLayout(LayoutKind.Explicit)] public struct INPUT { [FieldOffset(0)] public Int32 type; [FieldOffset(4)] public KEYBDINPUT ki; [FieldOffset(4)] public MOUSEINPUT mi; [FieldOffset(4)] public HARDWAREINPUT hi; } [StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT { public Int32 dx; public Int32 dy; public Int32 mouseData; public Int32 dwFlags; public Int32 time; public IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public Int16 wVk; public Int16 wScan; public Int32 dwFlags; public Int32 time; public IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] public struct HARDWAREINPUT { public Int32 uMsg; public Int16 wParamL; public Int16 wParamH; } public const int INPUT_KEYBOARD = 1; public const int KEYEVENTF_KEYUP = 0x0002; private void button1_Click(object sender, EventArgs e) { INPUT[] inDown = new INPUT[4]; inDown[0] = new INPUT(); inDown[1] = new INPUT(); inDown[2] = new INPUT(); inDown[3] = new INPUT(); inDown[0].type = inDown[1].type = inDown[2].type = inDown[3].type = INPUT_KEYBOARD; inDown[0].ki.wVk = inDown[2].ki.wVk =(int) Keys.LWin; inDown[1].ki.wVk = inDown[3].ki.wVk = (int)Keys.R; inDown[2].ki.dwFlags = inDown[3].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(4, inDown, Marshal.SizeOf(inDown[0])); }
UINT SendInput( UINT nInputs, // count of input events LPINPUT pInputs, // array of input events int cbSize // size of structure );第一个参数应该是按键的次数 我测试过修改为4后就没有问题了睡觉
发现问题了,我原来把 [StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public Int16 wVk; public Int16 wScan; public Int32 dwFlags; public Int32 time; public IntPtr dwExtraInfo; } 给定义成下面这样了,浪费了一天时间,郁闷 [StructLayout(LayoutKind.Sequential)] public struct KEYBDINPUT { public int wVk; public int wScan; public Int32 dwFlags; public Int32 time; public IntPtr dwExtraInfo; }
Ctrl ^
Alt %
{
textBox1.Focus();
SendKeys.Send("+a");
}
It's the sendinput function in win32 Api, Not the send function in the framework class sendkeys.
[DllImport("user32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, INPUT[] pInputs, int cbSize);
[DllImport("kernel32.dll")]
public static extern int GetTickCount();
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
[FieldOffset(0)]
public Int32 type;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public HARDWAREINPUT hi;
} [StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
public Int32 dx;
public Int32 dy;
public Int32 mouseData;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public Int16 wVk;
public Int16 wScan;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct HARDWAREINPUT
{
public Int32 uMsg;
public Int16 wParamL;
public Int16 wParamH;
} public const int INPUT_KEYBOARD = 1;
public const int KEYEVENTF_KEYUP = 0x0002; private void button1_Click(object sender, EventArgs e)
{
INPUT[] inDown = new INPUT[4]; inDown[0] = new INPUT();
inDown[1] = new INPUT();
inDown[2] = new INPUT();
inDown[3] = new INPUT(); inDown[0].type = inDown[1].type = inDown[2].type = inDown[3].type = INPUT_KEYBOARD;
inDown[0].ki.wVk = inDown[2].ki.wVk =(int) Keys.LWin;
inDown[1].ki.wVk = inDown[3].ki.wVk = (int)Keys.R; inDown[2].ki.dwFlags = inDown[3].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(4, inDown, Marshal.SizeOf(inDown[0]));
}
{
INPUT[] inDow = new INPUT[4];
int i = 0; while (i < inDow.Length)
{
inDow[i] = new INPUT();
inDow[i++].type = INPUT_KEYBOARD;
} inDow[0].ki.wVk = inDow[3].ki.wVk = (int)Keys.LMenu;
inDow[1].ki.wVk = inDow[2].ki.wVk = (int)Keys.E;
inDow[2].ki.dwFlags = inDow[3].ki.dwFlags = KEYEVENTF_KEYUP; this.Text = API.SendInput(6,inDow, Marshal.SizeOf(inDow[0])).ToString(); }Form上有个文本框,对窗口定义了一个事件的响应,如果按下Alt+E组合键的话,文本框的Text="menu",这点是成功了,但是关闭程序以后发现Alt一直没弹起,也就是inDow[3]这个发送过去但是没起作用,但是发送成功了,因为Form的Text显示了4。
怎么测试出没有弹出呢
关闭程序以后很多鼠标操作都变了,比如双击桌面的程序快捷方式打不开程序,而是打开快捷方式的属性,直到我手动按一下键盘上的LMenu键才恢复正常。
是什么原因,该如何解决,希望帮助解答,给你加分,谢谢!
第一个参数
UINT nInputs, // count of input events
LPINPUT pInputs, // array of input events
int cbSize // size of structure
);第一个参数应该是按键的次数
我测试过修改为4后就没有问题了睡觉
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public Int16 wVk;
public Int16 wScan;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}
给定义成下面这样了,浪费了一天时间,郁闷
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public int wVk;
public int wScan;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}