在Windows Media Center中使用keySend

时间:2010-09-25 05:38:37

标签: c# windows-media-center

嘿,我正在使用C#尝试将关键命令发送到Windows 7中的Windows媒体中心。

目前,我可以发送4个密钥,并在Windows媒体中心看到数字4。

问题是任何组合键如Ctrl + p(暂停电影)似乎对媒体中心没有任何影响。

非常感谢任何帮助。这是我的代码段。

    // Get a handle to an application window.
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName,
    string lpWindowName);

    // Activate an application window.
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);


    String HandleClass = "eHome Render Window";
    String HandleWindow = "Windows Media Center";

    private bool SendKeyCommand()
    {
        bool success = true;
        IntPtr PrgHandle = FindWindow(HandleClass, HandleWindow);
        if (PrgHandle == IntPtr.Zero)
        {
            MessageBox.Show(HandleWindow + " is not running");
            return false;
        }
        SetForegroundWindow(PrgHandle);
        SendKeys.SendWait("^p");
        return success;
    }

2 个答案:

答案 0 :(得分:1)

我实际上无法使用VK Class实现任何有用的功能。 MediaCenter不会回应这个keydown / keyup的东西。

相反,我使用这种方法将媒体中心放在前面:

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

public static void activateMediaCenterForm()
{
    System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("ehshell");
    if (p.Length > 0) //found
    {
        SetForegroundWindow(p[0].MainWindowHandle);
    }
    //else not Found -> Do nothing.
}

之后,SendKeys应该工作。我把它包裹在try / catch中。

private void SendKey(string key)
{
    activateMediaCenterForm();
    try
    {
        SendKeys.SendWait(key);
    }
    catch (Exception e)
    {
        //Handle exception, if needed.
    }
}

现在SendKey("{ENTER}");以及SendKey("{RIGHT}");和所有其他键在Windows 7上运行正常。

答案 1 :(得分:0)

我实际上终于找到了可在此网站上运行的解决方案:

http://michbex.com/wordpress/?p=3

我最终使用他的VK Class和Remote Sender Class方法来解决这个问题。 Windows媒体中心必须具有较低级别的密钥挂钩,您必须实现keyup / keydown发送解决方案以利用挂钩。

我终于可以暂停一部电影!我将清理代码并稍后发布。