3

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;
    }
Marvin Dickhaus
  • 785
  • 12
  • 27
Scott
  • 993
  • 3
  • 10
  • 28
  • I know it is irrelevant but I've noticed the extern method and what it stands for. – Tarik Sep 25 '10 at 06:14
  • The extern modifier means that the method is implemented outside the C# code. http://msdn.microsoft.com/en-us/library/e59b22c5%28VS.80%29.aspx – Scott Sep 25 '10 at 19:17

2 Answers2

1

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.

Marvin Dickhaus
  • 785
  • 12
  • 27
  • Very cool, sorry you weren't able to get the VK class working. It may be that windows media player updates over the past 2 years broke the VK class. – Scott Feb 27 '13 at 01:17
0

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.

Scott
  • 993
  • 3
  • 10
  • 28