/// <summary>
        /// Registers a hot valueField in the system.
        /// </summary>
        /// <param name="modifier">The modifiers that are associated with the hot valueField.</param>
        /// <param name="valueField">The valueField itself that is associated with the hot valueField.</param>
        public KeyboardHook_item RegisterHotKey(ModifierKeys modifier, Keys key)
        {
            var id = _currentId++;

            // register the hot valueField.
            if (!RegisterHotKey(_window.Handle, id, (uint)modifier, (uint)key))
            {
                throw new InvalidOperationException("Couldn’t register the hot key.");
            }

            var h = new KeyboardHook_item(modifier, key, id, this);

            _hooks.Add(h);
            return(h);
        }
Example #2
0
        protected void onShortcutFired(object sender, KeyPressedEventArgs args)
        {
            if (this.CtrlVLock)
            {
                return;
            }

            KeyboardHook_item khi = (KeyboardHook_item)sender;

            TemplatesDataSet.TemplatesRow dr = (TemplatesDataSet.TemplatesRow)khi.CustomData;
            if (dr == null)
            {
                return;
            }


            string puvodniClipboard = Clipboard.GetText(); // Zaloha obsahu clipboard

            // Copy template to clipboard
            string template = dr.Template.Replace("\r\n", "\n"); // Mac, Unix newlines to Win newlines

            // Using application direct write
            // TODO: wait until KEYUP insted of 500ms pause
            Thread.Sleep(new TimeSpan(0, 0, 0, 0, 500));

            String selectedText = "";

            // Overring clipboard with current selection, if template wants to do that
            if (template.Contains("<copy!/>\n"))
            {
                try
                {
                    selectedText = getSelectedTextUsingAutomationElement();
                }
                catch (InvalidOperationException e)
                {
                    // SMALL C here!!! Otherwise this sends CTRL+SHIFT+C
                    SendKeys.SendWait("^(c)");  // Send CTRL+C

                    SendKeys.Flush();
                    //Thread.Sleep(new TimeSpan(0, 0, 0, 0, 50));
                    selectedText = Clipboard.GetText();
                }

                template = template.Replace("<copy!/>\n", "");
            }

            // Macros
            template = template.Replace("<clipboard/>", selectedText);

            this.CtrlVLock = true;
            Clipboard.SetText(template);
            SendKeys.SendWait("^(v)"); // intentionally small 'v'
            SendKeys.Flush();

            // wait before we restore clipboard this could fix problems with pasting original values
            Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100));

            try
            {
                if (puvodniClipboard != null)
                {
                    Clipboard.SetText(puvodniClipboard); // obnovime puvodni obsah clipboardu
                }
            }
            catch (System.Runtime.InteropServices.ExternalException e)
            { /*do nothing*/ }
            this.CtrlVLock = false;

            // escape string
            // http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx
            // template = template.Replace("{", "----{{----")
            //     .Replace("}", "----}}----")
            //     .Replace("----{{----", "{{}")
            //     .Replace("----}}----", "{}}")
            //     .Replace("+", "{+}")
            //     .Replace("^", "{^}")
            //     .Replace("%", "{%}")
            //     .Replace("~", "{~}")
            //     .Replace("(", "{(}")
            //     .Replace(")", "{)}")
            //     .Replace("[", "{[}")
            //     .Replace("]", "{]}")
            //
            //     .Replace("\n", "{ENTER}")
            //     .Replace("\r", "{ENTER}");

            // Send result to application
            // SendKeys.SendWait(template);
            // SendKeys.Flush();



            // Notify, that all has been done
            //this.notifyIcon.ShowBalloonTip(1, "ClipboardTemplates", "Template has been sent to application.", ToolTipIcon.Info);


            // Copy to clipboard
            //var originalClipboardContent = Clipboard.GetDataObject();
            //Clipboard.SetText(dr.Template);
            //
            //this.notifyIcon.ShowBalloonTip(1, "ClipboardTemplate", "Your template has been copied to clipboard", ToolTipIcon.Info);
            //
            ////Thread.Sleep(new TimeSpan(0,0,0,0,250));
            //
            ////SendKeys.SendWait("^V");
            ////SendKeys.SendWait("+{INSERT}");
            //
            //Clipboard.SetDataObject(originalClipboardContent);
        }
 /// <summary>
 /// Unregisters a hotkey
 /// </summary>
 /// <param name="h"></param>
 public void UnregisterHotKey(KeyboardHook_item h)
 {
     UnregisterHotKey(_window.Handle, h.Id);
     this._hooks.Remove(h);
 }