VB一个简单的键盘按键响应程序
'打开一个新的工程并将变量ShiftKey添加到窗体的声明部分中:'将Textbox控件添加到窗体上并将此过程添加到KeyDown事件中:PrivateSubText1_...
'打开一个新的工程并将变量 ShiftKey 添加到窗体的声明部分中:
'将 Textbox 控件添加到窗体上并将此过程添加到 KeyDown 事件中:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ShiftKey As Integer
ShiftKey = Shift And 7
Select Case ShiftKey
Case 1 ' 或 vbShiftMask
Label1.Caption = "You pressed the SHIFT key."
Case 2 ' 或 vbCtrlMask
Label1.Caption = "You pressed the CTRL key."
Case 4 ' 或 vbAltMask
Label1.Caption = "You pressed the ALT key."
Case 3
Label1.Caption = "You pressed both SHIFT and CTRL."
Case 5
Label1.Caption = "You pressed both SHIFT and ALT."
Case 6
Label1.Caption = "You pressed both CTRL and ALT."
Case 7
Label1.Caption = "You pressed SHIFT, CTRL, and ALT."
End Select
End Sub
为什么要Shift And 7?而不是 Shift?感谢各位对菜鸟的回答。 展开
'将 Textbox 控件添加到窗体上并将此过程添加到 KeyDown 事件中:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ShiftKey As Integer
ShiftKey = Shift And 7
Select Case ShiftKey
Case 1 ' 或 vbShiftMask
Label1.Caption = "You pressed the SHIFT key."
Case 2 ' 或 vbCtrlMask
Label1.Caption = "You pressed the CTRL key."
Case 4 ' 或 vbAltMask
Label1.Caption = "You pressed the ALT key."
Case 3
Label1.Caption = "You pressed both SHIFT and CTRL."
Case 5
Label1.Caption = "You pressed both SHIFT and ALT."
Case 6
Label1.Caption = "You pressed both CTRL and ALT."
Case 7
Label1.Caption = "You pressed SHIFT, CTRL, and ALT."
End Select
End Sub
为什么要Shift And 7?而不是 Shift?感谢各位对菜鸟的回答。 展开
展开全部
因为vbShiftMask,vbCtrlMask,vbAltMask是按位排列的,即2^n(n=0,1,2),分别为1,2,4,像这种情况,程序处理的时候就要用到位运算符(And)来处理这种数据。这个程序可以简化,你看看下面的。组合键情况显示在窗体Caption里。
代码如下。
=====================
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim s As String
If (Shift And vbShiftMask) = vbShiftMask Then s = s & "SHIFT" & " "
If (Shift And vbCtrlMask) = vbCtrlMask Then s = s & "CTRL" & " "
If (Shift And vbAltMask) = vbAltMask Then s = s & "ALT" & " "
Caption = s
End Sub
代码如下。
=====================
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim s As String
If (Shift And vbShiftMask) = vbShiftMask Then s = s & "SHIFT" & " "
If (Shift And vbCtrlMask) = vbCtrlMask Then s = s & "CTRL" & " "
If (Shift And vbAltMask) = vbAltMask Then s = s & "ALT" & " "
Caption = s
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询