在c#winforms中制作Numpad不起作用

时间:2012-08-24 12:20:52

标签: c# winforms keyboard keyboard-events

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

my form layout

这件事没有任何帮助请。当我点击我的文本框并单击ont按钮1然后1不会出现在这里..并且文本框焦点也会丢失..

1 个答案:

答案 0 :(得分: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将您的模拟按键路由到当前聚焦的控件,当然这是您的按钮。所以修复只是将焦点重新转移到你最后来的地方。