Esempio n. 1
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;
        }
Esempio n. 2
0
        private bool QuitIfLRClosed()
        {
            if (!this.IsLRWindowOpen())
            {
                KeyControl.TraceLine("Lightroom window was detected as closed. Shutting down");
                this.QuitApplication();

                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        public void MakeLRActive()
        {
            foreach (Window win in App.Current.Windows)
            {
                if (win.IsVisible)
                {
                    return;
                }
            }

            Process lr = this.GetLRRunningInstance();

            if (lr != null)
            {
                KeyControl.SetForegroundWindow(lr.MainWindowHandle);
            }
        }
Esempio n. 4
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;
     }
 }
Esempio n. 5
0
        public void StartUpEventLoop()
        {
            this.LoadDictionaryFromActiveKeysFile();

            this.hookProc = new LowLevelKeyboardProc(this.HookCallBack);

            using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    this.hookID = KeyControl.SetWindowsHookEx(
                        Constants.WH_KEYBOARD_LL,
                        this.hookProc,
                        KeyControl.GetModuleHandle(curModule.ModuleName),
                        0);
                }
            if (this.hookID == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Esempio n. 6
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));
            }
        }
Esempio n. 7
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();
        }
Esempio n. 8
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));
        }