[已解决]一段键盘测试代码无法退出
本帖最后由 waxy 于 2010-7-5 10:29 编辑在“源码区”看到gapkiller 兄弟写的一个键盘的测试工具,觉得挺方便的,修改了一下,但无法点右上角的“X”
退出程序。源码原来定义按“Pause"退出程序,觉得有点不方便,就想改成点窗口上的“X”退出。试了好久,也
想了蛮多办法,始终没有搞定。这不,没招了只得向大家请教了,代码如下------#NoTrayIcon
#include <WindowsConstants.au3>
#include <GUIConstantsex.au3>
#include <GUIConstants.au3>
#include <GuiButton.au3>
Dim $KBVirtualCode = [0x1B, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x2C,0x13 , 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, _
0xA2, 0x23, 0x5B, 0xA4, 0x20, 0xA5, 0x5D, 0xA3, 0x25, 0x28, 0x27]
Dim $KBText = ["Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "PrtScr", "Pause", "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", _
"Ctrl", "Fn+End", "Win", "Alt", "Space", "Alt", "APPS", "Ctrl", "Left", "Down", "Right"]
Dim $KBCtrl
Dim $KBState
GUICreate("KB_Test- 按ESC键退出测试",905,400,-1,-1);,0x00800000)
GUISetOnEvent ($GUI_EVENT_CLOSE, "KB_Test- 按ESC键退出测试" )
$Group1 = GUICtrlCreateGroup("", 5, 0, 892, 376)
GUISetFont(10,600)
GUISetState(@SW_SHOW)
$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+5
$w = 40
$h = 59
Case 30 ; <- backspace
$w = 108
Case 31 ; Tab
$x = 10
$y += $h+5
$w = 65
Case 44 ; \
$w = 83
Case 45 ; Cap
$x = 10
$y += $h+5
$w = 80
Case 57 ; Enter
$w = 129
Case 58 ; Shift
$x = 10
$y += $h+5
$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+5
$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("{esc}","_pause_Btn") ;HotKeySet("{pause}","_Pause_Btn")
While 1
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 _Pause_Btn()
;Exit 0
;EndFunc
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
好像大家都忙哦,没空理我啊。呵呵 afan,也不知道去哪了,好久没见到他。 再提一次,看有没有哪位可解决上述问题的。 本帖最后由 rikthhpgf2005 于 2010-7-3 23:27 编辑
#NoTrayIcon
#include <WindowsConstants.au3>
#include <GUIConstantsex.au3>
#include <GUIConstants.au3>
#include <GuiButton.au3>
Dim $KBVirtualCode = [0x1B, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x2C,0x13 , 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, _
0xA2, 0x23, 0x5B, 0xA4, 0x20, 0xA5, 0x5D, 0xA3, 0x25, 0x28, 0x27]
Dim $KBText = ["Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "PrtScr", "Pause", "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", _
"Ctrl", "Fn+End", "Win", "Alt", "Space", "Alt", "APPS", "Ctrl", "Left", "Down", "Right"]
Dim $KBCtrl
Dim $KBState
GUICreate("KB_Test- 按ESC键退出测试",905,400,-1,-1);,0x00800000)
$Group1 = GUICtrlCreateGroup("", 5, 0, 892, 376)
GUISetFont(10,600)
GUISetState(@SW_SHOW)
$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+5
$w = 40
$h = 59
Case 30 ; <- backspace
$w = 108
Case 31 ; Tab
$x = 10
$y += $h+5
$w = 65
Case 44 ; \
$w = 83
Case 45 ; Cap
$x = 10
$y += $h+5
$w = 80
Case 57 ; Enter
$w = 129
Case 58 ; Shift
$x = 10
$y += $h+5
$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+5
$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()
While 1
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
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
_Pause_Btn()
EndSwitch
WEnd
Func _Pause_Btn()
Exit
EndFunc
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 如果是加事件
就在前加
Opt("GUIOnEventMode", 1) 这代码我有点迷茫 看不懂` 回复 6# rikthhpgf2005
谢谢,我去试一下先。 基础啊基础
页:
[1]