VirtualKeyCode.MEDIA_PLAY_PAUSE不起作用

我希望你们能帮助我。 我有一个用C#编写的小型Windows应用程序,我本质上是一个服务器监听来自android设备的HTTP调用。 该应用程序基本上作为一个遥控器 – 如果它听到静音,它会静音电脑的音量,等等。 我的问题是我有暂停前台应用程序的一些问题。 我特别试图模仿你在这么多键盘上看到的播放/暂停button。 我这样做的方法是使用Windowsinput模拟器项目( http://inputsimulator.codeplex.com/ )来模拟播放/暂停键。 不幸的是,我没有得到任何明显的反应 – 没有什么特别的事情发生。 担心我的设置,我将VirtualKeyCode.MEDIA_PLAY_PAUSE标志切换到静音键标志,并成功地静音我的机器。 请注意,我正在使用Windows Media Player,因此我确信它正在侦听电话。

我想知道的是,有什么额外的设置,我可以暂停任何媒体播放之前,我需要做的? 有没有其他的方式来实现我所追求的,而不是模拟按键? 任何帮助将不胜感激的家伙!

Solutions Collecting From Web of "VirtualKeyCode.MEDIA_PLAY_PAUSE不起作用"

要模拟媒体播放/暂停按钮的按下,您需要将SendMessage传递WM_APPCOMMAND消息。

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

然后当你想模拟按下暂停按钮。

 void AppCommand(AppComandCode commandCode) { var windowInteropHelper = new WindowInteropHelper(this); int CommandID = (int)commandCode << 16; SendMessageW(windowInteropHelper.Handle, WM_APPCOMMAND, windowInteropHelper.Handle, (IntPtr)CommandID); } 

如果您的应用程序不是WPF应用程序,请尝试使用此功能

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

其中命令代码是以下之一:

 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 }