After registration WPF hotkey hotkey message processing (non-winform embodiment)

Since recently doing wpf version of the screenshot of the software, in dealing with global hotkeys, and found all winform keyboard handling form, this approach requires the use of dynamic libraries used by winform domestic blog, so let me start uncoordinated code find related code on github.

Eventually, I found, wpf itself supports keyboard message handling systems (including hot keys).

Using a keyboard message handling ComponentDispatcher

Paste the following code to facilitate copying and pasting:

static class HotkeyListener public 
    { 
        /// <Summary> 
        /// hotkey message 
        /// </ summary> 
        const int WindowsMessageHotkey = 786; 
        /// <Summary> 
        instance handle /// demo of 
        /// </ summary> 
        IExcuteHotKey Instance = null static public; 
        static HotkeyListener () 
        { 
            // hot key register (call windows API implementation consistent with WinForm) 
            hotkey hotkey = new new hotkey (Keys.F2, Modifiers.None, to true); 
            // process the hotkey message (using the keyboard process wpf) 
            ComponentDispatcher.ThreadPreprocessMessage + = (the MSG the Message REF, REF BOOL the Handled) => 
            { 
                // determines whether the message hotkey
                IF (message.message == WindowsMessageHotkey) 
                { 
                    // Get hotkey ID 
                    var ID = Message.wParam.ToInt32 (); 
                    // hotkey callback method (registered need to determine whether the hot key matching is performed) 
                    Instance.ExcuteHotKeyCommand (ID); 
                } 
            }; 
        } 
    }
 

The description of ComponentDispatcher

 

Guess you like

Origin www.cnblogs.com/zhuxiaoxiao/p/11420359.html