Utilizzando keySend con Windows Media Center
-
05-10-2019 - |
Domanda
Ehi, sto usando C # per provare a inviare comandi da tastiera per Windows Media Center in Windows 7.
Al momento posso inviare le chiavi come 4 e vedere il numero 4 appare al centro di Windows Media.
Il problema è qualsiasi combinazione di tasti come CTRL + P (per mettere in pausa un film) non sembra avere alcun effetto sul media center.
Qualsiasi aiuto sarebbe molto apprezzato. Ecco il mio frammento di codice.
// 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;
}
Soluzione 2
Mi è stato effettivamente in grado di trovare finalmente una soluzione che ha lavorato su questo sito:
http://michbex.com/wordpress/?p=3
Ho finito per usare la sua classe e VK mittente remoto I metodi di classe per risolvere questo problema. media center Windows deve avere ganci chiave di livello inferiore ed è necessario implementare una soluzione keyup / keydown invio al sfruttati i ganci.
posso finalmente mettere in pausa un film! Io ripulire il codice e inviare in un secondo momento.
Altri suggerimenti
Io in realtà non è stato in grado di raggiungere utile qualsiasi cosa con la classe VK. MediaCenter non sarebbe rispondere a questa keydown / keyup roba.
Invece ho usato questo metodo per portare il media center di fronte:
[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.
}
In seguito, SendKeys dovrebbe funzionare. Ho appena avvolto intorno try / catch.
private void SendKey(string key)
{
activateMediaCenterForm();
try
{
SendKeys.SendWait(key);
}
catch (Exception e)
{
//Handle exception, if needed.
}
}
Ora SendKey("{ENTER}");
così come SendKey("{RIGHT}");
e tutti gli altri tasti funziona bene su Windows 7.