0
The following code works without problem I made it based on the code available on the internet of Sirmestre, however it only opens/closes the Mainwindow UI, I have tried some things to work but error or does not work on 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();
}
}
}
Functioning of this code is very simple, I press Pgup or the key I configure in the "Getasynckeystate" that it opens/closes the UI, when I change the "Mainwindow" to "Window1" which is another UI in the application it returns an error saying this: "Application" does not contain a definition for "Windows1" and could not find any extension method "Widnows1" that accepts a first argument of type "Application". (In that we keep getting code on the net hehehe).
How so changes the "Mainwindow" to "Window1"? Puts what you tried to do
– StanleyIPC
Application.Current.MainWindow.Hide();
forWindow1 window1 = new Window1
Application.Current.Window1.Hide();
– Guilherme Cunha
The use of your code is still very confusing. I cannot understand what the goal is. Do you want every time you press a key specifically (in this case Pgup) to hide or display the entire application? Just a specific window? How you’re instantiating the class
KeyBoardHooking
? More information...– StanleyIPC
using Model.Libraries.KeyBoardHooking;

public MainWindow() {
 DataContext = this;
 InitializeComponent();
 new KeyBoardHooking();
}
In this case it already enters the loopWhile(true){}
and is always checking on if the key was pressed, if pressed, it goes pro second if where it checks if the application is in Top, if true it hides the application, if false it shows the application. https://gist.github.com/ruidevs/a80afcb9ea83ca3e6f7260d1f1c6f7e3– Guilherme Cunha
What I would like to do, but it doesn’t work is to do the same with the other Window, called Window1, but when I change the "Mainwindow" to Window1 from the second if of
KeyBoardHooking
, it returns the following error: "Application" does not contain a definition for "Windows1" and could not find any extension method "Widnows1" that accepts a first argument of type "Application". https://imgur.com/GLt72do– Guilherme Cunha