Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Simulate VolumeMute press

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.

like image 295
Ron Avatar asked Jan 23 '11 16:01

Ron


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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.

like image 153
Hans Passant Avatar answered Sep 20 '22 15:09

Hans Passant