Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the focus on a WPF desktop application?

Tags:

c#

.net

wpf

I would like to create a windows desktop application that can be used by my daughter. The use case is that I give her a keyboard and I start this application so she can press any keys that she wants. The application can display something (e.g. the pressed letters in big, or pictures, etc.) and prevents my daughter to delete/modify things from/on my computer.

I have some difficulties with the special key handling. I can disable the ALT+F4 with the following technique but I cannot control the ALT+TAB and Win keys that way.

public MainWindow()
{
    InitializeComponent();
    this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
}

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
    if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
    {
        e.Handled = true;
    }
    else
    {
        base.OnPreviewKeyDown(e);
    }
}

I found an article which disables such functionalities when the application is active but I think it quite fragile for non-expected application exit.

What I finally did is a workaround which works but does not feel too professional.

public MainWindow()
{
    InitializeComponent();
    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += OnTimerTick;
    dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
    dispatcherTimer.Start();
}

private void OnTimerTick(object sender, EventArgs e)
{
    this.Activate();
    this.Focus();
}

As you can see I created a timer which brings the focus back from time to time. This way if you press the Win Key or the ALT+TAB it will brings the application back.

How can I keep the focus on my application on a more professional way? I am thinking about a solution which detects when the application loses the focus (for any reasons) and sets the focus back.

like image 279
Gabor Meszaros Avatar asked Feb 20 '16 13:02

Gabor Meszaros


People also ask

How do you focus a control in WPF?

When setting initial focus at application startup, the element to receive focus must be in the visual tree of the initial window loaded by the application, and the element must have Focusable and IsVisible set to true . The recommended place to set initial focus is in the Loaded event handler.

Is WPF still in demand?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).

How do I center a WPF window?

Centring Windows To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed. Run the program and click the button to see the result.

How do I keep windows on top of WPF?

Basically, to keep it on top you just set the lose focus event to make it go back to top.


1 Answers

Use the Window.Deactivated event and re-activate it to achieve what you want:

this.Deactivated += (s, e) => this.Activate();

What you would also like would be to make the app full-screen and within the app create a hidden shortcut key. After you press that, you are prompted to type a password and then you can close the app. Otherwise, it will be a hassle to close the app for you as well.

like image 61
Fᴀʀʜᴀɴ Aɴᴀᴍ Avatar answered Sep 19 '22 15:09

Fᴀʀʜᴀɴ Aɴᴀᴍ