Gabor Meszaros
Gabor Meszaros

Reputation: 1325

How to keep the focus on a WPF desktop application?

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.

Upvotes: 11

Views: 5859

Answers (3)

Kylo Ren
Kylo Ren

Reputation: 8803

Use following window XAML:

<Window x:Class="WpfStackOverflowTempProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="450" Width="525"
    DataContext="{RelativeSource Mode=PreviousData}"
    xmlns:local="clr-namespace:WpfStackOverflowTempProject" 
    WindowState="Maximized"   ResizeMode="NoResize"  
    Topmost="True"        
    >

Topmost="True" should do the trick functional point of view. WindowState="Maximized" & ResizeMode="NoResize" to restrict interaction with other windows.

Upvotes: 0

Konrad Viltersten
Konrad Viltersten

Reputation: 39058

In my XAML markup I added the event listener for deactivation.

<Window x:Class="Desktop.MainWindow"
        Deactivated="MainWindow_OnDeactivated">
...
</Window>

In code behind, I added the event handler for reactivation.

namespace Desktop
{
  public partial class MainWindow : Window
  {
    public MainWindow() { InitializeComponent(); }

    private void MainWindow_OnDeactivated(object sender, EventArgs eventArgs)
    {
      Activate();
    }
  }
}

Upvotes: 0

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

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.

Upvotes: 5

Related Questions