C#中用sendinput如何模拟Ctrl、Win、Alt等按键

Inpool 2008-09-16 04:55:38
不要说用keybd_event。

不知道他们的虚拟码是多少?或者是没有?

Keys.Control = 131072的数值代表什么,如果是扫描码的话那Ctrl的虚拟码该怎么设置。

请高手赐教。
...全文
1579 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yymycabbage 2009-07-22
  • 打赏
  • 举报
回复
刚刚也使用类似代码,可以弹起的
enaking 2008-09-17
  • 打赏
  • 举报
回复
也就是你只发了按下键的消息,而没发keyup的消息
ZengHD 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Inpool 的回复:]
再问,sendinput发送Alt键的弹起操作不起作用是怎么回事,程序关了那个键还是按下状态,必须手动按一下键盘上的Alt键才行,怎么解决?急~~
[/Quote]

好像没有这个问题啊
怎么测试出没有弹出呢
Inpool 2008-09-17
  • 打赏
  • 举报
回复
发现问题了,我原来把
        [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;
}
ZengHD 2008-09-17
  • 打赏
  • 举报
回复
UINT SendInput(
UINT nInputs, // count of input events
LPINPUT pInputs, // array of input events
int cbSize // size of structure
);

第一个参数应该是按键的次数
我测试过修改为4后就没有问题了

睡觉
Inpool 2008-09-17
  • 打赏
  • 举报
回复
我测试的时候第一个参数是4的,不行

上面这是我修改过的代码,贴上来的时候忘了改过来
ZengHD 2008-09-17
  • 打赏
  • 举报
回复
this.Text = API.SendInput(4,inDow, Marshal.SizeOf(inDow[0])).ToString();
第一个参数
Inpool 2008-09-17
  • 打赏
  • 举报
回复
To: ZengHD

下面是测试代码

private void button1_Click_1(object sender, EventArgs e)
{
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键才恢复正常。
是什么原因,该如何解决,希望帮助解答,给你加分,谢谢!
Inpool 2008-09-16
  • 打赏
  • 举报
回复
自己顶,在线等。
ZengHD 2008-09-16
  • 打赏
  • 举报
回复
还有问题??
Inpool 2008-09-16
  • 打赏
  • 举报
回复
没人能解答么?
Inpool 2008-09-16
  • 打赏
  • 举报
回复
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.
Inpool 2008-09-16
  • 打赏
  • 举报
回复
再问,sendinput发送Alt键的弹起操作不起作用是怎么回事,程序关了那个键还是按下状态,必须手动按一下键盘上的Alt键才行,怎么解决?急~~
wdgphc 2008-09-16
  • 打赏
  • 举报
回复
http://www.cnblogs.com/thunderdanky/articles/846890.html
Inpool 2008-09-16
  • 打赏
  • 举报
回复
还有一个问题,用上面的方法我发现Ctrl、Alt和Win键都不能弹起,弹起的那个怎么设都没用是怎么回事?
ZengHD 2008-09-16
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
SendKeys.Send("+a");
}
ZengHD 2008-09-16
  • 打赏
  • 举报
回复
Shift +

Ctrl ^

Alt %

ZengHD 2008-09-16
  • 打赏
  • 举报
回复

[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]));
}

110,566

社区成员

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

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

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