怎样在键盘按下的同时执行按钮操作并有押下的效果?
我想试着做一个类似打字的软件,比如按下A键可以执行按钮1的处理,并且还可以看到按钮被押下的效果。如能帮助,感激不尽。 --------------------编程问答-------------------- keypress、keydown事件 --------------------编程问答-------------------- Button难以直接代码控制其显示效果为按下状态,你最好使用checkbox控件,用checked属性控制呈现状态. --------------------编程问答-------------------- Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Select Case e.KeyChar
Case "A"
Button1_Click(Button1, New System.EventArgs)
Button2.Focus()
Case "B"
Button2_Click(Button2, New System.EventArgs)
Button3.Focus()
Case "C"
Button3_Click(Button3, New System.EventArgs)
Button1.Focus()
End Select
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("A")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("B")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MsgBox("C")
End Sub --------------------编程问答-------------------- 首先先把窗体的KeyPreview属性设置成True
方法1:
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Select Case e.KeyChar
Case Chr(13)
MsgBox("ok")
End Select
End Sub
方法2:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.A
MsgBox("ok")
End Select
End Sub --------------------编程问答-------------------- 使用以下类:
Public Class myCheckButton
Inherits CheckBox
Sub New()
Me.Appearance = Windows.Forms.Appearance.Button
Me.AutoCheck = False
Me.SetStyle(ControlStyles.Selectable, False)
End Sub
Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
MyBase.OnParentChanged(e)
Static Frm As Form
If Frm IsNot Nothing Then
RemoveHandler Frm.KeyDown, AddressOf ParentKeyDown
RemoveHandler Frm.KeyUp, AddressOf ParentKeyUp
RemoveHandler Frm.KeyPress, AddressOf ParentKeyPress
End If
Frm = GetParentForm(Me)
If Frm IsNot Nothing Then
Frm.KeyPreview = True
AddHandler Frm.KeyDown, AddressOf ParentKeyDown
AddHandler Frm.KeyUp, AddressOf ParentKeyUp
AddHandler Frm.KeyPress, AddressOf ParentKeyPress
End If
End Sub
Private Function GetParentForm(ByVal control As Control) As Form
Dim U_frm As Form = Nothing
If control IsNot Nothing Then
If control.Parent IsNot Nothing Then
Return GetParentForm(control.Parent)
Else
If TypeOf control Is Form Then
Return control
End If
End If
End If
Return U_frm
End Function
Private Sub ParentKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyData = _key Then
Me.Checked = True
OnKeyDown(e)
End If
End Sub
Private Sub ParentKeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyData = _key Then
Me.Checked = False
OnKeyUp(e)
End If
End Sub
Private Sub ParentKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
OnKeyPress(e)
End Sub
Private _key As Keys
<System.ComponentModel.Category("按键")> _
Public Property Key() As Keys
Get
Return _key
End Get
Set(ByVal value As Keys)
_key = value
End Set
End Property
End Class
编译后使用这个控件就行了,用法就是拖动控件到窗口,设置控件的Key属性为你要呈现的按键,再在控件的KeyPress事件中写你需要的操作,例:
myCheckButton1.Key=Keys.A
Sub myCheckButton1_KeyPress(...) handle ...
msgbox "A"
End Sub
补充:.NET技术 , VB.NET