Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# P/Invoke keyboard_event

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.

like image 444
Bali C Avatar asked Dec 22 '22 04:12

Bali C


1 Answers

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
like image 50
ken2k Avatar answered Dec 30 '22 04:12

ken2k