2012-01-26 17 views
9

C# Windows Forms ile bir ekran klavyesi hazırladım. Tuş vuruşlarını göndermek için Sendkeys.Send() işlevini kullanıyorum. Tüm harfler ancak i harfleri düzgün çalışıyor. Microsoft Word açıkken ben klavyedeki harf i bastığınızda, o Ctrl + ALT + Ben gönderir ve yazdırma iletişim kutusu açılır. Not Defteri'nde ++ da aynıdır. Ama notepad yazmayı denediğimde iyi çalışıyor.SendKeys ile 'i' harfi gönderme

Kodumda, SendKeys.Send(value); anahtarlarını gönderirim; burada değer, basılan düğmenin metnidir. Aşağıdaki kodu kullanarak metni alırım:

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

Neden düzgün çalışmıyor?

Düzenleme: Sadece bir düğme ile yeni bir windows form projesi oluşturdum ve tüm kod aşağıda. Hala çalışmıyor. Herhangi bir fikir takdir edilecektir.

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; 
      } 
     } 
    } 

Geçersiz kılınan işlev, formun odaklanmasını engellemektir. Böylece tuş vuruşlarını odağı olan diğer uygulamaya gönderebiliyorum.

+0

Öyle mi x saniye boyunca düğmeye basarak, bir pencereye anahtarı gönderir? 'Value' nedir, bu bir anahtar kelime değil midir? Bazı duruşlarda, '((MyClass) nesnesiyle' 'yi kullanarak yazmaktan ziyade' '(nesne olarak MyClass)' i kullanarak döküm yapmayı tercih edebilirsiniz. Nesnenin bir sınıf dışı bir istisna atmak yerine, objektif bir MyClass değilse, ikincisi null değerini döndürür. – MoonKnight

+0

Çok üzgünüm. Bunun yerine s değeri olacaktır. Sonuç, ben böyle yapmam bile değişmez: Sendkeys.Send ("i"); –

+0

's' nin elde edildiğini kontrol etmek için bir hata ayıklayıcı kullandınız mı? Bu, sorunu daraltmanıza yardımcı olacaktır. –

cevap

1

iki alternatif:

Ayrıca How to: Simulate Mouse and Keyboard Events in Code

, burada örnekten kod: Burada

MSDN'de bir örnek

1- bir tuş taklit eder, bakınız http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx

Örnek:

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

2- kesinlikle bu kod kısmında

[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 yöntemini çağırmıyorsunuz. Bu nedenle, "SendKeys" aramanızın, hedeflenen uygulama olarak değil, uygulamanıza anahtarlar göndermesi olasıdır.

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

Uygulamamı sadece odaklanamadım. Bu yüzden her zaman son uygulama, uygulamamdaki düğmeyi tıkladıktan sonra bile odaklanır. Bu yaklaşım yanlış mı? Not defteri ve wordpad üzerinde çalıştığımda çalışır, ancak ms word ve notepad ++ üzerinde çalışmıyor –

İlgili konular