Joey Morani
Joey Morani

Reputation: 26581

How to simulate a shift press in C# code?

I'm using thiis code to simulate key presses or key ups:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;

// Key up
keybd_event((byte)Convert.ToInt32("A0"), 0, KEYEVENTF_KEYUP | 0, 0);

// Key down
keybd_event((byte)Convert.ToInt32("A0"), 0, KEYEVENTF_EXTENDEDKEY | 0, 0);

According to this table, left-shift has the keycode A0.

For some reason the code above isn't working. Does anyone know why? Thanks a lot for the help! :)

Upvotes: 0

Views: 3795

Answers (2)

gunakkoc
gunakkoc

Reputation: 1069

I would use here for keys.

 keybd_event(160, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);

Upvotes: 0

perilbrain
perilbrain

Reputation: 8197

If using keycode is not necessary then you can use Keys.LShiftKey from KEY enumeration of System.Windows.Forms Namespace.

Further using

keybd_event((byte)0xA0, 0x45, KEYEVENTF_KEYUP | 0, 0); 

is not a bad idea.

Upvotes: 1

Related Questions