VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 通用按键检测程序
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:13697, 日期:2002-03-29
通用按键检测程序
武汉 艾军
    在许多的热键操作软件中,都有按键检测的功能,那么你知道它是如果检测出来的吗?呵呵,只要你会用一点 VB,那就跟我来吧!
    先给大家介绍一下基础的工作原理。其实很简单,在文本框的 KeyDown 事件中,KeyCode 的值就是你所按按键的值了,再根据值进行一定的处理就可以了,下面我将给出完整的代码及详细的注释。
    1、首先进行程序界面的设计:(如 图1)
       窗口的属性:Caption="热键设置";BorderStyle=3;StartUpPosition=2;ControlBox=False
       装载 1 个 Frame 控件,其属性:Caption="按键测试"
       装载 1 个 Text 控件,其属性:Text="无"
       装载 1 个 Label 控件,其属性:Caption="例子:F10 或者 ALT+A";AutoSize=True
       装载 2 个 CommandButton 控件,其属性分别为:Caption="确定";Caption="取消";Index=0;Index=1
    2、程序代码如下:
       Form1.frm
Option Explicit
Dim KeyText As New Collection 注释:定义集合,用于保存键值数据
Dim MyShift, MyCtrl, MyAlt As Boolean 注释:用于判断组合键的状态
Dim KeyShift, KeyCtrl, KeyAlt, Key As Integer 注释:用于保存键值

Private Sub Command1_Click(Index As Integer)
    Select Case Index
        Case 0  注释:你可根据取得的各个键值,通过热键程序,定义自己的热键
            If Text1.Text = "无" Then Call KeyAll
            MsgBox "Key=" & Key & ";" & " KeyShift=" & KeyShift & ";" & " KeyCtrl=" & KeyCtrl & ";" & " KeyAlt=" & KeyAlt, , "键值"
            Text1.SetFocus
            Text1.SelStart = Len(Text1.Text)
        Case 1
            Unload Me
    End Select
End Sub

Private Sub Form_Load()
    Call AppendKey 注释:装载数据
    Text1.SelStart = Len(Text1.Text) 注释:给光标定位
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) 注释:按键按下时发生
On Error GoTo ErrorKey 注释:由于在使用组合键时,会发生一些错误

    Call KeyAll
    If KeyCode = 16 Then MyShift = True 注释:根据按键信息,定义组合键的状态(下同)
    If KeyCode = 17 Then MyCtrl = True
    If KeyCode = 18 Then MyAlt = True

    
    If MyCtrl = True Then 注释:判断 Ctrl+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Ctrl + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 2) <> 0 Then Text1.Text = "Ctrl + " 注释:判断键值,由于 KeyDown 事件会产生一些不确定的按键值(下同)
        KeyCtrl = 17
        Key = KeyCode
        If Key = KeyCtrl Then Key = 0
    End If
    
    If MyShift = True Then  注释:判断 Shift+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Shift + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 2) <> 0 Then Text1.Text = "Shift + "
        KeyShift = 16
        Key = KeyCode
        If Key = KeyShift Then Key = 0
    End If
    
    If MyAlt = True Then 注释:判断 Alt+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Alt + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 2) <> 0 Then Text1.Text = "Alt + "
        KeyAlt = 18
        Key = KeyCode
        If Key = MyAlt Then Key = 0
    End If
    
    If MyCtrl = True And MyAlt = True Then   注释:判断 Ctrl+Alt+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Ctrl + Alt + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 3) <> 0 Then Text1.Text = "Ctrl + Alt + "
    End If
    
    If MyShift = True And MyAlt = True Then   注释:判断 Shift+Alt+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Shift + Alt + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 3) <> 0 Then Text1.Text = "Shift + Alt + "
    End If
    
    If MyCtrl = True And MyShift = True Then   注释:判断 Ctrl+Shift+1 的按键模式"
        Text1.Text = ""
        Text1.Text = "Ctrl + Shift + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 3) <> 0 Then Text1.Text = "Ctrl + Shift + "
    End If
    
    If MyCtrl = True And MyShift = True And MyAlt = True Then 注释:判断 Ctrl+Shift+Alt+1 的按键模式
        Text1.Text = ""
        Text1.Text = "Ctrl + Shift + Alt + " + KeyText.Item(Format(KeyCode))
        If ynStr(Text1.Text, "+", 4) <> 0 Then Text1.Text = "Ctrl + Shift + Alt + "
    End If
    
    If MyShift <> True And MyCtrl <> True And MyAlt <> True Then 注释:判断一般的按键模式
        Text1.Text = KeyText.Item(Format(KeyCode)) 注释:单一按键
        Key = KeyCode
    End If
    
    Text1.SelStart = Len(Text1.Text)
        
    Exit Sub 注释:退出这部分的程序,不然会运行到下面的"错误"处
    
ErrorKey:
    Text1.Text = "无"
    Set KeyText = Nothing 注释:释放集合
    Call AppendKey 注释:重新装载数据
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    KeyAscii = 0 注释:抑制键盘的输入
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) 注释:按键弹起时发生
    If KeyCode = 16 Then MyShift = False 注释:设置组合键的为不可用(下同)
    If KeyCode = 17 Then MyCtrl = False
    If KeyCode = 18 Then MyAlt = False
    If Text1.Text = "Ctrl + " Or Text1.Text = "Shift + " Or Text1.Text = "Alt + " Or Text1.Text = "Ctrl + Alt + " Or Text1.Text = "Shift + Alt + " Or Text1.Text = "Ctrl + Shift + " Or Text1.Text = "Ctrl + Shift + Alt + " Then Text1.Text = "无"
End Sub

Function ynStr(ByVal str1 As String, ByVal str2 As String, ByVal j As Integer) As Integer 注释:自定义的判断函数,这里是用来判断键值的合法性
    Dim m As Integer
    For m = 1 To j
        ynStr = InStr(ynStr + 1, str1, str2)
    Next
End Function

Sub AppendKey() 注释:把有关按键的信息存放在集合中,这部分是键值与ASCII 码关系的资料
    KeyText.Add "Backspace", "8"
    KeyText.Add "Tab", "9"
    KeyText.Add "无", "13"
    KeyText.Add "Shift + ", "16"
    KeyText.Add "Ctrl + ", "17"
    KeyText.Add "Alt + ", "18"
    KeyText.Add "Pause", "19"
    KeyText.Add "CapsLock", "20"
    KeyText.Add "Esc", "27"
    KeyText.Add "无", "32"
    KeyText.Add "PageUp", "33"
    KeyText.Add "PageDown", "34"
    KeyText.Add "End", "35"
    KeyText.Add "Home", "36"
    KeyText.Add "Left", "37"
    KeyText.Add "Up", "38"
    KeyText.Add "Right", "39"
    KeyText.Add "Down", "40"
    KeyText.Add "Insert", "45"
    KeyText.Add "Delete", "46"
    KeyText.Add "0", "48"
    KeyText.Add "1", "49"
    KeyText.Add "2", "50"
    KeyText.Add "3", "51"
    KeyText.Add "4", "52"
    KeyText.Add "5", "53"
    KeyText.Add "6", "54"
    KeyText.Add "7", "55"
    KeyText.Add "8", "56"
    KeyText.Add "9", "57"
    KeyText.Add "A", "65"
    KeyText.Add "B", "66"
    KeyText.Add "C", "67"
    KeyText.Add "D", "68"
    KeyText.Add "E", "69"
    KeyText.Add "F", "70"
    KeyText.Add "G", "71"
    KeyText.Add "H", "72"
    KeyText.Add "I", "73"
    KeyText.Add "J", "74"
    KeyText.Add "K", "75"
    KeyText.Add "L", "76"
    KeyText.Add "M", "77"
    KeyText.Add "N", "78"
    KeyText.Add "O", "79"
    KeyText.Add "P", "80"
    KeyText.Add "Q", "81"
    KeyText.Add "R", "82"
    KeyText.Add "S", "83"
    KeyText.Add "T", "84"
    KeyText.Add "U", "85"
    KeyText.Add "V", "86"
    KeyText.Add "W", "87"
    KeyText.Add "X", "88"
    KeyText.Add "Y", "89"
    KeyText.Add "Z", "90"
    KeyText.Add "Num 0", "96"
    KeyText.Add "Num 1", "97"
    KeyText.Add "Num 2", "98"
    KeyText.Add "Num 3", "99"
    KeyText.Add "Num 4", "100"
    KeyText.Add "Num 5", "101"
    KeyText.Add "Num 6", "102"
    KeyText.Add "Num 7", "103"
    KeyText.Add "Num 8", "104"
    KeyText.Add "Num 9", "105"
    KeyText.Add "Num *", "106"
    KeyText.Add "Num +", "107"
    KeyText.Add "Num -", "109"
    KeyText.Add "Num .", "110"
    KeyText.Add "Num /", "111"
    KeyText.Add "F1", "112"
    KeyText.Add "F2", "113"
    KeyText.Add "F3", "114"
    KeyText.Add "F4", "115"
    KeyText.Add "F5", "116"
    KeyText.Add "F6", "117"
    KeyText.Add "F7", "118"
    KeyText.Add "F8", "119"
    KeyText.Add "F9", "120"
    KeyText.Add "F10", "121"
    KeyText.Add "F11", "122"
    KeyText.Add "F12", "123"
    KeyText.Add "NumLock", "144"
    KeyText.Add "ScrollLock", "145"
    KeyText.Add ";", "186"
    KeyText.Add "=", "187"
    KeyText.Add ",", "188"
    KeyText.Add "-", "189"
    KeyText.Add ".", "190"
    KeyText.Add "/", "191"
    KeyText.Add "~", "192"
    KeyText.Add "[", "219"
    KeyText.Add "\", "220"
    KeyText.Add "]", "221"
End Sub

Sub KeyAll() 注释:把键值全部清零
    KeyShift = 0
    KeyCtrl = 0
    KeyAlt = 0
    Key = 0
End Sub

    到此为此,程序全部完成,本程序在 VB6.0,Win98 环境下运行正常,大家如果还有什么问题,可到 www.d1vb.com 来一起讨论。