I need to simulate pressing of multimedia keys (such as play/pause, prev/next track, rewind/forward, etc.) in Delphi. I can simulate 'normal' keys easily using the next code:
keybd_event(VK_SPACE,0, 0, 0);
keybd_event(VK_SPACE,0, KEYEVENTF_KEYUP, 0);
Also, I've found the lists of MAKE/BREAK codes, but what should I do with them?
MSDN says:
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWORD dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
);
bVk - Specifies a virtual-key code. The code must be a value in the range 1 to 254.
bScan - Specifies a hardware scan code for the key.
dwFlags - A set of flag bits that specify various aspects of function operation.
An application can use any combination of the following predefined constant
values to set the flags:
KEYEVENTF_EXTENDEDKEY - If specified, the scan code was preceded by a
prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP If specified, the key is being released. If
not specified, the key is being depressed.
dwExtraInfo - Specifies an additional 32-bit value associated with the key stroke.
I've found the scan codes for Volume Up:
Make Code: E0, 32 Break Code: E0, F0, 32
I tried:
keybd_event(0,$32, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0,$32, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
but with no luck too (this must simulate E0 32 E0 32, without F0). Also MSDN says bVk must be [1..254], and I used 0 because I haven't found anything suitable in key codes list.
It works for me in Delphi XE3:
keybd_event(VK_VOLUME_UP {$AF},0, 0, 0);
keybd_event(VK_VOLUME_UP,0, KEYEVENTF_KEYUP, 0);
If these constants are not declared in your Delphi version, look at the table here