C# 模拟VolumeMute按下

发布于 2024-10-13 12:43:31 字数 671 浏览 1 评论 0原文

我得到以下代码来模拟音量静音按键:

    [DllImport("coredll.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    byte VK_VOLUME_MUTE = 0xAD;
    const int KEYEVENTF_KEYUP = 0x2;
    const int KEYEVENTF_KEYDOWN = 0x0;
    private void button1_Click(object sender, EventArgs e)
    {
            keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYDOWN, 0);
            keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYUP, 0);
    }

此代码不起作用。我知道还有另一种方法可以通过 SendMessageW 静音/取消静音,但我不想使用 SendMessageW,因为我使用 KeyState 来检测是否需要静音或取消静音(如果用户想要取消静音且其已经取消静音)那么我不需要切换 - 这就是为什么我需要模拟 VolumeMute 按键)

谢谢。

I got the following code to simulate volumemute keypress:

    [DllImport("coredll.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    byte VK_VOLUME_MUTE = 0xAD;
    const int KEYEVENTF_KEYUP = 0x2;
    const int KEYEVENTF_KEYDOWN = 0x0;
    private void button1_Click(object sender, EventArgs e)
    {
            keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYDOWN, 0);
            keybd_event(VK_VOLUME_MUTE, 0, KEYEVENTF_KEYUP, 0);
    }

This code doesnt work. I know there's another way to mute/unmute sound by SendMessageW, but I dont want to use SendMessageW because I use KeyState to detect wheter I need to mute the sound or unmute the sound (if the user wants to unmute the sound and its already unmuted then I dont need to toggle - thats why I need to simulate VolumeMute keypress)

Thanks.

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

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

发布评论

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

评论(1

朱染 2024-10-20 12:43:31

它不起作用的第一个原因是因为您使用了错误的 DLL,coredll.dll 是 Windows Mobile。在Windows桌面版本中,keybd_event由user32.dll导出。它不起作用的第二个原因是发送击键不够好。不太清楚为什么,这似乎是在通用输入处理程序之前被拦截的。

您可以使用 WM_APPCOMMAND,它支持一系列命令,APPCOMMAND_VOLUME_MUTE 就是其中之一。它充当开关,打开和关闭静音。让您的代码如下所示:

    private void button1_Click(object sender, EventArgs e) {
        var msg = new Message();
        msg.HWnd = this.Handle;
        msg.Msg = 0x319;              // WM_APPCOMMAND
        msg.WParam = this.Handle;
        msg.LParam = (IntPtr)0x80000; // APPCOMMAND_VOLUME_MUTE
        this.DefWndProc(ref msg);
    }

此代码需要位于表单的实例方法内,注意它如何使用 DefWndProc()。如果你想把它放在其他地方,那么你需要回退到 SendMessage()。实际的窗口句柄并不重要,只要它是有效的顶级窗口句柄即可。

The first reason it doesn't work is because you use the wrong DLL, coredll.dll is Windows Mobile. In the desktop version of Windows, keybd_event is exported by user32.dll. The second reason it doesn't work is because sending the keystroke isn't good enough. Not quite sure why, this seems to be intercepted before the generic input handler.

You can use WM_APPCOMMAND, it supports a range of commands and APPCOMMAND_VOLUME_MUTE is one of them. It acts as a toggle, turning muting on and off. Make your code look like this:

    private void button1_Click(object sender, EventArgs e) {
        var msg = new Message();
        msg.HWnd = this.Handle;
        msg.Msg = 0x319;              // WM_APPCOMMAND
        msg.WParam = this.Handle;
        msg.LParam = (IntPtr)0x80000; // APPCOMMAND_VOLUME_MUTE
        this.DefWndProc(ref msg);
    }

This code needs to be inside a instance method of the form, note how it uses DefWndProc(). If you want to put it elsewhere then you need to fallback to SendMessage(). The actual window handle doesn't matter, as long as it is a valid toplevel one.

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