将 keySend 与 Windows Media Center 结合使用

发布于 2024-09-24 13:29:02 字数 1038 浏览 1 评论 0原文

嘿,我正在使用 C# 尝试在 Windows 7 中向 Windows Media Center 发送按键命令。

目前我可以发送像 4 这样的按键,并看到数字 4 出现在 Windows Media Center 上。

问题是像 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;
    }

Hey, I am using C# to try to send key commands to windows media center in windows 7.

Currently I can send keys like 4 and see the number 4 appears on the windows media center.

The problem is any key combination like Ctrl+p (to pause a movie) does not seem to have any effects on the media center.

Any help would be greatly appreciated. Here is my code snippet.

    // 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-10-01 13:29:02

实际上,我无法通过 VK 课程取得任何有用的成果。 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 上都可以正常工作。

I actually wasn't able to achieve anything usefull with the VK Class. MediaCenter wouldn't respond to this keydown/keyup stuff.

Instead I used this method to bring the media center to front:

[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.
}

Afterwards, SendKeys should work. I just wrapped it around try/catch.

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

Now SendKey("{ENTER}"); as well as SendKey("{RIGHT}"); and all other keys works just fine on Windows 7.

挽清梦 2024-10-01 13:29:02

我实际上终于找到了适用于该网站的解决方案:

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

我最终使用他的 VK 类和远程发送器类方法来解决这个问题。 Windows 媒体中心必须具有较低级别的键挂钩,并且您必须实现 keyup/keydown 发送解决方案才能利用这些挂钩。

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

I was actually able to finally find a solution that worked on this website:

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

I ended up using his VK Class and Remote Sender Class methods to solve this problem. Windows media center must have lower level key hooks and you must implement a keyup/keydown sending solution to exploited the hooks.

I can finally pause a movie! I will clean up the code and post it later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文