The following code works without problem, I did it based on the code available on the internet of SirMestre, however it only opens / closes the MainWindow UI, I have already tried some things to work but the error or does not work in other UI's.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace Model.Libraries.KeyBoardHooking
{
public class KeyBoardHooking
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(Key vKey);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(int vKey);
public KeyBoardHooking()
{
Thread Thread = new Thread(() =>
{
while (true)
{
// tecla abrir/fechar (PgUp)
if (GetAsyncKeyState(33) == -32767)
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
if (Application.Current.MainWindow.Topmost == true)
{
Application.Current.MainWindow.Hide();
Application.Current.MainWindow.Topmost = false;
}
else
{
Application.Current.MainWindow.Show();
Application.Current.MainWindow.Topmost = true;
}
}));
}
Thread.Sleep(1);
}
})
{
IsBackground = true
};
Thread.SetApartmentState(ApartmentState.STA);
Thread.Start();
}
}
}
Function of this code is quite simple, I press PgUp or the key that I configure in "GetAsyncKeyState" that it opens / closes the UI, when I change the "MainWindow" to "Window1" which is another UI in the application it returns a error saying this: "Application" does not contain a setting for "Windows1" and could not find any "Widnows1" extension method that accepts a first argument of type "Application". (That's what you keep getting code on the net hehehe.)