我正在尝试通过使用user32.dll keybd_event模拟按键来突出显示文本框中的文本(使用 SHIFT + RIGHT_ARROW Win快捷方式),但它不起作用:< / p>
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int SHIFT_LEFT = 0xA0;
public const int RIGHT = 0x27;
....
keybd_event(SHIFT_LEFT, 0, 0, 0);
keybd_event(RIGHT, 0, 0, 0);
keybd_event(RIGHT, 0, 2, 0);
keybd_event(SHIFT_LEFT, 0, 2, 0);
光标向右移动,但文字未突出显示......有人可以解释原因吗?
修改 为什么这适用于Windows OnScreenKeyboard?
KEYEVENTF_EXTENDEDKEY (0x0001):如果指定,则扫描代码前面有一个值为0xE0(224)的前缀字节。
所以,我这样做了:
keybd_event(SHIFT_LEFT, 0, 1 | 0, 0);
keybd_event(RIGHT, 0, 1 | 0, 0);
keybd_event(RIGHT, 0, 1 | 2, 0);
keybd_event(SHIFT_LEFT, 0, 1 | 2, 0);
问题解决了!
有关KEYEVENTF_EXTENDEDKEY的详细说明,请访问here。
答案 0 :(得分:1)
某些功能已在MSDN上公开,我不需要解释。以下是作为代码的基本信息,它在不同的系统上运行:
''' <summary>
''' Simulate key down event on Windows machine
''' </summary>
''' <param name="nCode">key</param>
''' <remarks></remarks>
Public Sub SetKeyDown(ByVal nCode As Integer)
Dim vKey As Byte = Convert.ToByte(nCode)
Dim scanCode As Integer = MapVirtualKey(vKey, 0)
Dim ret As Integer = keybd_event(vKey, CByte(scanCode), KEYEVENTF_EXTENDEDKEY Or 0, IntPtr.Zero)
End Sub
''' <summary>
''' Simulate key up event on Windows machine
''' </summary>
''' <param name="nCode">key</param>
''' <remarks></remarks>
Public Sub SetKeyUp(ByVal nCode As Integer)
Dim vKey As Byte = Convert.ToByte(nCode)
Dim scanCode As Integer = MapVirtualKey(vKey, 0)
Dim ret As Integer = keybd_event(vKey, CByte(scanCode), KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, IntPtr.Zero)
End Sub
Keybd_event:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304%28v=vs.85%29.aspx
MapVirtualKey:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646306%28v=vs.85%29.aspx