I have been using some P/Invoke code to simulate a key press, but I can't work out how to press more than one key at once. I am trying to simulate pressing and holding CTRL and then pressing C and then V, so just a copy and paste.
The code I am using so far is this, but so far I can only manage to press CTRL, not hold it and press C and V:
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int VK_LCONTROL = 0xA2;
static void Main(string[] args)
{
keybd_event(VK_LCONTROL, 0, 0, 0);
}
I would really appreciate any suggestions. Thanks.
The dwFlags
controls if the key is released or not.
Try the following:
keybd_event(VK_CONTROL, 0, 0, 0);// presses ctrl
keybd_event(0x43, 0, 0, 0); // presses c
keybd_event(0x43, 0, 2, 0); //releases c
keybd_event(VK_CONTROL, 0, 2, 0); //releases ctrl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With