I'm trying to highlight text in text box (with SHIFT+RIGHT_ARROW Win shortcut) by simulating key press with user32.dll keybd_event but it's not working:
[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);
Cursor moves to right but text is not highlighted... Can anyone explain why?
EDIT: Why is this working with Windows OnScreenKeyboard?
KEYEVENTF_EXTENDEDKEY (0x0001): If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
So, I did this:
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);
Problem solved!
Detailed explanation about KEYEVENTF_EXTENDEDKEY can be found here.