1
private void btnPress_Click(object sender, EventArgs e)
    {
        String key = ((Button)sender).Text;
        InputSimulator.SimulateTextEntry(key);
        SendKeys.SendWait(VirtualKeyCode.NUMPAD4.ToString());
    }

[DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new KeyPress());
    }

私のフォームレイアウト

このことは助けてください動作していません。テキストボックスをクリックしてボタン1をクリックすると、ここに1が表示されず、テキストボックスのフォーカスも失われます..

4

1 に答える 1

0

私があなたを手に入れたら、あなたは次のようなものが必要です

private TextBox _lastFocused; defined in your form

次に、関係する各テキストボックス設定 _lastFocused に OnEnter または OnExit を追加します。これにより、キーを押したときにどのコントロールに移動するかがわかります。

次に、ボタンクリックで

String key = ((Button)sender).Text;
_lastFocused.Focus();
InputSimulator.SimulateTextEntry(key);
SendKeys.SendWait(VirtualKeyCode.NUMPAD4.ToString()); 
((Button)sender).Focus(); // if you want to out focus back the button clicked

何が起こっているかを見る限り、ボタンをクリックすると、フォーカスがテキストボックスからボタンに移動します。Sendkeys は、シミュレートされたキー押下を、もちろんあなたのボタンである現在フォーカスされているコントロールにルーティングします。したがって、修正は、フォーカスを最後に来た場所に戻すだけです。

于 2012-08-24T12:45:52.260 に答える