Ejemplo n.º 1
0
 private void prov_KeyUp(object sender, KeyHookEventArgs e)
 {
 }
Ejemplo n.º 2
0
        private int MainHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if ((nCode >= HC_ACTION) && ((wParam == WM_KEYDOWN) || (wParam == WM_SYSKEYDOWN)))
            {
                // Cast : structure pour obtenir les données du hook clavier.
                KBDLLHOOKSTRUCT hookStruct;
                hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                // On récupère le code de touche.
                Keys keyCode = (Keys)hookStruct.vkCode;

                // On récupère l'état du clavier. Nécessaire pour ensuite obtenir le caractère ASCII.
                byte[] keyStates = new byte[255];
                GetKeyboardState(keyStates);
                StringBuilder sbChar = new StringBuilder();
                ToAscii((uint)hookStruct.vkCode, (uint)hookStruct.scanCode, keyStates, sbChar, (uint)hookStruct.flags);
                char keyChar;
                if (sbChar.Length > 0)
                {
                    keyChar = sbChar[0];
                }
                else
                {
                    keyChar = ' ';
                }

                // On récupère l'état de chaque touche spéciale (Control, Alt, Shift et Windows)
                if (hookStruct.vkCode == 0x5B)
                {
                    leftWin = true;
                }
                if (hookStruct.vkCode == 0x5C)
                {
                    rightWin = true;
                }
                if (hookStruct.vkCode == 0xA0)
                {
                    leftShift = true;
                }
                if (hookStruct.vkCode == 0xA1)
                {
                    rightShift = true;
                }
                if (hookStruct.vkCode == 0xA2)
                {
                    leftCtrl = true;
                }
                if (hookStruct.vkCode == 0xA3)
                {
                    rightCtrl = true;
                }
                if (hookStruct.vkCode == 0xA4)
                {
                    alt = true;
                }
                if (hookStruct.vkCode == 0xA5)
                {
                    altCar = true;
                }
                if (hookStruct.vkCode == 0x1B)
                {
                    escape = true;
                }
                if (hookStruct.vkCode == 0x5D)
                {
                    apps = true;
                }
                if (hookStruct.vkCode == 0x09)
                {
                    tab = true;
                }

                // On crée l'évènement.
                KeyHookEventArgs args = new KeyHookEventArgs(keyCode, keyChar, alt, altCar, leftCtrl, rightCtrl, leftShift, rightShift, leftWin, rightWin, escape, apps, tab);
                EventHandler <KeyHookEventArgs> keyDown = KeyDown;

                // Vérifier si il y a au moins un délégué qui écoute l'évènement.
                // Sinon, ça déclenche une erreur si on lance l'évènement dans le vide.
                if (keyDown != null)
                {
                    keyDown(this, args);    // Lancer l'évènement.
                }

                bool a = alt || altCar;
                bool c = rightCtrl || leftCtrl;
                if ((a) && (c) && (hookStruct.vkCode == 0x2E))              //Cas spécial ALT+CTRL+SUPP
                {
                    Program.LogFich.Error("[KBHookProvider] MainHookProc() - SAISIE CTRL + ALT + SUPPR");
                    Uri url = new Uri(SandBox.Properties.Settings.Default.urlApplication.ToString() + "/admin/ctrl_alt_suppr.asp");

                    StringBuilder sb = new StringBuilder();
                    sb.Append("machine=").Append(HttpUtility.UrlEncode(Functions.getHost(), Encoding.ASCII));
                    sb.Append("&typeError=").Append(HttpUtility.UrlEncode("-15", Encoding.ASCII));
                    byte[] postBytes = Encoding.ASCII.GetBytes(sb.ToString());
                    webBrowser.Navigate(url, "", postBytes, "Content-Type: application/x-www-form-urlencoded");
                    MessageBox.Show("Saisie du ctrl+alt+suppr détectée");
                    webBrowser.GoBack();
                    return(block);
                }
                else if (alt && tab)                                             //ALT + TAB
                {
                    return(CallNextHookEx(hHook, nCode, wParam, lParam));
                }
                else if ((leftShift && (hookStruct.vkCode == 0x79)) || apps)     //Shift + F10 ou Apps
                {
                    return(block);
                }
                else if (leftWin || rightWin || escape || alt)
                {
                    return(block);
                }
                else
                {
                    return(CallNextHookEx(hHook, nCode, wParam, lParam));
                }
            }
            if ((nCode >= HC_ACTION) && ((wParam == WM_KEYUP) || (wParam == WM_SYSKEYUP)))
            {
                // Cast : structure pour obtenir les données du hook clavier.
                KBDLLHOOKSTRUCT hookStruct;
                hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

                // On récupère le code de touche.
                byte keyCode = (byte)hookStruct.vkCode;

                // obtenir le caractère ASCII.
                byte[] keyStates = new byte[255];
                GetKeyboardState(keyStates);
                StringBuilder sbChar = new StringBuilder();
                ToAscii((uint)hookStruct.vkCode, (uint)hookStruct.scanCode, keyStates, sbChar, (uint)hookStruct.flags);
                char keyChar;
                if (sbChar.Length > 0)
                {
                    keyChar = sbChar[0];
                }
                else
                {
                    keyChar = ' ';
                }

                if (keyCode == 0x5B)
                {
                    leftWin = false;
                }
                if (keyCode == 0x5C)
                {
                    rightWin = false;
                }
                if (keyCode == 0xA0)
                {
                    leftShift = false;
                }
                if (keyCode == 0xA1)
                {
                    rightShift = false;
                }
                if (keyCode == 0xA2)
                {
                    leftCtrl = false;
                }
                if (keyCode == 0xA3)
                {
                    rightCtrl = false;
                }
                if (keyCode == 0xA4)
                {
                    alt = false;
                }
                if (keyCode == 0xA5)
                {
                    altCar = false;
                }
                if (keyCode == 0x1B)
                {
                    escape = false;
                }
                if (keyCode == 0x5D)
                {
                    apps = false;
                }
                if (keyCode == 0x09)
                {
                    tab = false;
                }

                KeyHookEventArgs args = new KeyHookEventArgs((Keys)keyCode, keyChar, alt, altCar, leftCtrl, rightCtrl, leftShift, rightShift, leftWin, rightWin, escape, apps, tab);
                EventHandler <KeyHookEventArgs> keyup = KeyUp;
                if (keyup != null)
                {
                    keyup(this, args);
                }

                bool a = alt || altCar;
                bool c = rightCtrl || leftCtrl;
                if ((a) && (c) && (hookStruct.vkCode == 0x2E))              //Cas spécial ALT+CTRL+SUPP
                {
                    return(block);
                }
                if (alt && tab)                                             //ALT + TAB
                {
                    return(CallNextHookEx(hHook, nCode, wParam, lParam));
                }
                if ((leftShift && (hookStruct.vkCode == 0x79)) || apps)     //Shift + F10 ou Apps
                {
                    return(block);
                }
                if (leftWin || rightWin || escape || alt)
                {
                    return(block);
                }
                else
                {
                    return(CallNextHookEx(hHook, nCode, wParam, lParam));
                }
            }
            else
            {
                return(CallNextHookEx(hHook, nCode, wParam, lParam));
            }
        }