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

我的表单布局

这件事没有任何帮助。当我单击我的文本框并单击 ont button1 时,此处不会出现 1.. 并且文本框焦点也丢失了..

4

1 回答 1

0

好吧,如果我得到你,你需要类似的东西

private TextBox _lastFocused; defined in your form

然后将 OnEnter 或 OnExit 添加到每个参与的文本框设置 _lastFocused,这样您就知道您希望按键转到哪个控件

然后在你的 buttonClick(s)

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 回答