Problems with hotkey to open and hide the interface using "Application.Current" (C# WPF)

Asked

Viewed 39 times

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

  • Application.Current.MainWindow.Hide(); for Window1 window1 = new Window1 Application.Current.Window1.Hide();

  • 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...

  • using Model.Libraries.KeyBoardHooking;

public MainWindow() {
 DataContext = this;
 InitializeComponent();
 new KeyBoardHooking();
} In this case it already enters the loop While(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

  • 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

1 answer

0

Come on William,

What happens is that you are misinterpreting Static class Application and consequently property Current.

Application.Current.Window1.Hide();

This usage is wrong. There is no property Window1 in Application.Current.

To summarize, when creating an instance, you should use the created instance. Example:

Window novaJanela = new Window(); //instancia criada.
novaJanela.Hide(); //executa o método Hide

SOLUTION

Method KeyBoardHooking corrected

public KeyBoardHooking()
        {
            StatusViews Window1 = new StatusViews();

            Thread Thread = new Thread(() =>
            {
                while (true)
                {
                    // tecla abrir/fechar (PgUp)
                    if (GetAsyncKeyState(33) == -32767)
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                        {
                            if (Window1.Topmost == true)
                            {
                                Window1.Hide();
                                Window1.Topmost = false;
                            }
                            else
                            {
                                Window1.Show();
                                Window1.Topmost = true;
                            }
                        }));
                    }
                    Thread.Sleep(1);
                }
            })
            {
                IsBackground = true
            };
            Thread.SetApartmentState(ApartmentState.STA);
            Thread.Start();
        }

I hope it helps. Hugs...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.