VirtualKeyCode.MEDIA_PLAY_PAUSE not working

前提是你 提交于 2019-12-04 18:59:32

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!