VirtualKeyCode.MEDIA_PLAY_PAUSE not working

2020-07-27 05:58发布

问题:

I'm hoping you guys can help me out. I have a small windows application written in C# that I'm making that is essentially a server listening for HTTP calls from an android device. The application basically acts as a remote - if it hears mute, it will mute the volume of the computer, and so on. My issue is I'm having some problems with pausing the foreground application. I'm specifically trying to imitate the play/pause button you see on so many keyboards these days. My method for doing so has been to use the Windows Input Simulator project (http://inputsimulator.codeplex.com/) to simulate that play/pause key. Unfortunately, I'm not getting any type of discernible response - nothing in particular is happening. Fearing my set up, I switched the VirtualKeyCode.MEDIA_PLAY_PAUSE flag to the mute key flag and successfully muted my machine. Note that I am using Windows Media Player so I'm confident it's listening for the call.

What I'm wondering is, is there any extra setup I need to do before I can pause whatever media is currently playing? Are there any other ways to achieve what I'm after rather than simulating the key press? Any help would be much appreciated guys!

回答1:

To emulate the pressing of the Media Play / Pause button you need to pass WM_APPCOMMAND messages with SendMessage.

[DllImport("user32.dll")]
extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
int WM_APPCOMMAND = 0x0319;

Then when you want to simulate the pressing of the pause button.

void AppCommand(AppComandCode commandCode)
{
    var windowInteropHelper = new WindowInteropHelper(this);

    int CommandID = (int)commandCode << 16;
    SendMessageW(windowInteropHelper.Handle, WM_APPCOMMAND, windowInteropHelper.Handle, (IntPtr)CommandID);
}

If your application is not a WPF application, try this function instead

void AppCommand(AppComandCode commandCode)
{
    int CommandID = (int)commandCode << 16;
    SendMessageW(Process.GetCurrentProcess().MainWindowHandle, WM_APPCOMMAND, Process.GetCurrentProcess().MainWindowHandle, (IntPtr)CommandID);
}

Where command code is one of the following:

public enum AppComandCode : uint
{
    BASS_BOOST = 20,
    BASS_DOWN = 19,
    BASS_UP = 21,
    BROWSER_BACKWARD = 1,
    BROWSER_FAVORITES = 6,
    BROWSER_FORWARD = 2,
    BROWSER_HOME = 7,
    BROWSER_REFRESH = 3,
    BROWSER_SEARCH = 5,
    BROWSER_STOP = 4,
    LAUNCH_APP1 = 17,
    LAUNCH_APP2 = 18,
    LAUNCH_MAIL = 15,
    LAUNCH_MEDIA_SELECT = 16,
    MEDIA_NEXTTRACK = 11,
    MEDIA_PLAY_PAUSE = 14,
    MEDIA_PREVIOUSTRACK = 12,
    MEDIA_STOP = 13,
    TREBLE_DOWN = 22,
    TREBLE_UP = 23,
    VOLUME_DOWN = 9,
    VOLUME_MUTE = 8,
    VOLUME_UP = 10,
    MICROPHONE_VOLUME_MUTE = 24,
    MICROPHONE_VOLUME_DOWN = 25,
    MICROPHONE_VOLUME_UP = 26,
    CLOSE = 31,
    COPY = 36,
    CORRECTION_LIST = 45,
    CUT = 37,
    DICTATE_OR_COMMAND_CONTROL_TOGGLE = 43,
    FIND = 28,
    FORWARD_MAIL = 40,
    HELP = 27,
    MEDIA_CHANNEL_DOWN = 52,
    MEDIA_CHANNEL_UP = 51,
    MEDIA_FASTFORWARD = 49,
    MEDIA_PAUSE = 47,
    MEDIA_PLAY = 46,
    MEDIA_RECORD = 48,
    MEDIA_REWIND = 50,
    MIC_ON_OFF_TOGGLE = 44,
    NEW = 29,
    OPEN = 30,
    PASTE = 38,
    PRINT = 33,
    REDO = 35,
    REPLY_TO_MAIL = 39,
    SAVE = 32,
    SEND_MAIL = 41,
    SPELL_CHECK = 42,
    UNDO = 34,
    DELETE = 53,
    DWM_FLIP3D = 54
}