如何模拟多媒体按键(C语言)?

发布于 2024-09-04 06:56:43 字数 100 浏览 9 评论 0原文

现代键盘具有特殊的多媒体键,例如“暂停/播放”或“打开网络浏览器”。是否可以编写一个“按下”这些键的程序?

我更喜欢 C 语言的解决方案,但我也接受与语言无关的解决方案。

Modern keyboards have special multimedia keys, e.g. 'Pause/Play' or 'Open Web Browser'. Is it possible to write a program that "presses" these keys?

I would prefer solution in C, but I would accept a language agnostic solution, too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2024-09-11 06:56:43

如果您正在谈论 Win32 下的编程,请使用 SendInput Windows API。

您需要构建 INPUT 结构,将类型成员设置为 INPUT_KEYBOARD。在 ki 成员(KEYBDINPUT 类型)中,您可以将 vk(虚拟密钥)设置为您所需的 VK 代码(例如,VK_MEDIA_NEXT_TRACK、VK_MEDIA_STOP)。

虚拟键代码:http://msdn.microsoft。 com/en-us/library/dd375731(v=VS.85).aspx

SendInput 函数:http://msdn.microsoft.com/en-us/library/ms646310(v=VS.85).aspx

我尚未测试以下内容,但应该是这样的:

KEYBDINPUT kbi;
kbi.wVk = VK_MEDIA_STOP; // Provide your own
kbi.wScan = 0;
kbi.dwFlags = 0;  // See docs for flags (mm keys may need Extended key flag)
kbi.time = 0;
kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

INPUT input;
input.type = INPUT_KEYBOARD;
input.ki   = kbi;

SendInput(1, &input, sizeof(INPUT));

Use the SendInput Windows API, if you are talking about programming under Win32.

You need to build INPUT structures, setting the type member to INPUT_KEYBOARD. In the ki member (KEYBDINPUT type), you can set vk (virtual key) to your desired VK code (for example, VK_MEDIA_NEXT_TRACK, VK_MEDIA_STOP).

Virtual key codes: http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx

SendInput Function: http://msdn.microsoft.com/en-us/library/ms646310(v=VS.85).aspx

I've not tested the following, but should be like this:

KEYBDINPUT kbi;
kbi.wVk = VK_MEDIA_STOP; // Provide your own
kbi.wScan = 0;
kbi.dwFlags = 0;  // See docs for flags (mm keys may need Extended key flag)
kbi.time = 0;
kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

INPUT input;
input.type = INPUT_KEYBOARD;
input.ki   = kbi;

SendInput(1, &input, sizeof(INPUT));
冷︶言冷语的世界 2024-09-11 06:56:43

比接受的答案更容易的是 keybd_event没有结构,只是带有数字参数的调用。

// C# example:
public const int KEYEVENTF_EXTENDEDKEY = 1;
public const int KEYEVENTF_KEYUP = 2;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll", SetLastError = true)]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);

keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);

Even more easier than the accepted answer is keybd_event. No structs just a call with numeric parameters.

// C# example:
public const int KEYEVENTF_EXTENDEDKEY = 1;
public const int KEYEVENTF_KEYUP = 2;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;

[DllImport("user32.dll", SetLastError = true)]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);

keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文