0

I'm trying to register some keys as Hot Keys that operate not just within global application scope, but are captured even when the application doesn't have focus. I'm very well aware of the fact that doing this is not a good idea, and have sufficient reason to be doing it anyway.

The application is written using MVVM and the MainWindow doesn't even have a code-behind file. If I did use a code-behind for this, I would need to tie into commands in my MainViewModel class file which serves as the DataContext for my MainWindow.

I've setup a test application using code-behind with this class written by Eric Ouellet and it works great. However, when I register a HotKey inside my MainViewModel, the Action - OnHotKeyHandler - never fires.

My stripped down code looks like this. It's VB.NET and the class I linked to above is C#, so I compiled it as a C# class library and set it in my references. I'm getting no error messages at any time and in the real code I'm not swallowing exceptions. My MainWindow is really bound to this class and debugs show that it's been instantiated.

Public Class MainViewModel
    Private Shared _hotKeyF4 As GlobalHotKey.HotKey

    Public Sub New()
        _hotKeyF4 = New GlobalHotKey.HotKey(Key.F4, GlobalHotKey.KeyModifier.None, OnHotKeyHandler, True)
    End Sub

    Private Function OnHotKeyHandler() As Action(Of GlobalHotKey.HotKey)
        Debug.Print("F4 was pressed") 'Never fires
    End Function
End Class

If I can't make this particular class/method work, that's OK. All I need is some way to listen Globally (OS level) to keypresses inside a WPF MVVM application. C# code is fine as I will either convert it or compile it, whichever makes the most sense.

Note: Looks like I found a solution which was just recently written but I'm having some problems with it throwing unhandled exceptions if the HotKey I've chosen has already been assigned to something else.

Community
  • 1
  • 1
HK1
  • 11,941
  • 14
  • 64
  • 99
  • Just relating to your note, what do you expect to happen if the hotkey is already registered? can't you just choose a different hotkey if the hotkey is already registered or do you still want to capture them? – Viv Feb 26 '14 at 14:10
  • @Viv It shouldn't crash the program like it does now. As a developer I can go into the code and register a different key but as an end user you'd be screwed. – HK1 Feb 26 '14 at 16:03
  • Sure. Registering the hotkey is a function from native "user32.dll" you can check it [Here](https://github.com/thomaslevesque/NHotkey/blob/master/NHotkey/NativeMethods.cs#l8-l10) and it's usage in the project [Here](https://github.com/thomaslevesque/NHotkey/blob/master/NHotkey/Hotkey.cs#L47-l52) . So you got a few options. Clone the repository and handle the error differently in the Register function, or create your keybinding in your code from the view's code-behind surrounding it with a try catch or implement the keybinding in a `Behavior` and handle the exception with a try catch there. – Viv Feb 26 '14 at 16:13

0 Answers0