/// <exception cref="System.Runtime.InteropServices.ExternalException"></exception> /// <exception cref="System.Threading.ThreadStateException"></exception> private void KeyboardMouseEvents_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if ((usingControl && e.Control == false) || (!usingControl && e.Control == true)) { return; } if ((usingShift && e.Shift == false) || (!usingShift && e.Shift == true)) { return; } if ((usingAlt && e.Alt == false) || (!usingAlt && e.Alt == true)) { return; } if (((char)e.KeyValue) == cbKeys.SelectedItem.ToString()[0]) { if (Clipboard.ContainsText()) { string content = Clipboard.GetText(); content = selectedMockType.MockifyText(content); Clipboard.SetText(content); } } }
public void DumpWFClipboardTest() { TestContext.WriteLine($"ContainsAudio: {WFClipboard.ContainsAudio()}"); TestContext.WriteLine($"ContainsData: {WFClipboard.ContainsData(DataFormats.StringFormat)}"); TestContext.WriteLine($"ContainsFileDropList: {WFClipboard.ContainsFileDropList()}"); TestContext.WriteLine($"ContainsImage: {WFClipboard.ContainsImage()}"); TestContext.WriteLine($"ContainsText: {WFClipboard.ContainsText()}"); TestContext.WriteLine($"GetAudioStream: {WFClipboard.GetAudioStream()}"); TestContext.WriteLine($"GetData: {WFClipboard.GetData(DataFormats.StringFormat)}"); TestContext.WriteLine($"GetDataObject: {WFClipboard.GetDataObject()}"); TestContext.WriteLine($"GetFileDropList: {string.Join("\n", WFClipboard.GetFileDropList().Cast<string>())}"); TestContext.WriteLine($"GetImage: {WFClipboard.GetImage()}"); TestContext.WriteLine($"GetText: {WFClipboard.GetText()}"); }
public static bool ContainsText(TextDataFormat format) { return(ClipboardProxy.ContainsText(format)); }
public static bool ContainsText() { return(ClipboardProxy.ContainsText()); }
public void SaveClipboardToRecent() { Console.WriteLine("attempting to save clipboard to recent"); if (!selfCopy) { Console.WriteLine("checking for recent duplicate"); ClipboardObject duplicateObject = null; foreach (ClipboardObject clipboardObject in categories[0].ClipboardObjects) { if (clipboardObject.MatchesClipboard()) { duplicateObject = clipboardObject; break; } } if (duplicateObject != null) { Console.WriteLine("moving duplicate clipboard to most recent"); categories[0].RemoveClipboardObject(duplicateObject); if (CurrentCategoryId == categories[0].Id) { ContentPanel.Children.Remove(duplicateObject.ClipboardContainer); } duplicateObject.Name = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); categories[0].AddClipboardObject(duplicateObject); if (CurrentCategoryId == categories[0].Id) { ContentPanel.Children.Insert(1, duplicateObject.ClipboardContainer); } } else { ClipboardObject newClipboardObject = null; Console.WriteLine("not a duplicate, finding format to save as"); foreach (string format in Clipboard.GetDataObject().GetFormats()) { Console.WriteLine(format); } if (Clipboard.ContainsText()) { newClipboardObject = new TextClipboardObject(this, DateTime.Now.ToString(Recent.DATE_FORMAT), Clipboard.GetText()); } else if (Clipboard.ContainsImage()) { newClipboardObject = new ImageClipboardObject(this, DateTime.Now.ToString(Recent.DATE_FORMAT), Clipboard.GetDataObject()); } else if (Clipboard.ContainsFileDropList()) { newClipboardObject = new FileDropListClipboardObject(this, DateTime.Now.ToString(Recent.DATE_FORMAT), Clipboard.GetFileDropList()); } if (newClipboardObject != null) { categories[0].AddClipboardObject(newClipboardObject); if (CurrentCategoryId == categories[0].Id) { ContentPanel.Children.Insert(1, newClipboardObject.ClipboardContainer); } if (categories[0].ClipboardObjects.Count() > MAX_RECENTS) { categories[0].RemoveClipboardObject(categories[0].ClipboardObjects.Last()); if (CurrentCategoryId == categories[0].Id) { ContentPanel.Children.RemoveAt(categories[0].ClipboardObjects.Count() + 1); } } } } SetActiveCategory(Recent.ID); } else { Console.WriteLine("ignoring self copy"); selfCopy = false; } }
static void Main(string[] args) { PathType pathType = PathType.None; string parsedPath = null; var screenBounds = Screen.PrimaryScreen.Bounds; GImgClicks gimgClicks = null; foreach (var supported in GImgClicksSupported) { if (supported.ScreenResolution.Width == screenBounds.Width && supported.ScreenResolution.Height == screenBounds.Height) { gimgClicks = supported; break; } } if (gimgClicks == null) { MessageBox.Show("Error: Screen resolution not supported.", "GoSearch"); return; } Utility.RunAsSTAThread(() => { if (Clipboard.ContainsText()) { var s = Clipboard.GetText(); if (s.StartsWith("http://") || s.StartsWith("https://")) { pathType = PathType.WebUrl; return; } else if (File.Exists(s)) { pathType = PathType.LocalFile; return; } } // Note The WPF version of Clipboard.GetImage() is buggy if (Clipboard.ContainsImage()) { var image = Clipboard.GetImage(); parsedPath = Path.Combine(Path.GetTempPath(), "temp-gs.png"); pathType = PathType.LocalFile; image.Save(parsedPath, System.Drawing.Imaging.ImageFormat.Png); return; } if (Clipboard.ContainsFileDropList()) { var fs = Clipboard.GetFileDropList(); parsedPath = fs[0]; pathType = PathType.LocalFile; } }); if (pathType == PathType.None) { MessageBox.Show("Error: No valid path copied.", "GoSearch"); return; } var w = new ControlledWindow { ProcessPath = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", Arguments = @"-inprivate http://images.google.com" }; void SleepAndCheck(int sleepMs = 500) { Thread.Sleep(500); if (w.Process.HasExited) { throw new TargetWindowExited(); } } if (!w.CreateNew(x => x.StartsWith("Google Images"))) { MessageBox.Show("Error: Failed to create new browser window.", "GoSearch"); return; } try { if (!w.Process.HasExited && w.WindowRect.HasValue) { if (!w.WaitUntilTitleStartsWith("Google Images")) { MessageBox.Show("Error: Failed to load Google Images site.", "GoSearch"); return; } if (w.Process.HasExited) { return; } //var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; //const int StandardResolutionWidth = 1920; //const int StandardResolutionHeight = 1080; //const int StandardClickX = 1200; //const int StandardClickY = 550; //if (bounds.Width == StandardResolutionHeight && bounds.Height == StandardResolutionWidth) //{ // Input.MouseClick(1200, 550); //} //else //{ // var x = StandardClickX * bounds.Width / StandardResolutionWidth; // var y = StandardClickY * bounds.Height / StandardResolutionHeight; // Input.MouseClick(x, y); //} Input.MouseClick(gimgClicks.CameraButton.X, gimgClicks.CameraButton.Y); SleepAndCheck(); if (w.Process.HasExited) { return; } if (pathType == PathType.WebUrl) { Debug.Assert(parsedPath == null); CtrlV(); } else if (pathType == PathType.LocalFile) { Input.MouseClick(gimgClicks.UploadImageTab.X, gimgClicks.UploadImageTab.Y); SleepAndCheck(); Input.MouseClick(gimgClicks.ChooseFileButton.X, gimgClicks.ChooseFileButton.Y); SleepAndCheck(); if (parsedPath != null) { Input.TypeStr("\"" + parsedPath + "\"\n"); } else { CtrlV(); } } } } catch (TargetWindowExited) { MessageBox.Show("Error: Search window lost.", "GoSearch"); } }
void GetClipboardContents() { if (_image) { if (WinFormsClipboard.ContainsImage()) { _result = WinFormsClipboard.GetImage(); } return; } if (_audio) { if (WinFormsClipboard.ContainsAudio()) { _result = WinFormsClipboard.GetAudioStream(); } return; } if (_files) { if (WinFormsClipboard.ContainsFileDropList()) { StringCollection paths = WinFormsClipboard.GetFileDropList(); List <FileSystemInfo> infos = new List <FileSystemInfo>(); foreach (string p in paths) { if (File.Exists(p)) { infos.Add(new FileInfo(p)); } else if (Directory.Exists(p)) { infos.Add(new DirectoryInfo(p)); } } _result = infos.ToArray(); } return; } if (_html) { if (WinFormsClipboard.ContainsText(TextDataFormat.Html)) { string content = WinFormsClipboard.GetText(TextDataFormat.Html); RegexOptions regexOptions = RegexOptions.Singleline | RegexOptions.IgnoreCase; if (_html && _htmlFragment) { Regex regex = new Regex("<!--StartFragment-->(.*)<!--EndFragment-->", regexOptions); Match match = regex.Match(content); if (match.Success) { content = match.Groups[1].Value; } } else if (_html) { Regex regex = new Regex(".*?(<HTML>.*)", regexOptions); Match match = regex.Match(content); if (match.Success) { content = match.Groups[1].Value; } } _result = content; return; } return; } if (WinFormsClipboard.ContainsText()) { _result = WinFormsClipboard.GetText(RequestedTextFormat); return; } }
private Cancel ValidateFormat(EditType editType, string character) { Cancel resultCancel = new Cancel(); string previewText = string.Empty; string text = base.Text; #region Negative if (character == "-") { if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number || this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { if (this.MaskProp.AllowNegative == false || this.Text.Contains("-")) { resultCancel.IsCancel = true; return(resultCancel); } if (text != string.Empty && Convert.ToDecimal(text) != 0) { int selectionStart = this.SelectionStart + 1; base.Text = string.Concat("-", text); this.Select(selectionStart, 0); resultCancel.IsCancel = true; return(resultCancel); } } } else if (character == "+" && text.Contains("-")) { if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number || this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { int selectionStart = this.SelectionStart > 0 ? this.SelectionStart - 1 : 0; base.Text = text.Substring(1); this.Select(selectionStart, 0); resultCancel.IsCancel = true; return(resultCancel); } } #endregion #region Separator if (new string[] { ".", "," }.Contains(character) && this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { int index = text.IndexOf(DecimalSeparator); if (index != -1) { this.Select(index + 1, 0); } resultCancel.IsCancel = true; return(resultCancel); } #endregion #region Move cursor if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { if (editType == EditType.Delete) { int index = text.IndexOf(DecimalSeparator); if (index != -1 && this.SelectionStart == index && this.SelectionLength == 0) { this.Select(index + 1, 0); resultCancel.IsCancel = true; return(resultCancel); } } else if (editType == EditType.Backspace) { int index = text.IndexOf(DecimalSeparator); if (index != -1 && this.SelectionStart == index + 1 && this.SelectionLength == 0) { this.Select(index, 0); resultCancel.IsCancel = true; return(resultCancel); } } } #endregion #region Preview Text string left_text = text.Substring(0, this.SelectionStart); string selected_text = this.SelectedText; string right_text = text.Substring(this.SelectionStart + this.SelectionLength); switch (editType) { case EditType.NewCharacter: previewText = left_text + character + right_text; break; case EditType.Cut: previewText = left_text + right_text; if (this.SelectionLength > 0) { Clipboard.Clear(); Clipboard.SetText(selected_text); } break; case EditType.Paste: if (Clipboard.ContainsText()) { selected_text = Clipboard.GetText(); } previewText = left_text + selected_text + right_text; break; case EditType.Delete: if (selected_text.Length == 0) { if (right_text.Length > 0) { right_text = right_text.Substring(1); } } else { selected_text = ""; } previewText = left_text + selected_text + right_text; break; case EditType.Backspace: if (selected_text.Length == 0) { if (left_text.Length > 0) { left_text = left_text.Substring(0, this.SelectionStart - 1); } } else { selected_text = ""; } previewText = left_text + selected_text + right_text; break; case EditType.Text: selected_text = character; previewText = character; break; } #endregion #region Integer digits number if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number) { if (previewText.Replace("-", "").Length > MaskProp.IntegerDigits) { resultCancel.IsCancel = true; return(resultCancel); } } #endregion #region Number/Decimal Empty if (this.MaskProp.NumberEmpty == NumberEmpty.Zero && previewText.Length == 0) { if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number) { base.Text = "0"; this.SelectionStart = editType == EditType.Backspace ? 0 : 1; resultCancel.IsCancel = true; return(resultCancel); } else if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { base.Text = 0m.ToString("N" + MaskProp.NumberDecimalDigits); this.SelectionStart = 0; resultCancel.IsCancel = true; return(resultCancel); } } #endregion #region Validate type if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number) { Int64 test_value; if (Int64.TryParse(previewText, out test_value) == false) { resultCancel.IsCancel = true; return(resultCancel); } else if (test_value < 0 && this.MaskProp.AllowNegative == false) { resultCancel.IsCancel = true; return(resultCancel); } } else if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { if (previewText.Contains(DecimalSeparator) == false) { #region Decimal separator Int32 indexSep = base.Text.IndexOf(DecimalSeparator); if (indexSep == -1) { indexSep = previewText.Length; } Int32 endCursor = this.SelectionStart + this.SelectionLength; if (endCursor == indexSep + 1) { previewText = previewText.Insert(previewText.Length - MaskProp.NumberDecimalDigits, DecimalSeparator); } else if (endCursor == this.Text.Length) { previewText = string.Concat(previewText, DecimalSeparator, new string('0', MaskProp.NumberDecimalDigits)); } else { previewText = string.Concat(previewText.Substring(0, previewText.Length - (this.Text.Length - endCursor)), DecimalSeparator, new string('0', MaskProp.NumberDecimalDigits - (this.Text.Length - endCursor)), previewText.Substring(previewText.Length - (this.Text.Length - endCursor))); } #endregion } previewText = previewText.Replace(GroupSeparator, ""); Int32 indexSeparator = previewText.IndexOf(DecimalSeparator); if (indexSeparator != -1 && previewText.Substring(indexSeparator + 1).Length > MaskProp.NumberDecimalDigits) { previewText = previewText.Substring(0, indexSeparator + 1 + MaskProp.NumberDecimalDigits); } Decimal test_value; if (Decimal.TryParse(previewText, out test_value) == false) { resultCancel.IsCancel = true; return(resultCancel); } else if (test_value < 0 && this.MaskProp.AllowNegative == false) { resultCancel.IsCancel = true; return(resultCancel); } } else if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Letter) { if (previewText != string.Empty && System.Text.RegularExpressions.Regex.IsMatch(previewText, @"^[a-zA-Z]+$") == false) { resultCancel.IsCancel = true; return(resultCancel); } } else if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.LetterAndNumber) { if (previewText != string.Empty && System.Text.RegularExpressions.Regex.IsMatch(previewText, @"^[a-zA-Z0-9]+$") == false) { resultCancel.IsCancel = true; return(resultCancel); } } #endregion #region Zero left number if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Number && this.MaskProp.NumberZeroLeft == false && previewText.Length > 1 && previewText.StartsWith("0")) { int rightText = text.Substring(this.SelectionStart + this.SelectionLength).Length; base.Text = Convert.ToInt64(previewText).ToString(); this.Select((rightText > base.Text.Length ? 0 : base.Text.Length - rightText), 0); resultCancel.IsCancel = true; return(resultCancel); } #endregion #region Decimal digits number if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal) { if (this.SelectionStart <= base.Text.IndexOf(DecimalSeparator) && previewText.Replace("-", "").Substring(0, previewText.IndexOf(DecimalSeparator)).Length > MaskProp.IntegerDigits) { resultCancel.IsCancel = true; return(resultCancel); } } #endregion #region Format decimal if (this.MaskProp.Mask == System.Windows.Forms.Controls.Mask.Decimal && previewText != string.Empty) { int selectionStartRigth = 0; int indexSeparator = 0; int selectionLength = 0; bool fullSelection = false; if (this.SelectionLength > 0) { selectionStartRigth = base.Text.Length - (this.SelectionStart + this.SelectionLength); selectionLength = base.SelectionLength; fullSelection = base.Text.Length == base.SelectionLength; base.Text = Convert.ToDecimal(previewText).ToString("N" + MaskProp.NumberDecimalDigits); indexSeparator = base.Text.IndexOf(DecimalSeparator); } else { selectionStartRigth = base.Text.Length - this.SelectionStart; base.Text = Convert.ToDecimal(previewText).ToString("N" + MaskProp.NumberDecimalDigits); indexSeparator = base.Text.IndexOf(DecimalSeparator); } if (editType == EditType.Backspace) { if (this.Text.Length - selectionStartRigth > indexSeparator) { this.Select(selectionStartRigth >= base.Text.Length ? 0 : this.Text.Length - selectionStartRigth - 1, 0); } else { if (this.Text.Length - selectionStartRigth > 0 && this.Text[this.Text.Length - selectionStartRigth - 1].ToString() == GroupSeparator) { this.Select(this.Text.Length - selectionStartRigth - 1, 0); } else { this.Select(selectionStartRigth >= base.Text.Length ? 0 : this.Text.Length - selectionStartRigth, 0); } } } else if (editType == EditType.Delete) { if (this.Text.Length - selectionStartRigth < indexSeparator && this.Text.Length - selectionStartRigth > 0 && this.Text[this.Text.Length - selectionStartRigth].ToString() == GroupSeparator) { this.Select(this.Text.Length - selectionStartRigth + 1, 0); } else if (this.Text.Length - selectionStartRigth > indexSeparator && selectionLength == 0) { this.Select(this.Text.Length - selectionStartRigth + 1, 0); } else if (this.Text.Length - selectionStartRigth > indexSeparator && selectionLength > 0) { this.Select(this.Text.Length - selectionStartRigth, 0); } else { this.Select(selectionStartRigth > base.Text.Length ? 0 : this.Text.Length - selectionStartRigth, 0); } } else { if (fullSelection) { this.Select(indexSeparator, 0); } else if (this.Text.Length - selectionStartRigth > indexSeparator && selectionLength == 0) { this.Select(this.Text.Length - selectionStartRigth + 1, 0); } else if (this.Text.Length - selectionStartRigth > indexSeparator && selectionLength > 0) { this.Select(this.Text.Length - selectionStartRigth, 0); } else { this.Select(this.Text.Length - selectionStartRigth, 0); } } resultCancel.IsCancel = true; resultCancel.Sucess = true; return(resultCancel); } #endregion #region Letter, number and all if (this.MaskProp.Mask == Mask.Letter || this.MaskProp.Mask == Mask.LetterAndNumber || this.MaskProp.Mask == Mask.Number || this.MaskProp.Mask == Mask.All) { bool fullSelection = base.Text.Length == base.SelectionLength && base.Text.Length > 0; int selectionLength = this.SelectionLength; int selectionStart = this.SelectionStart; base.Text = previewText; if (editType == EditType.Backspace) { if (selectionLength == 0) { this.Select(selectionStart > 0 ? selectionStart - 1 : 0, 0); } else { this.Select(selectionStart > 0 ? selectionStart : 0, 0); } } else if (editType == EditType.Delete || editType == EditType.Cut) { this.Select(selectionStart <= base.Text.Length ? selectionStart : base.Text.Length, 0); } else { if (fullSelection && editType == EditType.NewCharacter) { this.Select(1, 0); } else if (fullSelection) { this.Select(this.Text.Length, 0); } else if (editType == EditType.Paste) { this.Select(selectionStart + selected_text.Length, 0); } else { this.Select(selectionStart + 1, 0); } } resultCancel.IsCancel = true; resultCancel.Sucess = true; return(resultCancel); } #endregion return(resultCancel); }