Example #1
0
		/// <summary>
		/// Send the keys to the expected window or current window if null
		/// </summary>
		/// <param name="keys">stirng to send</param>
		public void SendKeys(WinAuthForm form, string keys, string code)
		{
			if (m_hWnd != IntPtr.Zero && m_hWnd != WinAPI.GetForegroundWindow())
			{
				WinAPI.SetForegroundWindow(m_hWnd);
				System.Threading.Thread.Sleep(50);
			}

			// replace any {CODE} items
			keys = Regex.Replace(keys, @"\{\s*CODE\s*\}", code, RegexOptions.Singleline);

			// clear events and stop input
			Application.DoEvents();
			bool blocked = WinAPI.BlockInput(true);
			try
			{
				// for now just split into parts and run each
				foreach (Match match in Regex.Matches(keys, @"\{.*?\}|[^\{]*", RegexOptions.Singleline))
				{
					// split into either {CMD d w} or just plain text
					if (match.Success == true)
					{
						string single = match.Value;
						if (single.Length == 0)
						{
							continue;
						}

						if (single[0] == '{')
						{
							// send command {COMMAND delay repeat}
							Match cmdMatch = Regex.Match(single.Trim(), @"\{([^\s]+)\s*(\d*)\s*(\d*)\}");
							if (cmdMatch.Success == true)
							{
								// extract the command and any optional delay and repeat
								string cmd = cmdMatch.Groups[1].Value.ToUpper();
								int delay = 0;
								if (cmdMatch.Groups[2].Success == true && cmdMatch.Groups[2].Value.Length != 0)
								{
									int.TryParse(cmdMatch.Groups[2].Value, out delay);
								}
								int repeat = 1;
								if (cmdMatch.Groups[3].Success == true && cmdMatch.Groups[3].Value.Length != 0)
								{
									int.TryParse(cmdMatch.Groups[3].Value, out repeat);
								}
								// run the command
								switch (cmd)
								{
									case "BS":
										SendKey('\x08', delay, repeat);
										break;
									case "TAB":
										SendKey('\t', delay, repeat);
										break;
									case "ENTER":
										SendKey('\n', delay, repeat);
										break;
									case "WAIT":
										for (; repeat > 0; repeat--)
										{
											System.Threading.Thread.Sleep(delay);
										}
										break;
									case "COPY":
										form.Invoke(new WinAuthForm.SetClipboardDataDelegate(form.SetClipboardData), new object[] { code });
										break;
									case "PASTE":
										string clipboard = form.Invoke(new WinAuthForm.GetClipboardDataDelegate(form.GetClipboardData), new object[] { typeof(string) }) as string;
										if (string.IsNullOrEmpty(clipboard) == false)
										{
											foreach (char key in clipboard)
											{
												SendKey(key);
											}
										}
										break;
									case "EXIT":
										Application.Exit();
										break;
									default:
										break;
								}
							}
						}
						else
						{
							SendKey(single);
						}
					}
				}
			}
			finally
			{
				// resume input
				if (blocked == true)
				{
					WinAPI.BlockInput(false);
				}
				Application.DoEvents();
			}
		}
Example #2
0
        /// <summary>
        ///     Send the keys to the expected window or current window if null
        /// </summary>
        /// <param name="keys">stirng to send</param>
        public void SendKeys(WinAuthForm form, string keys, string code)
        {
            if (m_hWnd != IntPtr.Zero && m_hWnd != WinAPI.GetForegroundWindow())
            {
                WinAPI.SetForegroundWindow(m_hWnd);
                Thread.Sleep(50);
            }

            // replace any {CODE} items
            keys = Regex.Replace(keys, @"\{\s*CODE\s*\}", code, RegexOptions.Singleline);

            // clear events and stop input
            Application.DoEvents();
            var blocked = WinAPI.BlockInput(true);

            try
            {
                // for now just split into parts and run each
                foreach (Match match in Regex.Matches(keys, @"\{.*?\}|[^\{]*", RegexOptions.Singleline))
                {
                    // split into either {CMD d w} or just plain text
                    if (match.Success)
                    {
                        var single = match.Value;
                        if (single.Length == 0)
                        {
                            continue;
                        }

                        if (single[0] == '{')
                        {
                            // send command {COMMAND delay repeat}
                            var cmdMatch = Regex.Match(single.Trim(), @"\{([^\s]+)\s*(\d*)\s*(\d*)\}");
                            if (cmdMatch.Success)
                            {
                                // extract the command and any optional delay and repeat
                                var cmd   = cmdMatch.Groups[1].Value.ToUpper();
                                var delay = 0;
                                if (cmdMatch.Groups[2].Success && cmdMatch.Groups[2].Value.Length != 0)
                                {
                                    int.TryParse(cmdMatch.Groups[2].Value, out delay);
                                }
                                var repeat = 1;
                                if (cmdMatch.Groups[3].Success && cmdMatch.Groups[3].Value.Length != 0)
                                {
                                    int.TryParse(cmdMatch.Groups[3].Value, out repeat);
                                }
                                // run the command
                                switch (cmd)
                                {
                                case "BS":
                                    SendKey('\x08', delay, repeat);
                                    break;

                                case "TAB":
                                    SendKey('\t', delay, repeat);
                                    break;

                                case "ENTER":
                                    SendKey('\n', delay, repeat);
                                    break;

                                case "WAIT":
                                    for (; repeat > 0; repeat--)
                                    {
                                        Thread.Sleep(delay);
                                    }
                                    break;

                                case "COPY":
                                    form.Invoke(new WinAuthForm.SetClipboardDataDelegate(form.SetClipboardData),
                                                code);
                                    break;

                                case "PASTE":
                                    var clipboard =
                                        form.Invoke(new WinAuthForm.GetClipboardDataDelegate(form.GetClipboardData),
                                                    typeof(string)) as string;
                                    if (string.IsNullOrEmpty(clipboard) == false)
                                    {
                                        foreach (var key in clipboard)
                                        {
                                            SendKey(key);
                                        }
                                    }
                                    break;

                                case "EXIT":
                                    Application.Exit();
                                    break;
                                }
                            }
                        }
                        else
                        {
                            SendKey(single);
                        }
                    }
                }
            }
            finally
            {
                // resume input
                if (blocked)
                {
                    WinAPI.BlockInput(false);
                }
                Application.DoEvents();
            }
        }