使用不需要窗口句柄的keybd_event
VB:
Public Class MyKeyPress
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub keybd_event(ByVal bVk As UInteger, ByVal bScan As UInteger, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)
End Sub
' To find other keycodes check bellow link
' http://www.kbdedit.com/manual/low_level_vk_list.html
Public Shared Sub Send(key As Keys)
Select Case key
Case Keys.A
keybd_event(&H41, 0, 0, 0)
Case Keys.Left
keybd_event(&H25, 0, 0, 0)
Case Keys.LShiftKey
keybd_event(&HA0, 0, 0, 0)
Case Keys.RShiftKey
keybd_event(&HA1, 0, 0, 0)
Case Else
Throw New NotImplementedException()
End Select
End Sub
End Class
C#:
public static class MyKeyPress
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);
// To get other key codes check bellow link
// http://www.kbdedit.com/manual/low_level_vk_list.html
public static void Send(Keys key)
{
switch (key)
{
case Keys.A:
keybd_event(0x41, 0, 0, 0);
break;
case Keys.Left:
keybd_event(0x25, 0, 0, 0);
break;
case Keys.LShiftKey:
keybd_event(0xA0, 0, 0, 0);
break;
case Keys.RShiftKey:
keybd_event(0xA1, 0, 0, 0);
break;
default: throw new NotImplementedException();
}
}
}
用法:
MyKeyPress.Send(Keys.LShiftKey)