Esempio n. 1
0
        //build keyboard command
        private static void BuildKeyboardCommand(Keys key)
        {
            if (!_performKeyboardCapture)
            {
                return;
            }

            bool toUpperCase = false;

            //determine if casing is needed
            if (IsKeyDown(Keys.ShiftKey) && IsKeyToggled(Keys.Capital))
            {
                toUpperCase = false;
            }
            else if (!IsKeyDown(Keys.ShiftKey) && IsKeyToggled(Keys.Capital))
            {
                toUpperCase = true;
            }
            else if (IsKeyDown(Keys.ShiftKey) && !IsKeyToggled(Keys.Capital))
            {
                toUpperCase = true;
            }
            else if (!IsKeyDown(Keys.ShiftKey) && !IsKeyToggled(Keys.Capital))
            {
                toUpperCase = false;
            }

            var buf           = new StringBuilder(256);
            var keyboardState = new byte[256];

            if (toUpperCase)
            {
                keyboardState[(int)Keys.ShiftKey] = 0xff;
            }

            ToUnicode((uint)key, 0, keyboardState, buf, 256, 0);
            var selectedKey = buf.ToString();

            //translate key press to sendkeys identifier
            if (key.ToString() == StopHookKey)
            {
                //STOP HOOK
                StopHook();
                return;
            }
            else
            {
                bool result = BuildSendAdvancedKeystrokesCommand(key, GeneratedCommands, "Current Window");
                if (result)
                {
                    return;
                }
            }

            //generate sendkeys together
            if ((GeneratedCommands.Count > 1) && (GeneratedCommands[GeneratedCommands.Count - 1] is SendKeystrokesCommand))
            {
                var lastCreatedSendKeysCommand = (SendKeystrokesCommand)GeneratedCommands[GeneratedCommands.Count - 1];

                //append chars to previously created command
                //this makes editing easier for the user because only 1 command is issued rather than multiples
                var previouslyInputChars = lastCreatedSendKeysCommand.v_TextToSend;
                lastCreatedSendKeysCommand.v_TextToSend = previouslyInputChars + selectedKey;
            }
            else
            {
                //build a pause command to track pause since last command
                BuildPauseCommand();

                //build keyboard command
                var keyboardCommand = new SendKeystrokesCommand
                {
                    v_TextToSend = selectedKey,
                    v_WindowName = "Current Window"
                };
                GeneratedCommands.Add(keyboardCommand);
            }
        }
Esempio n. 2
0
        //build keyboard command
        private static void BuildKeyboardCommand(Keys key)
        {
            bool toUpperCase = false;

            //determine if casing is needed
            if (IsKeyDown(Keys.ShiftKey) && IsKeyToggled(Keys.Capital))
            {
                toUpperCase = false;
            }
            else if (!IsKeyDown(Keys.ShiftKey) && IsKeyToggled(Keys.Capital))
            {
                toUpperCase = true;
            }
            else if (IsKeyDown(Keys.ShiftKey) && !IsKeyToggled(Keys.Capital))
            {
                toUpperCase = true;
            }
            else if (!IsKeyDown(Keys.ShiftKey) && !IsKeyToggled(Keys.Capital))
            {
                toUpperCase = false;
            }


            var buf           = new StringBuilder(256);
            var keyboardState = new byte[256];


            if (toUpperCase)
            {
                keyboardState[(int)Keys.ShiftKey] = 0xff;
            }

            ToUnicode((uint)key, 0, keyboardState, buf, 256, 0);

            var selectedKey = buf.ToString();


            if ((selectedKey == "") || (selectedKey == "\r"))
            {
                selectedKey = key.ToString();
            }


            //translate key press to sendkeys identifier
            if (selectedKey == StopHookKey)
            {
                //STOP HOOK
                StopHook();
                return;
            }
            else if (selectedKey == "Return")
            {
                selectedKey = "ENTER";
            }
            else if (selectedKey == "Space")
            {
                selectedKey = " ";
            }
            else if (selectedKey == "OemPeriod")
            {
                selectedKey = ".";
            }
            else if (selectedKey == "Oemcomma")
            {
                selectedKey = ",";
            }
            else if (selectedKey == "OemQuestion")
            {
                selectedKey = "?";
            }
            else if (selectedKey.Contains("ShiftKey"))
            {
                return;
            }

            if (!_performKeyboardCapture)
            {
                return;
            }

            //add braces
            if (selectedKey.Length > 1)
            {
                selectedKey = "{" + selectedKey + "}";
            }

            //generate sendkeys together
            if ((GeneratedCommands.Count > 1) && (GeneratedCommands[GeneratedCommands.Count - 1] is SendKeystrokesCommand))
            {
                var lastCreatedSendKeysCommand = (SendKeystrokesCommand)GeneratedCommands[GeneratedCommands.Count - 1];

                if (lastCreatedSendKeysCommand.v_TextToSend.Contains("{ENTER}"))
                {
                    //append this to a new command because you dont want text to input after user presses enter

                    //build a pause command to track pause since last command
                    BuildPauseCommand();

                    //build keyboard command
                    var keyboardCommand = new SendKeystrokesCommand
                    {
                        v_TextToSend = selectedKey,
                        v_WindowName = "Current Window"
                    };
                    GeneratedCommands.Add(keyboardCommand);
                }
                else
                {
                    //append chars to previously created command
                    //this makes editing easier for the user because only 1 command is issued rather than multiples
                    var previouslyInputChars = lastCreatedSendKeysCommand.v_TextToSend;
                    lastCreatedSendKeysCommand.v_TextToSend = previouslyInputChars + selectedKey;
                }
            }
            else
            {
                //build a pause command to track pause since last command
                BuildPauseCommand();

                //build keyboard command
                var keyboardCommand = new SendKeystrokesCommand
                {
                    v_TextToSend = selectedKey,
                    v_WindowName = "Current Window"
                };
                GeneratedCommands.Add(keyboardCommand);
            }
        }