From 8cfda6b09fc7b593c4b9da252d9c9be222334a20 Mon Sep 17 00:00:00 2001 From: ishikawakouji Date: Tue, 5 Dec 2023 16:55:27 +0900 Subject: [PATCH] through BS, Arrow key, when no text --- PushToWSLg/Form1.Designer.cs | 1 + PushToWSLg/Form1.cs | 73 +++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/PushToWSLg/Form1.Designer.cs b/PushToWSLg/Form1.Designer.cs index 059b821..ef88860 100644 --- a/PushToWSLg/Form1.Designer.cs +++ b/PushToWSLg/Form1.Designer.cs @@ -40,6 +40,7 @@ private void InitializeComponent() this.textBox1.Size = new System.Drawing.Size(424, 27); this.textBox1.TabIndex = 0; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); + this.textBox1.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.textBox1_PreviewKeyDown); // // PushToWSLg // diff --git a/PushToWSLg/Form1.cs b/PushToWSLg/Form1.cs index 4f0f83b..f41d346 100644 --- a/PushToWSLg/Form1.cs +++ b/PushToWSLg/Form1.cs @@ -99,13 +99,22 @@ public struct POINT static extern bool GetCursorPos(out POINT lpPoint); // カーソル位置にクリップボードのテキストを挿入 - private async void InsertClipboardTextAtCursor(string text) + private void InsertClipboardTextAtCursor(string text) { - POINT cursorPos; + Clipboard.SetText(text); //Clipboard.SetDataObject(text, false); + SendKeyEventToAnotherWindow("^v"); + + + } + + // send key event + private async void SendKeyEventToAnotherWindow(string etext) + { + POINT cursorPos; this.Hide(); await Task.Delay(200); @@ -113,7 +122,7 @@ private async void InsertClipboardTextAtCursor(string text) if (GetCursorPos(out cursorPos)) { // カーソル位置にクリップボードのテキストを挿入 - SendKeys.SendWait("^v"); + SendKeys.SendWait(etext); } this.Show(); @@ -121,7 +130,6 @@ private async void InsertClipboardTextAtCursor(string text) private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { - /* int textlen = textBox1.Text.Length; string text = ""; bool flagpaste = true; @@ -130,22 +138,6 @@ private void textBox1_KeyPress(object sender, KeyPressEventArgs e) switch (e.KeyChar) { - case (char)Keys.Left: - case (char)Keys.Right: - case (char)Keys.Back: - if (textlen == 0) - { - text += e.KeyChar; - } - else - { - flagpaste = false; - } - break; - case (char)Keys.Up: - case (char)Keys.Down: - text += e.KeyChar; - break; case (char)Keys.Enter: text = textBox1.Text; textBox1.Clear(); @@ -164,9 +156,8 @@ private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { InsertClipboardTextAtCursor(text); } - */ - + /* if (e.KeyChar == (char)Keys.Enter) { string text = textBox1.Text; @@ -182,8 +173,46 @@ private void textBox1_KeyPress(object sender, KeyPressEventArgs e) e.Handled = true; } + */ } + private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) + { + int textlen = textBox1.Text.Length; + string etext = ""; + + // enter, arrow, backspace + if (textlen == 0) + { + switch (e.KeyCode) + { + case Keys.Up: + etext = "{UP}"; + break; + case Keys.Down: + etext = "{DOWN}"; + break; + case Keys.Left: + etext = "{LEFT}"; + break; + case Keys.Right: + etext = "{RIGHT}"; + break; + case Keys.Back: + etext = "{BS}"; + break; + default: + break; + } + if (etext.Length != 0) + { + SendKeyEventToAnotherWindow(etext); + } + + } + + } + } }