killgore Posted August 6, 2013 Posted August 6, 2013 Hi! I'm trying to adapt piece of code found here to set time limit for user to enter something in command prompt. I wrote something like this: Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = "" Local $hFile = _WinAPI_CreateFile("CON", 2, 2) $start = TimerInit() While TimerDiff($start) < 10000 _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead) If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1) WEnd But it doesn't work. Command prompt waits endlessly for input. I've tried changing "TimerDiff($start) < 10000 " to "if TimerDiff($start) > 10000 Then ExitLoop " but it seems like condition is not even evaluated. Am I missing something?
Moderators JLogan3o13 Posted August 6, 2013 Moderators Posted August 6, 2013 You need to do some debugging, to see where it is failing (i.e. does it recognize the TimerDiff or is it failing somewhere else). Try modifying your While statement and include a msgbox or ConsoleWrite, like this: $start = TimerInit() While 1 If TimerDiff($start) > 10000 Then MsgBox(0, TimerDiff($start), "Time's Up!") Sleep(500) WEnd "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
killgore Posted August 6, 2013 Author Posted August 6, 2013 I've tried. If I remove _WinAPI_ReadFile related code timer works, but with this three lines even if I insert some ConsoleWrite or FileWrite (eg. ConsoleWrite(Int(TimerDiff($start))) ) in this loop it looks like this piece of code is omitted. I don't know exactly what's going on in these three lines so maybe the problem is it's working completely different than I think it is.
FireFox Posted August 6, 2013 Posted August 6, 2013 Hi, Are you using the $start variable elsewhere? Br, FireFox.
killgore Posted August 6, 2013 Author Posted August 6, 2013 No, and I think it's not the point. It looks like condition is not evaluated. I've made an experiment and removed while loop completely, so only these 2 lines left. _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead) If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1) Even without While command prompt waits for user input. So it seems that _WinAPI_ReadFile stops execution.
FireFox Posted August 6, 2013 Posted August 6, 2013 Does the CON file exist?Have you tried in administrator mode? (#RequireAdmin function)Br, FireFox.
killgore Posted August 6, 2013 Author Posted August 6, 2013 (edited) Look, "con" file is a special file. Just take a look at . This code works. I was trying to modify it to set time limit for a user. It seems that I misunderstood how it works. I thought that while loop is running all the time capturing keystrokes. But using _WinAPI_ReadFile without while also waits for ENTER, so it seems I can't achieve what I want with this code. So, I will rephrase my question: Is there a way in autoit to read user input from command prompt with some kind if time limit? Edited August 6, 2013 by killgore
FireFox Posted August 6, 2013 Posted August 6, 2013 That's maybe why I don't understand what the command prompt does here.
oo0oo Posted July 5, 2014 Posted July 5, 2014 killgore i also have same problem... Func _ConsoleInput($sPrompt) Local $Timer = TimerInit() ;Wait time If Not @Compiled Then Return SetError(1, 0, 0) ; Not compiled ConsoleWrite($sPrompt) Local $tBuffer = DllStructCreate("char"), $nRead, $sRet = "" Local $hFile = _WinAPI_CreateFile("CON", 2, 2) While 1 If TimerDiff($Timer)/1000 > 3 Then Msgbox(0,TimerDiff($Timer)/1000,"") _WinAPI_CloseHandle($hFile) Return "y" ExitLoop EndIf _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead) If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1) Sleep(25) WEnd _WinAPI_CloseHandle($hFile) Return $sRet EndFunc
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now