kingfirekkk 发表于 2010-12-14 10:32:02

[已解决]--答案见3楼--如何确认当前按键是来自USB外接键盘or内置键盘?

本帖最后由 kingfirekkk 于 2011-1-26 08:18 编辑

最近从论坛里面找出了一个键盘测试的源代码,源贴地址:
http://www.autoitx.com/forum.php?mod=viewthread&tid=11216&highlight=%BC%FC%C5%CC
被我改成了,只要按了F1~F12后便自动退出了,只是当有时候如果外接了USB键盘以后,就会出现按笔记的键盘是没有用的!
提问问题:
有没有什么方法切换当前键盘的??可以自由的在USB外接键盘与内置的键盘间切换的?
#include <Misc.au3>

Dim $KBVirtualCode = [0x1B, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x13, 0x2C, 0x2D, 0x2E, _
                0xC0, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0xBD, 0xBB, 0x08, _
                0x09, 0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49, 0x4F, 0x50, 0xDB, 0xDD, 0xDC, _
                0x14, 0x41, 0x53, 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0xBA, 0xDE, 0x0D, _
                0xA0, 0x5A, 0x58, 0x43, 0x56, 0x42, 0x4E, 0x4D, 0xBC, 0xBE, 0xBF, 0xA1, 0x26, _
                0x23, 0xA2, 0x5B, 0xA4, 0x20, 0xA5, 0x5D, 0xA3, 0x25, 0x28, 0x27]
Dim $KBText = ["Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Pause", "PrtScr", "Ins", "Del", _
                "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace", _
                "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\", _
                "Cap", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter", _
                "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Shift", "UP", _
                "Fn+right", "Ctrl", "Win", "Alt", "Space", "Alt", "APPS", "Ctrl", "Left", "Down", "Right"]
Dim $KBCtrl
Dim $KBState
$dll = DllOpen("user32.dll")
Dim $a0 = 0, $a1 = 0, $a2 = 0, $a3 = 0, $a4 = 0, $a5 = 0, $a6 = 0, $a7 = 0, $a8 = 0, $a9 = 0, $a10 = 0, $a11 = 0

GUICreate("KB Test V1.0 - {Press Pause to Exit}", 905, 400, -1, -1, 0x00800000)
GUISetFont(10, 600)

$x = 10
$y = 10
For $i = 0 To 81
        Switch $i
                Case 0 To 16 ; Esc - Del
                        $w = 50
                        $h = 40
                Case 17 ; ~
                        $x = 10
                        $y += $h + 2
                        $w = 40
                        $h = 59
                Case 30 ; <- backspace
                        $w = 108
                Case 31 ; Tab
                        $x = 10
                        $y += $h + 2
                        $w = 65
                Case 44 ; \
                        $w = 83
                Case 45 ; Cap
                        $x = 10
                        $y += $h + 2
                        $w = 80
                Case 57 ; Enter
                        $w = 129
                Case 58 ; Shift
                        $x = 10
                        $y += $h + 2
                        $w = 120
                Case 66, 67, 68, 70, 79, 80, 81 ; <>?UP
                        $w = 41
                Case 69 ; Shift
                        $w = 118
                Case 71 ; Fn
                        $x = 10
                        $y += $h + 2
                        $w = 70
                Case 75 ; Space
                        $w = 286
                Case 78 ; Ctrl
                        $w = 88
                Case Else ; Others
                        $w = 59
        EndSwitch
        $KBCtrl[$i] = GUICtrlCreateLabel($KBText[$i], $x, $y, $w, $h, 0x0201)
        GUICtrlSetBkColor(-1, 0xB0B0B0)
        $x += $w + 2
Next

GUISetState()

;~ HotKeySet("{pause}", "_Pause_Btn")
While 1
        del()
        For $i = 0 To 81
                $key = _API_GetKey($KBVirtualCode[$i])
                If BitAND($key, 0x8000) And $KBState[$i] <> 1 Then
                        GUICtrlSetBkColor($KBCtrl[$i], 0x008000)
                        $KBState[$i] = 1
                EndIf
                If $KBState[$i] = 1 And Not BitAND($key, 0x8000) Then
                        $KBState[$i] = 2
                        GUICtrlSetBkColor($KBCtrl[$i], 0x00FF00)
                EndIf
        Next
WEnd


Func del()
;~ Func _Pause_Btn()
        If _IsPressed("70", $dll) Then $a0 = 1
;~         MsgBox(0, "keytest", "你按了F1键")
        If _IsPressed("71", $dll) Then $a1 = 1
;~         MsgBox(0, "keytest", "你按了F2键")
       
        If _IsPressed("72", $dll) Then $a2 = 1
;~         MsgBox(0, "keytest", "你按了F3键")
       
        If _IsPressed("73", $dll) Then $a3 = 1
;~         MsgBox(0, "keytest", "你按了F4键")
       
        If _IsPressed("74", $dll) Then $a4 = 1
;~         MsgBox(0, "keytest", "你按了F5键")
       
        If _IsPressed("75", $dll) Then $a5 = 1
;~         MsgBox(0, "keytest", "你按了F6键")
       
        If _IsPressed("76", $dll) Then $a6 = 1
;~         MsgBox(0, "keytest", "你按了F7键")
       
        If _IsPressed("77", $dll) Then $a7 = 1
;~         MsgBox(0, "keytest", "你按了F8键")
       
        If _IsPressed("78", $dll) Then $a8 = 1
;~         MsgBox(0, "keytest", "你按了F9键")
       
        If _IsPressed("79", $dll) Then $a9 = 1
;~         MsgBox(0, "keytest", "你按了F10键")
       
        If _IsPressed("7A", $dll) Then $a10 = 1
;~         MsgBox(0, "keytest", "你按了F11键")
       
        If _IsPressed("7B", $dll) Then $a11 = 1
;~         MsgBox(0, "keytest", "你按了F12键")
       
        If $a0 = 1 and$a1 = 1 and$a2 = 1 and$a3 = 1 and$a4 = 1 and$a5 = 1 and$a6 = 1 and$a7 = 1 and$a8 = 1 and$a9 = 1 and$a10 = 1 and$a11 = 1 Then
        DllClose($dll)
        Exit
        EndIf
       


EndFunc   ;==>del
;~ EndFunc   ;==>_Pause_Btn

Func _API_GetKey($code)
        Local $rKey
        $rKey = DllCall("user32.dll", "short", "GetKeyState", "int", $code)
        If @error Then
                MsgBox(0, "", "error")
                Return SetError(@error, 0, False)
        EndIf
        Return $rKey
EndFunc   ;==>_API_GetKey

lxz 发表于 2010-12-14 12:45:47

代码收下....

ceoguang 发表于 2010-12-14 14:46:37

参考@KBLayout及@OSLang

kingfirekkk 发表于 2010-12-14 16:57:07

回复 3# ceoguang

回头试试看,USB键盘的布局和笔记本的布局会不一样?

kingfirekkk 发表于 2011-1-13 10:49:06

贴个地址,键盘布局的:
http://zh.wikipedia.org/zh/%E9%94%AE%E7%9B%98%E5%B8%83%E5%B1%80#.E9.94.AE.E7.9B.98.E7.BB.93.E6.9E.84
页: [1]
查看完整版本: [已解决]--答案见3楼--如何确认当前按键是来自USB外接键盘or内置键盘?