Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture key press in whole application

Tags:

c#

wpf

keypress

Is it possible, to capture (somewhere in app.xaml.cs i guess) any key and if it pressed open window?

Thanks for help!

like image 715
user13657 Avatar asked Dec 19 '12 17:12

user13657


People also ask

What is a keypress event?

The KeyPress event occurs when the user presses and releases a key or key combination that corresponds to an ANSI code while a form or control has the focus. This event also occurs if you send an ANSI keystroke to a form or control by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

What is keypress in Salesforce?

The KeyPressevent occurs when the user presses and releases a key or key combination that corresponds to an ANSI code while a form or control has the focus. This event also occurs if you send an ANSI keystroke to a form or control by using the SendKeys action in a macro or the SendKeysstatement in Visual Basic. Syntax expression. KeyPress(KeyAscii)

Is there a way to detect multiple key pressed events?

I may do a follow up blog post with a clean and easy way to detect multiple simultaneous key pressed events such as “Ctrl+Alt+Del” or “Ctrl+F” if there is any interest.

Is it possible to capture key strokes in a WPF application?

If your application is small and the depth is nothing more than a Window with buttons, this is certainly attainable and would follow the standard approach to capturing key strokes within a WPF application. If your application is large you can attempt a global hook as detailed here but understand that the aforementioned caveats can still exist.


3 Answers

There is a better way. Found this on a MS forum. Works like a charm.

Put this code in Application startup:

EventManager.RegisterClassHandler(typeof(Window),
     Keyboard.KeyUpEvent,new KeyEventHandler(keyUp), true);

private void keyUp(object sender, KeyEventArgs e)
{
      //Your code...
}
like image 166
Adam Calvet Bohl Avatar answered Oct 10 '22 13:10

Adam Calvet Bohl


You could use something like this gist to register a global hook. It will fire whenever the given keys are pressed while your application is running. You can use it in your App class like this:

public partial class App
{
    private HotKey _hotKey;

    protected override void OnActivated(EventArgs e)
    {
        base.OnActivated(e);
        RegisterHotKeys();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        base.OnExit(e);
        UnregisterHotKeys();
    }

    private void RegisterHotKeys()
    {
        if (_hotKey != null) return;

        _hotKey = new HotKey(ModifierKeys.Control | ModifierKeys.Shift, Key.V, Current.MainWindow);
        _hotKey.HotKeyPressed += OnHotKeyPressed;
    }

    private void UnregisterHotKeys()
    {
        if (_hotKey == null) return;

        _hotKey.HotKeyPressed -= OnHotKeyPressed;
        _hotKey.Dispose();
    }

    private void OnHotKeyPressed(HotKey hotKey)
    {
        // Do whatever you want to do here
    }
}
like image 30
khellang Avatar answered Oct 10 '22 14:10

khellang


Yes and no.

Focus plays a role in the order for which a given key is handled. The control which captures the initial key press can opt to not pass the key along, which would prohibit you from capturing it at the top most level. In addition there are controls within the .NET framework that swallow certain keys under certain scenarios, however I am unable to recall a specific instance.

If your application is small and the depth is nothing more than a Window with buttons, this is certainly attainable and would follow the standard approach to capturing key strokes within a WPF application.

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
          myVariable = true;
    if (ctrl && e.Key == Key.S)
          base.OnKeyDown(e);
}

protected override void OnKeyUp(KeyEventArgs e)
{
    if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
          myVariable = false;

    base.OnKeyUp(e);
}

If your application is large you can attempt a global hook as detailed here but understand that the aforementioned caveats can still exist.

like image 38
Aaron McIver Avatar answered Oct 10 '22 14:10

Aaron McIver