我需要在 Delphi 中模拟按下多媒体键(例如播放/暂停、上一首/下一首轨道、倒带/前进等)。
我可以使用下一个代码轻松模拟“普通”键:
keybd_event(VK_SPACE,0, 0, 0);
keybd_event(VK_SPACE,0, KEYEVENTF_KEYUP, 0);
另外,我找到了 MAKE/BREAK 代码列表,但我应该如何处理它们?
MSDN 说:
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.
我找到了 Volume Up 的扫描码:
我试过了:
keybd_event(0,$32, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0,$32, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
但也没有运气(这必须模拟 E0 32 E0 32,没有 F0)。 MSDN 还说 bVk 必须是 [1..254],我使用了 0,因为我没有在关键代码列表中找到任何合适的东西。
最佳答案
它在 Delphi XE3 中对我有用:
keybd_event(VK_VOLUME_UP {$AF},0, 0, 0);
keybd_event(VK_VOLUME_UP,0, KEYEVENTF_KEYUP, 0);
如果您的 Delphi 版本中未声明这些常量,请查看 the table here
关于delphi - 在 Delphi 中模拟多媒体按键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23215462/