找回密码
 加入
搜索
查看: 2956|回复: 1

[系统综合] 控制台程序如何直接读取到键盘的输入

[复制链接]
发表于 2011-7-2 15:40:15 | 显示全部楼层 |阅读模式
控制台程序如何直接读取到键盘的输入?请高人指点下!
 楼主| 发表于 2011-7-4 20:46:43 | 显示全部楼层

RE: 控制台程序如何直接读取到键盘的输入

自己搜索了一下,用下面的方法解了!!
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=..\Aut2Exe\Power\icon_plugged.ico
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
Opt('MustDeclareVars', 1)
HotKeySet("[","check")
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <StructureConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Sound.au3>
Dim $Form1,$Label1,$Label2,$Label3,$Label4,$Label5,$Label6,$Label7,$Label8,$Label9,$Input1
Global $hHook, $hStub_KeyProc,$keycode, $buffer = "", $nMsg,$start=0,$end=0,$USN="",$rescan=0
$Form1 = GUICreate("电源电压设定检查 by TE/Kemy Liu", 615, 438, -1, -1)
$Label1 = GUICtrlCreateLabel("序列号", 8, 16, 94, 41)
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("", 104, 16, 505, 45)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0000FF)
$Label2 = GUICtrlCreateLabel("测试状态", 5, 72, 100, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$Label3 = GUICtrlCreateLabel("等待测试开始...", 107, 77, 500, 56)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0x00FFFF)
$Label4 = GUICtrlCreateLabel("机台电源电压", 72, 144, 190, 41)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
$Label5 = GUICtrlCreateLabel("出货电源电压", 372, 144, 190, 41)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
$Label6 = GUICtrlCreateLabel("", 104, 200, 85, 41)
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008000)
GUICtrlSetBkColor(-1, 0xC0C0C0)
$Label7 = GUICtrlCreateLabel("", 429, 201, 85, 41)
GUICtrlSetFont(-1, 24, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x008000)
GUICtrlSetBkColor(-1, 0xC0C0C0)
$Label8 = GUICtrlCreateLabel("电源电压比对结果", 185, 261, 252, 41)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
$Label9 = GUICtrlCreateLabel("", 1, 322, 613, 115)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0xC0C0C0)
;~ GUICtrlSetBkColor(-1, 0x008000)
GUISetState(@SW_SHOW)
WinSetOnTop($Form1,"",1)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Input1
                        $USN=GUICtrlRead($Input1)
        EndSwitch
WEnd

Func check()
        WinActivate($Form1)
        Local $hmod,$SFCS,$115PN,$MBPN,$USN,$timestart,$dif
        If $start Then Return 1
        GUICtrlSetData($Input1,"")
        GUICtrlSetData($Label3,"正在进行测试...")
        $start=1
        $buffer=""
        $end=0
        $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
        $hmod = _WinAPI_GetModuleHandle(0)
        $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
        Do
                Sleep(10)
        Until $end
        _WinAPI_UnhookWindowsHookEx($hHook)
        DllCallbackFree($hStub_KeyProc)
;~         MsgBox(0,"buffer",$buffer)
;~         ToolTip($buffer)
        If $buffer=="ERROR" Or Not ($buffer=="115" Or $buffer=="230") Then
                $start=0
                GUICtrlSetData($Input1,"")
                GUICtrlSetData($Label3,"测试治具返回错误,请检查测试治具!")
                Return 1
        EndIf
        $SFCS=IniRead("Voltage.ini","setting","SFCS",0)
        $115PN=IniReadSection("Voltage.ini","115PN")
        $MBPN=IniReadSection("Voltage.ini","MBPN")
        $rescan=0
        $timestart=TimerInit()
        GUICtrlSetData($Input1,"")
        GUICtrlSetData($Label3,"请刷机台序列号!")
        $USN=""
        While 1
                If $Input1 == GUIGetMsg() Then
                        $USN=GUICtrlRead($Input1)
                        If StringLen($USN)<>23 And $USN <> "" Then
                                GUICtrlSetData($Label3,"机台序列号错误,请重刷!")
                                GUICtrlSetData($Input1,"")
                                $timestart=TimerInit()
                        EndIf
                        If Not CheckUSN($USN) Then
                                GUICtrlSetData($Label3,"请不要刷MB序列号,请刷机台序列号!")
                                GUICtrlSetData($Input1,"")
                                $timestart=TimerInit()
                        EndIf
                        If StringLen($USN)== 23 And CheckUSN($USN) Then
                                ExitLoop
                        EndIf
                        $dif=TimerDiff($timestart)/1000
                        If $dif > 120 Then
                                GUICtrlSetData($Label3,"等待刷序列号超时!")
                                GUICtrlSetData($Input1,"")
                                $start=0
                                Return 1
                        EndIf
                EndIf
        WEnd
        If $SFCS==0 Then
                USNCheck($USN)
        Else
                SFCSCheck($USN)
        EndIf
        $start=0
;~         GUICtrlSetData($Label3,"等待测试开始...")
EndFunc        ;==>check

Func _KeyProc($nCode, $wParam, $lParam)
        Local $tKEYHOOKS
        $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
        If $nCode < 0 Then
                Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
        EndIf
        If $wParam = $WM_KEYDOWN Then
                If DllStructGetData($tKEYHOOKS, "vkCode") <> 221 Then
                        $buffer=$buffer & Chr(DllStructGetData($tKEYHOOKS, "vkCode"))
                Else
                        $end=1
                EndIf
        Else
                Local $flags = DllStructGetData($tKEYHOOKS, "flags")
                Switch $flags
                        Case $LLKHF_ALTDOWN
                                If DllStructGetData($tKEYHOOKS, "vkCode") <> 221 Then
                                        $buffer=$buffer & Chr(DllStructGetData($tKEYHOOKS, "vkCode"))
                                Else
                                        $end=1
                                EndIf
                EndSwitch
        EndIf
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func CheckUSN($str)
        Local $115PN,$MBPN,$i
        $MBPN=IniReadSection("Voltage.ini","MBPN")
        For $i=1 To $MBPN[0][0]
                If StringInStr($str,$MBPN[$i][1])>0 Then
                        Return 0
                EndIf
        Next
        Return 1
EndFunc

Func USNCheck($string1)
        Local $115PN,$i,$voltage=230,$sound,$AlarmSound
        $115PN=IniReadSection("Voltage.ini","115PN")
        For $i=1 To $115PN[0][0]
                If StringInStr($string1,$115PN[$i][1])>0 Then
                        $voltage=115
                        ExitLoop
                EndIf
        Next
        GUICtrlSetData($Label6,$buffer &" V")
        GUICtrlSetData($Label7,$voltage&" V")
;~         MsgBox(0,"Result","$buffer="&$buffer&"  "&"$voltage="&$voltage)
        If $buffer == $voltage Then
                GUICtrlSetData($Label3,"测试完成:电源电压比对PASS!")
                GUICtrlSetData($Label9,@CRLF&"                 电源电压比对PASS")
                GUICtrlSetBkColor(-1, 0x008000)
        Else
                GUICtrlSetData($Label3,"测试完成:电源电压比对FAIL!")
                GUICtrlSetData($Label9,@CRLF&"                 电源电压比对FAIL")
                GUICtrlSetBkColor(-1, 0xFF0000)        
                $AlarmSound=IniRead("Voltage.ini","setting","AlarmSound","alarm.wav")
                $sound = _SoundOpen($AlarmSound)
                _SoundPlay($sound,1)
                _SoundStop($sound)
        EndIf
EndFunc

Func SFCSCheck($string2)
        
EndFunc

        
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-21 03:27 , Processed in 0.071048 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表