2012-04-12 25 views
3

は、私は別のビューへの最初のボタンの変更をクリックしたときに WPFの仮想キーボード:実際の入力デバイスをエミュレートしますか?

|---|---|---| 9 char in first block (abcdefgijk) 

|---|---|---| 

|---|---|---|

は、9ボタンを持っているようにwin7の中で、単純な仮想キーボードを作成したいです。

|---|---|---| 
a  b c 
|---|---|---| 
e f g  
|---|---|---| 
i j k

は今、私は別のアプリケーションに文字出力を生成することができ、プレス、キーボードのようなボタンをクリックする方法について混乱しています。私は使用しています

System.Windows.Forms.SendKeys.SendWait(sendString); 

しかし、動作しません。私はConsole.WriteLine(sendString);のように見えるのでsendStringが正しいことを知っています。

もう1つの質問は、ボタンをクリックするとフォーカスがボタンに戻りません。

誰でもこのキーボードを実装する方法はありますか?

ありがとうございます!私はボタンの多くの時間をクリックすると、それはまた、いくつかの問題を抱えている

は、返信いただきありがとうございますし、実際に私はすでに、これらのコード を追加しました。ボタンをクリックしてもフォーカスは常にボタンに集中します。このサンプルでは2つのボタンしか含まれていません。それらのコードがエラーを見つけるのを助けることができますか?多くの人が思う!

namespace WpfApplication5 
{ 
    public partial class UserControl1 : UserControl 
    { 
     [DllImport("user32.dll")] 
     public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex); 

     private IInputElement focusedInputElement; 
     private Window parentWindow; 
     private List<Button> keyCollection = new List<Button>(); 

     public UserControl1(Window parent) 
     { 
      this.parentWindow = parent; 
      this.setupKeyboardControl(); 
     } 

     public UserControl1(IInputElement elementToFocusOn) 
     { 
      // set focus 
      this.focusedInputElement = elementToFocusOn; 
      // setup this control 
      this.setupKeyboardControl(); 
     } 

     private void setupKeyboardControl() 
     { 
      InitializeComponent(); 
      // add all keys to internal collection 
      this.addAllKeysToInternalCollection(); 
      // install clicks 
      this.installAllClickEventsForCollection(this.keyCollection); 
     } 

     private void addAllKeysToInternalCollection() 
     { 
      // itterate all panels 
      // itterate all buttons 
      // add to list 
      Console.WriteLine("Run at here"); 
      this.keyCollection.Add(A); 
      this.keyCollection.Add(B); 
     } 

     /// <summary> 
     /// Install click events for all keys in a collection 
     /// </summary> 
     /// <param name="keysToInstall"></param> 
     private void installAllClickEventsForCollection(List<Button> keysToInstall) 
     { 
      // itterate all 
      foreach (Button buttonElement in keysToInstall) 
      { 
       // install click event 
       buttonElement.Click += new RoutedEventHandler(buttonElement_Click); 
      } 
     } 

     /* private void Window_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.A) 
       A.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 
      else if (e.Key == Key.B) 
       B.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); 
     }*/ 

     void buttonElement_Click(object sender, RoutedEventArgs e) 
     { 
      // create variable for holding string 
      String sendString = ""; 

      try 
      { 
       // stop all event handling 
       e.Handled = true; 

       // set sendstring to key 
       sendString = ((Button)sender).CommandParameter.ToString(); 

       // if something to send 
       if (!String.IsNullOrEmpty(sendString)) 
       { 
        // if sending a string 
        if (sendString.Length > 1) 
        { 
         // add {} 
         sendString = "{" + sendString + "}"; 
        } 

        // if a focusable element has been specified 
        if (this.focusedInputElement != null) 
        { 
         Console.WriteLine("1",sendString); 
         // set keyboard focus 
         Keyboard.Focus(this.focusedInputElement); 
         // set normal focus 
         this.focusedInputElement.Focus(); 
        } 

        // send key to simulate key press 
        // System.Windows.Forms.SendKeys.Send(sendString); 

        System.Windows.Forms.SendKeys.SendWait(sendString); 
        Console.WriteLine(sendString); 

       } 
      } 
      catch (Exception) 
      { 
       // do nothing - not important for now 
       Console.WriteLine("Could not send key press: {0}", sendString); 
      } 
     } 

     private void UserControl_Loaded(object sender, RoutedEventArgs e) 
     { 
      // if we have specified a parent 
      if (this.parentWindow != null) 
      { 
       // Get this window's handle 
       IntPtr HWND = new WindowInteropHelper(this.parentWindow).Handle; 
       Console.WriteLine("Run in UserControl load"); 
       // style of window? 
       int GWL_EXSTYLE = (-20); 
       // get - retrieves information about a specified window 
       GetWindowLong(HWND, GWL_EXSTYLE); 
       // set - changes the attribute of a specified window - I think this stops it being focused on 
       SetWindowLong(HWND, GWL_EXSTYLE, (IntPtr)(0x8000000)); 
      } 
     } 
    } 
} 
+0

あなたはキーの文字を渡すことができる引数からKeyEventsを使用する必要があります – Prabhavith

答えて

0

これを行うための最も確実な方法は、あなたがCodePlexの

http://wosk.codeplex.com/に作業アプリケーションと完全に何をしたいのかの良い例があるのWin32 API

[DllImport("user32.dll")] 
public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize); 

[DllImport("user32.dll", EntryPoint = "SendInput")] 
public static extern uint SendInput(uint nInputs, InputKeys[] inputs, int cbSize); 

を使用することです

+0

私はすでにそれらのコードを追加しています –

関連する問題