I'm simulating a pressed Ctrl button by:
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
keybd_event(VK_CONTROL, 0, 0, 0); //key down
/* Stuff which needs ctrl to be pressed is done here */
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); //key up
This code is located in a .dll file. The dll is used in an WPF application. Now i have the strange behaviour, that the pressed Ctrl key is recognized only if the WPF window is NOT active. Meaning when I click on another window to deactivate the WPF window and force to run the code without activating the WPF window again it works. After clicking on the WPF window it doesn't work anymore.
Any ideas whats the problem here?
Edit: I moved the code from the dll to the wpf application which did not change anything. In the WPF app I tested:
keybd_event(VK_CONTROL, 0, 0, 0);
child.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);