2012-01-26 9 views
9

私はC#Windowsフォームでスクリーンキーボードを作った。私はSendkeys.Send()キーストロークを送信する機能を使用します。手紙以外のすべての手紙私はうまく動作します。私は、キーボードの上に手紙を押すと、Microsoft Wordが開いているとき、それは私がはCtrl +Altキー + を送信し、印刷ダイアログを開きます。メモ帳++でも同様です。しかし、メモ帳に入力しようとするとうまくいきます。SendKeysで文字 'i'を送信

私のコードでは、SendKeys.Send(value);とキーを送ります。ここで、valueは押されたボタンのテキストです。私は次のコードでテキストを取得します:

string s = ((Button)sender).Text; 

なぜそれが正しく動作しないのですか?

編集:私はちょうどボタンで新しいウィンドウフォームプロジェクトを作成し、全体のコードは以下のとおりです。まだ動かない。任意のアイデアをいただければ幸いです。

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      SendKeys.Send("i"); 
     } 

     // Prevent form being focused 
     const int WS_EX_NOACTIVATE = 0x8000000; 
     protected override CreateParams CreateParams 
     { 
      get 
      { 
       CreateParams ret = base.CreateParams; 
       ret.ExStyle |= WS_EX_NOACTIVATE; 
       return ret; 
      } 
     } 
    } 

オーバーライドされた関数は、フォームがフォーカスされないようにすることです。フォーカスがある他のアプリケーションにキーストロークを送ることができるように。

+0

がそれであるX秒間ボタンを押すと、ウィンドウにキーを送信しますか? 'value'とは何ですか?これはキーワードではありませんか?あなたは '((MyClass)オブジェクトを使ってキャスティングするのではなく、'(オブジェクトをMyClass) 'を使ってキャスティングするのではなく、それを見つけるかもしれません。 objがMyClassでない場合は、クラスキャスト例外をスローするのではなく、2つ目はnullを返します。 – MoonKnight

+0

本当に申し訳ありません。値sの代わりに文字列sになります。このようにしても結果は変わりません:Sendkeys.Send( "i"); –

+0

デバッガを使って 's'の値がどのようになっているかチェックしましたか?これは、問題の絞り込みに役立ちます。 –

答えて

1

つの選択肢:

How to: Simulate Mouse and Keyboard Events in Code

はまた、ここでの例からコードです

の1- が見る、キー入力をシミュレートhttp://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx

サンプル:

public static void ManagedSendKeys(string keys) 
     { 
      SendKeys.SendWait(keys); 
      SendKeys.Flush(); 
     } 

2 - は間違いなく、コードのこの部分で

[DllImport("user32.dll")] 
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); 
public static void KeyboardEvent(Keys key, IntPtr windowHandler, int delay) 
     { 
      const int KEYEVENTF_EXTENDEDKEY = 0x1; 
      const int KEYEVENTF_KEYUP = 0x2; 
      keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0); 
      Thread.Sleep(delay); 
      keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0); 
     } 
0

"SetForegroundWindow" Win32 APIメソッドを呼び出していません。したがって、あなたの "SendKeys"コールは目的のアプリケーションではなく、あなたのアプリにキーを送る可能性が高いです。ここで

は、MSDNの例です:

using System; 
using System.Runtime.InteropServices; 
using System.Drawing; 
using System.Windows.Forms; 

namespace SimulateKeyPress 
{ 
    class Form1 : Form 
    { 
     private Button button1 = new Button(); 

     [STAThread] 
     public static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.Run(new Form1()); 
     } 

     public Form1() 
     { 
      button1.Location = new Point(10, 10); 
      button1.TabIndex = 0; 
      button1.Text = "Click to automate Calculator"; 
      button1.AutoSize = true; 
      button1.Click += new EventHandler(button1_Click); 

      this.DoubleClick += new EventHandler(Form1_DoubleClick); 
      this.Controls.Add(button1); 
     } 

     // Get a handle to an application window. 
     [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] 
     public static extern IntPtr FindWindow(string lpClassName, 
      string lpWindowName); 

     // Activate an application window. 
     [DllImport("USER32.DLL")] 
     public static extern bool SetForegroundWindow(IntPtr hWnd); 

     // Send a series of key presses to the Calculator application. 
     private void button1_Click(object sender, EventArgs e) 
     { 
      // Get a handle to the Calculator application. The window class 
      // and window name were obtained using the Spy++ tool. 
      IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator"); 

      // Verify that Calculator is a running process. 
      if (calculatorHandle == IntPtr.Zero) 
      { 
       MessageBox.Show("Calculator is not running."); 
       return; 
      } 

      // Make Calculator the foreground application and send it 
      // a set of calculations. 
      SetForegroundWindow(calculatorHandle); 
      SendKeys.SendWait("111"); 
      SendKeys.SendWait("*"); 
      SendKeys.SendWait("11"); 
      SendKeys.SendWait("="); 
     } 

     // Send a key to the button when the user double-clicks anywhere 
     // on the form. 
     private void Form1_DoubleClick(object sender, EventArgs e) 
     { 
      // Send the enter key to the button, which raises the click 
      // event for the button. This works because the tab stop of 
      // the button is 0. 
      SendKeys.Send("{ENTER}"); 
     } 
    } 
} 
+0

私はちょうど私のアプリケーションをnonfocusableにしました。したがって、アプリケーションのボタンをクリックしても、常に最後のアプリケーションがフォーカスされます。このアプローチは間違っていますか?それは私がメモ帳とワードパッドでそれを試しても動作しますが、ms単語やメモ帳では動作しません++ –

関連する問題