所以在我的计算器程序中,我想设置equals操作,通过按Enter键(在主键盘上)或按下回车键(在小键盘上)可以计算出数学问题。我知道这些数字是VK_NUMPAD0 - VK_NUMPAD9,VK_ADD,VK_SUBTRACT,VK_MULTIPLY,VK_DIVIDE。但是我似乎找不到一个输入键。 VK_ENTER用于键盘输入键。我还查看了KeyEvent.KEY_LOCATION_NUMPAD,但不确定如何将其添加到我的代码中以使其工作。我有一个名为SetupKeyInput()的方法,它接受一些参数来添加inputmap和actionmap的键绑定。
Private Sub recruit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recruit.Click
Dim query3 As String
Dim n As Integer
Dim query2 As String = "select max(stag) from agent"
con.ConnectionString = ("Data Source=DESKTOP-CTN5IJ3\SQLEXPRESS;Integrated Security=True")
Dim autono As New SqlCommand(query2, con)
con.Open()
If IsDBNull(autono.ExecuteScalar) Then
n = 7
Else
n = autono.ExecuteScalar + 5
End If
con.Close()
query3 = "insert into agent values(" + n + ",'" + ncrypt(txtssn.Text) + "','" + ncrypt(txtname.Text) + "','" + ncrypt(txtadd.Text) + "',0,0,'newbpwd')"
Dim save As New SqlCommand(query3, con)
con.Open()
save.ExecuteNonQuery()
con.Close()
End Sub
所以,我可以像这样调用函数
private void SetupKeyInput(JButton component, int keyStroke, int mask) {
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyStroke, mask), "hmm");
component.getActionMap().put("hmm", new MyButtonEvents(component));
}
我对小键盘输入缺少什么?我也试过这个
SetupKeyInput(buttonEquals, KeyEvent.VK_ENTER, 0); // Only works for keyboard enter