Esempio n. 1
0
        // gestion CTRL-TAB, CTRL-F4, ALT-F4
        private void KeyboardHookProc(KeyboardHookMessage msg)
        {
            if (msg.nCode != User.Const.HC_ACTION) return;

            // gestion CTRL-TAB
            bool bActivateWindow = false;
            if (msg.KeyFlags.TransitionState == 0 && msg.vk == User.VK.VK_TAB && msg.KeyState.Ctrl && !msg.KeyState.Alt)
            {
                WindowsApp.CurrentWindowsApp.DisableSetFirstWindow = true;
                if (msg.KeyState.Shift)
                {
                    if (gbTrace)
                        pb.Trace.WriteLine("KeyboardHook                      : Active previous window");
                    WindowsApp.CurrentWindowsApp.ActivatePreviousWindow();
                }
                else
                {
                    if (gbTrace)
                        pb.Trace.WriteLine("KeyboardHook                      : Active next window");
                    WindowsApp.CurrentWindowsApp.ActivateNextWindow();
                }
                gbWindowActivatedByKeyOngoing = true;
                bActivateWindow = true;
                msg.RemoveMessage = true;
            }
            if (!bActivateWindow && gbWindowActivatedByKeyOngoing && msg.vk != User.VK.VK_TAB)
            {
                gbWindowActivatedByKeyOngoing = false;
                WindowsApp.CurrentWindowsApp.DisableSetFirstWindow = false;
                WindowsApp.CurrentWindowsApp.SetFirstWindow();
            }

            // gestion CTRL-F4
            if (msg.KeyFlags.TransitionState == 0 && msg.vk == User.VK.VK_F4)
            {
                if (msg.KeyState.Ctrl && !msg.KeyState.Alt && !msg.KeyState.Shift)
                {
                    WindowData window = FindWindow(msg.GUIThreadInfo.hwndActive);
                    string sWindow = "not found";
                    if (window != null) sWindow = window.Form.Text;
                    string sAction;
                    if (window != null && window.EnableCtrlF4Close) sAction = "close window"; else sAction = "don't close window";
                    if (gbTrace)
                        pb.Trace.WriteLine("KeyboardHook                      : form-{0,-25} CTRL-F4 hwnd active {1:x} focus {2:x} {3}", sWindow, (int)msg.GUIThreadInfo.hwndActive, (int)msg.GUIThreadInfo.hwndFocus, sAction);
                    if (window != null && window.EnableCtrlF4Close) window.Form.Close();
                    msg.RemoveMessage = true;
                }
            }

            // gestion ALT-F4
            if (msg.KeyState.Alt && !msg.KeyState.Ctrl && msg.vk == User.VK.VK_F4)
            {
                WindowData window = FindWindow(msg.GUIThreadInfo.hwndActive);
                string sWindow = "not found";
                if (window != null) sWindow = window.Form.Text;
                string sAction;
                if (window != null && window.DisableAltF4) sAction = "remove message ALT-F4"; else sAction = "don't remove message ALT-F4";
                if (window != null && window.CloseApplicationOnAltF4) sAction += " and close application"; else sAction = " and don't close application";
                if (gbTrace)
                    pb.Trace.WriteLine("KeyboardHook                      : form-{0,-25} ATL-F4 hwnd active {1:x} focus {2:x} {3}", sWindow, (int)msg.GUIThreadInfo.hwndActive, (int)msg.GUIThreadInfo.hwndFocus, sAction);
                if (window != null)
                {
                    if (window.DisableAltF4) msg.RemoveMessage = true;
                    if (window.CloseApplicationOnAltF4) ApplicationExit();
                }
            }
        }
Esempio n. 2
0
        private int _HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && (HookEvent != null || gbTrace))
            {
                User.VK       vk = Messages.GetVirtualKey((int)wParam);
                KeyboardState ks = Messages.GetKeyboardState();
                // attention (uint)lParam peut générer une exception si la valeur dépasse la capacité d'un int, utiliser (uint)p.ToInt64()
                //KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags((uint)lParam);
                KeystrokeMessageFlags     kf            = Messages.GetKeystrokeMessageFlags((uint)lParam.ToInt64());
                User.Struct.GUITHREADINFO GUIThreadInfo = Win32.Windows.GetGUIThreadInfo();

                if (gbTrace)
                {
                    string sCode;
                    if (nCode == User.Const.HC_ACTION)
                    {
                        sCode = "HC_ACTION";
                    }
                    else if (nCode == User.Const.HC_NOREMOVE)
                    {
                        sCode = "HC_NOREMOVE";
                    }
                    else
                    {
                        sCode = nCode.ToString();
                    }
                    string sKey = vk.ToString();
                    if (ks.Alt)
                    {
                        sKey = "ALT-" + sKey;
                    }
                    if (ks.Ctrl)
                    {
                        sKey = "CTRL-" + sKey;
                    }
                    if (ks.Shift)
                    {
                        sKey = "SHIFT-" + sKey;
                    }
                    string sTransition;
                    if (kf.TransitionState == 0)
                    {
                        sTransition = "WM_KEYDOWN";
                    }
                    else
                    {
                        sTransition = "WM_KEYUP";
                    }
                    string sPreviousKeyState;
                    if (kf.PreviousKeyState == 0)
                    {
                        sPreviousKeyState = "KEYUP";
                    }
                    else
                    {
                        sPreviousKeyState = "KEYDOWN";
                    }
                    pb.Trace.WriteLine("KeyboardHook                                   : {0,-20} {1,-20} {2:x,4} {3,-11} prev key state {4,-7} lParam {5:x}", sTransition, sKey, wParam, sCode, sPreviousKeyState, lParam);
                }

                if (HookEvent != null)
                {
                    KeyboardHookMessage msg = new KeyboardHookMessage()
                    {
                        nCode = nCode, wParam = wParam, lParam = lParam, vk = vk, KeyState = ks, KeyFlags = kf, GUIThreadInfo = GUIThreadInfo
                    };
                    HookEvent(msg);
                    if (msg.RemoveMessage)
                    {
                        return(1);
                    }
                }
            }
            return(User.CallNextHookEx(gHookHandle, nCode, wParam, lParam));
        }
Esempio n. 3
0
        private int _HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && (HookEvent != null || gbTrace))
            {
                User.VK vk = Messages.GetVirtualKey((int)wParam);
                KeyboardState ks = Messages.GetKeyboardState();
                // attention (uint)lParam peut générer une exception si la valeur dépasse la capacité d'un int, utiliser (uint)p.ToInt64()
                //KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags((uint)lParam);
                KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags((uint)lParam.ToInt64());
                User.Struct.GUITHREADINFO GUIThreadInfo = Win32.Windows.GetGUIThreadInfo();

                if (gbTrace)
                {
                    string sCode;
                    if (nCode == User.Const.HC_ACTION)
                        sCode = "HC_ACTION";
                    else if (nCode == User.Const.HC_NOREMOVE)
                        sCode = "HC_NOREMOVE";
                    else
                        sCode = nCode.ToString();
                    string sKey = vk.ToString();
                    if (ks.Alt) sKey = "ALT-" + sKey;
                    if (ks.Ctrl) sKey = "CTRL-" + sKey;
                    if (ks.Shift) sKey = "SHIFT-" + sKey;
                    string sTransition;
                    if (kf.TransitionState == 0) sTransition = "WM_KEYDOWN"; else sTransition = "WM_KEYUP";
                    string sPreviousKeyState;
                    if (kf.PreviousKeyState == 0) sPreviousKeyState = "KEYUP"; else sPreviousKeyState = "KEYDOWN";
                    pb.Trace.CurrentTrace.WriteLine("KeyboardHook                                   : {0,-20} {1,-20} {2:x,4} {3,-11} prev key state {4,-7} lParam {5:x}", sTransition, sKey, wParam, sCode, sPreviousKeyState, lParam);
                }

                if (HookEvent != null)
                {
                    KeyboardHookMessage msg = new KeyboardHookMessage() { nCode = nCode, wParam = wParam, lParam = lParam, vk = vk, KeyState = ks, KeyFlags = kf, GUIThreadInfo = GUIThreadInfo };
                    HookEvent(msg);
                    if (msg.RemoveMessage) return 1;
                }
            }
            return User.CallNextHookEx(gHookHandle, nCode, wParam, lParam);
        }