Exemplo n.º 1
0
        private bool IsInTextBox()
        {
            bool   prevIsInTextBox = this.privateIsInTextBox;
            IntPtr focusedHandle   = KeyControl.GetFocusedHandle();

            if (focusedHandle != IntPtr.Zero)
            {
                StringBuilder ClassName = new StringBuilder(100);
                int           nRet      = GetClassName(focusedHandle, ClassName, ClassName.Capacity);

                if (nRet != 0)
                {
                    this.privateIsInTextBox = (string.Compare(ClassName.ToString(), "Edit", true) == 0);
                }
                else
                {
                    this.privateIsInTextBox = false;
                }
            }

            if (!prevIsInTextBox && this.privateIsInTextBox)
            {
                KeyControl.TraceLine("Keys inactive: Textbox was entered.");
            }
            else if (prevIsInTextBox && !this.privateIsInTextBox)
            {
                KeyControl.TraceLine("Keys active: Textbox was exited.");
            }

            return(this.privateIsInTextBox);
        }
Exemplo n.º 2
0
        private void SendUpdate(Dictionary <string, string> update)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Constants.SERVER_UPDATE_ENDPOINT);

            string json = this.jsSerializer.Serialize(update);

            byte[] bytes = Encoding.UTF8.GetBytes(json);

            req.ContentLength = bytes.Length;
            req.ContentType   = "application/json";

            req.Method = "POST";

            try
            {
                Stream dataStream = req.GetRequestStream();
                dataStream.Write(bytes, 0, bytes.Length);

                dataStream.Close();

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();

                KeyControl.TraceLine(string.Format("SendUpdate success: {0}", update.Aggregate(string.Empty, (output, kv) => { return(string.Format("{0}{1} = {2}{3}", output, kv.Key.ToString(), kv.Value.ToString(), Environment.NewLine)); }, (output) => { return(string.Format("{{ {0}{1} }}", Environment.NewLine, output)); })));
                this.isServerAvailable = true;

                response.Close();
            }
            catch (WebException e)
            {
                KeyControl.TraceLine(string.Format("Server connect failed: {0}", e.Message));
                this.isServerAvailable = false;
            }
        }
Exemplo n.º 3
0
        private void SendKey(int key, bool isDown)
        {
            var input = new INPUT();

            input.Type                    = (UInt32)InputType.KEYBOARD;
            input.Data.Keyboard           = new KEYBDINPUT();
            input.Data.Keyboard.Vk        = (UInt16)key;
            input.Data.Keyboard.Scan      = 0;
            input.Data.Keyboard.Flags     = (isDown) ? 0 : (UInt32)KeyboardFlag.KEYUP;
            input.Data.Keyboard.Time      = 0;
            input.Data.Keyboard.ExtraInfo = IntPtr.Zero;

            INPUT[] inputList = new INPUT[1];
            inputList[0] = input;

            SentKey sent = new SentKey(key, isDown);

            this.sentKeys.Add(sent);

            uint numberOfSuccessfulSimulatedInputs = SendInput(1, inputList, Marshal.SizeOf(typeof(INPUT)));

            if (numberOfSuccessfulSimulatedInputs == 0)
            {
                KeyControl.TraceLine(string.Format("Failed to send key {0}", key));
                this.sentKeys.Remove(sent);
            }
        }
Exemplo n.º 4
0
        public void PrintKeyDebug(Int32 key, Modifiers mod, bool isKeyDown)
        {
            KeyCommand command    = new KeyCommand();
            string     winKeyStr  = "0x" + key.ToString("x2");
            string     commandStr = winKeyStr;

            if (this.winKeyMapping.ContainsValue(winKeyStr))
            {
                string macKeyStr = this.winKeyMapping.FirstOrDefault(x => x.Value == winKeyStr).Key;
                KeyControl.TraceLine(string.Format(winKeyStr + " : " + macKeyStr));

                command.key = int.Parse(macKeyStr);
                command.mod = mod;

                commandStr = this.GetCommandStringForCommand(command);
            }

            if (isKeyDown)
            {
                KeyControl.TraceLine(string.Format("Key Down: {0}", commandStr));
            }
            else
            {
                KeyControl.TraceLine(string.Format("Key Up: {0}", commandStr));
            }
        }
Exemplo n.º 5
0
        private void ToggleKeyControlActive()
        {
            this.isKeyControlActive = !this.isKeyControlActive;
            KeyControl.TraceLine(string.Format("KeyControl Active: {0}", this.isKeyControlActive));

            this.CheckServerAvailability();
        }
Exemplo n.º 6
0
        private void StartTiming()
        {
            if (this.startTime >= 0)
            {
                KeyControl.TraceLine(string.Format("Last hook execution was cut off. StartTime: {0}", this.startTime));
            }

            this.startTime = this.stopWatch.ElapsedMilliseconds;
        }
Exemplo n.º 7
0
        private bool QuitIfLRClosed()
        {
            if (!this.IsLRWindowOpen())
            {
                KeyControl.TraceLine("Lightroom window was detected as closed. Shutting down");
                this.QuitApplication();

                return(true);
            }

            return(false);
        }
Exemplo n.º 8
0
 private void ReportHookFailure()
 {
     if (this.startTime >= 0)
     {
         if (this.isPossiblyDead)
         {
             KeyControl.TraceLine(string.Format("Last hook execution was cut off. StartTime: {0}", this.startTime));
             this.startTime = -1;
         }
         else
         {
             this.isPossiblyDead = true;
         }
     }
     else
     {
         this.isPossiblyDead = false;
     }
 }
Exemplo n.º 9
0
        private void EndTiming(string position)
        {
            long total = this.stopWatch.ElapsedMilliseconds - this.startTime;

            this.startTime = -1;

            if (total > this.maxTime)
            {
                this.maxTime = total;

                if (!Constants.HOOK_PERF_DETAIL)
                {
                    KeyControl.TraceLine(string.Format("Max Hooktime: {0}ms", this.maxTime));
                }
            }

            if (Constants.HOOK_PERF_DETAIL)
            {
                KeyControl.TraceLine(string.Format("Hooktime ({2}): {0}ms / {1}ms", total, this.maxTime, position));
            }
        }
Exemplo n.º 10
0
        private void LoadAllKeyfiles()
        {
            this.keyfileList = new Dictionary <string, Keyfile>();

            foreach (string filepath in System.IO.Directory.GetFiles(this.keyfileDirectory, "*.*", SearchOption.AllDirectories).Where(s => s.ToLower().EndsWith(Constants.KEYFILE_VKEYS_EXTENSION) || s.ToLower().EndsWith(Constants.KEYFILE_JSON_EXTENSION)))
            {
                string contents = this.DecryptFile(filepath);

                Keyfile keyfile = this.GetLatestVersion(contents);

                if (keyfile == null || keyfile.lrVersion != this.lrVersion)
                {
                    continue;
                }

                keyfile.filename = Path.GetFileName(filepath);

                if (Properties.Settings.Default.KeyfileSettings.ContainsKey(keyfile.uuid))
                {
                    keyfile.isActive = Properties.Settings.Default.KeyfileSettings[keyfile.uuid].isActive;
                }
                else
                {
                    keyfile.isActive = true;
                    Properties.Settings.Default.KeyfileSettings[keyfile.uuid]          = new KeyfileSettings();
                    Properties.Settings.Default.KeyfileSettings[keyfile.uuid].isActive = true;
                }

                if (!this.keyfileList.ContainsKey(keyfile.uuid))
                {
                    this.keyfileList.Add(keyfile.uuid, keyfile);
                }
                else
                {
                    KeyControl.TraceLine(string.Format("Keyfile id {0} already exists. Skipping file load.", keyfile.uuid));
                }
            }

            this.InitActiveKeyfile();
        }
Exemplo n.º 11
0
        private IntPtr HookCallBack(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            this.StartTiming();

            int  keyState  = (int)wParam;
            bool isKeyUp   = keyState == Constants.WM_KEYUP || (keyState == Constants.WM_SYSKEYUP);
            bool isKeyDown = keyState == Constants.WM_KEYDOWN || (keyState == Constants.WM_SYSKEYDOWN);

            // MSDN documentation indicates that nCodes less than 0 should always invoke CallNextHookEx.
            //  skip if the message is not a keyup or down
            if (nCode < 0 || !(isKeyUp || isKeyDown))
            {
                this.EndTiming("code 0");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            Int32     key       = lParam.vkCode;
            Modifiers modifiers = this.GetModifiers();

            // capture windows key
            if (key == Constants.VK_LWIN)
            {
                this.isLWinKeyDown = isKeyDown;
            }

            if (key == Constants.VK_RWIN)
            {
                this.isRWinKeyDown = isKeyDown;
            }

            // disable if we are active
            if (key == Constants.VK_LWIN || key == Constants.VK_RWIN)
            {
                if (this.CheckIsKeySystemEngaged())
                {
                    this.EndTiming("WIN key");
                    return(new IntPtr(1));
                }
            }

            // pass through keys that were remapped by the application
            if (this.IsSentKey(key, isKeyDown))
            {
                this.EndTiming("Sent key");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            // capture and pass through straight modifier keys
            if (this.GetIsModifierKey(key))
            {
                this.EndTiming("Straight mod");
                return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
            }

            //this.PrintKeyDebug(key, modifiers, isKeyDown);

            // mode switch for app
            if (this.authSuccess && modifiers == Modifiers.None && key == this.controlToggleKey && this.CheckIsLRInForeground())
            {
                if (isKeyDown)
                {
                    this.ToggleKeyControlActive();
                }

                this.EndTiming("Mode switch");
                return(new IntPtr(1));
            }

            // show/hide quick keylist ( CONTROL + / )
            if (this.isKeyControlActive && (modifiers & Modifiers.Control) == Modifiers.Control && key == 0xbf)
            {
                if (isKeyUp)
                {
                    if (this.CheckIsKeySystemEngaged())
                    {
                        this.ShowQuickList();
                    }
                    else
                    {
                        this.DismissQuickList();
                    }
                }

                this.EndTiming("Quick Ref");
                return(new IntPtr(1));
            }

            if (this.CheckIsKeySystemEngaged())
            {
                if (this.keysDict != null)
                {
                    foreach (KeyCommand command in this.keysDict)
                    {
                        if (key == command.key && modifiers == command.mod)
                        {
                            if (command.adj.ContainsKey(Constants.KEYFILE_ADJUSTMENT_REMAP_NODENAME))
                            {
                                KeyCommand newKey = command.adj[Constants.KEYFILE_ADJUSTMENT_REMAP_NODENAME] as KeyCommand;

                                if (newKey == null)
                                {
                                    KeyControl.TraceLine(string.Format("Failed to map {0} {2} to {1}", key, this.TranslateKey(newKey.key), (isKeyDown) ? "Down" : "Up"));
                                    this.EndTiming("Remap failed");
                                    return(new IntPtr(1));
                                }

                                KeyControl.TraceLine(string.Format("Mapping {0} {2} to {1}", key, this.GetCommandStringForCommand(newKey), (isKeyDown) ? "Down" : "Up"));

                                this.EndTiming("Remap");

                                this.SendModifiers(modifiers, false);

                                this.SendModifiers(newKey.mod, true);

                                this.SendKey(this.TranslateKey(newKey.key), isKeyDown);

                                this.SendModifiers(newKey.mod, false);

                                this.SendModifiers(modifiers, true);

                                return(new IntPtr(1));
                            }

                            if (isKeyDown)
                            {
                                if (command.adj != null && command.adj.Count > 0)
                                {
                                    Dictionary <string, string> adjustments = command.adj.ToDictionary((o) => o.Key, (o) => (string)o.Value);
                                    KeyControl.TraceLine("Key Command Executed");
                                    this.statusBar.Dispatcher.BeginInvoke(this.sendUpdateDelegate, adjustments);
                                }
                            }

                            this.EndTiming("Key command");
                            return(new IntPtr(1));
                        }
                    }
                }
            }

            this.EndTiming("Pass through");
            return(KeyControl.CallNextHookEx(this.hookID, nCode, wParam, ref lParam));
        }