Jump to content
Sign in to follow this  
JScript

UDF: _InKey.au3

Recommended Posts

JScript

Hello,

Pauses the script until any key has been pressed or mouse activity.

_InKey(5)

Need say more?

#include-once
#Include <Timers.au3>

; #INDEX# ==========================================================================================================================================================
; Title .........: _InKey
; AutoIt Version : 3.2.10++
; Language ......: English
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; Author ........: João Carlos (jscript)
;==================================================================================================================================================================

; #CURRENT# ========================================================================================================================================================
;_InKey
; ==================================================================================================================================================================

; #INTERNAL_USE_ONLY#===============================================================================================================================================
; ==================================================================================================================================================================

; #FUNCTION# =======================================================================================================================================================
; Name...........: _InKey
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; Syntax.........: InKey( TimeOut )
; Parameters ....: TimeOut  - Timeout in seconds. After the timeout has elapsed the script will be continue.
; Requirement(s).: #Include <Timers.au3>
; Return values .: Success - 1
;                  Failure - 0 If TimeOut
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......: Sleep and _IsPressed
; Link ..........:
; Example .......: _Inkey(5); Pauses script five seconds if no mouse activity or any key pressed...
; ==================================================================================================================================================================
Func _InKey($nTimeOut)
    Local $nTimer
    
    $nTimeOut *= 1000
    
    $nTimer = TimerInit()
    While _Timer_GetIdleTime() > 0
        If TimerDiff($nTimer) > $nTimeOut Then Return 0 ; Timed out
        Sleep(10)
    WEnd
    Return 1
EndFunc   ;==>_InKey

EDIT: Add $nTimeOut = $nTimeOut * 1000 by colafrysen.

Correct:

$nTimeOut *= 1000
by Xenobiologist.

Or try my old UDF: _InKey( [ TimeOut = -1 [, MouseCheck = False ]] ))

#include-once
#Include <Misc.au3>

; #INDEX# ==========================================================================================================================================================
; Title .........: _InKey
; AutoIt Version : 3.2.10++
; Language ......: English
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; ==================================================================================================================================================================

; #CURRENT# ========================================================================================================================================================
;_InKey
; ==================================================================================================================================================================

; #INTERNAL_USE_ONLY#===============================================================================================================================================
;
; ==================================================================================================================================================================


; #FUNCTION# =======================================================================================================================================================
; Name...........: _InKey
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; Syntax.........: _InKey( [ TimeOut = -1 [, MouseCheck = False ]] )
; Parameters ....: TimeOut      - [Optional] Timeout in seconds. After the timeout has elapsed the script will be continue.
;                                   The default is -1 (waits indefinitely for a keystroke).
;                  MouseCheck   - [Optional] If True, check mouse activity, default is False
; Requirement(s).:
; Return values .: Success - 1
;                  Failure - 0 If TimeOut
;                           -1 If error and set @error to 1
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......: Sleep and _IsPressed
; Link ..........:
; Example .......: _Inkey(5); Pauses script five seconds if no mouse activity or any key pressed...
; ==================================================================================================================================================================
Func _InKey($nTimeOut = -1, $bMouseCheck = False)
    Local $skeys[117] = [116, "01", "02", "04", "05", "06", "08", "09", "0C", "0D", "10", "11", "12", "13", "14", "1B", "20", "21", _
            "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "30", "31", "32", "33", "34", "35", "36", _
            "37", "38", "39", "41", "42", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", _
            "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", _
            "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", _
            "7E", "7F", "80H", "81H", "82H", "83H", "84H", "85H", "86H", "87H", "90", "91", "A0", "A1", "A2", "A3", "A4", "A5"]
    Local $nTimer = TimerInit()
    If $bMouseCheck Then Local $nOldPos[2], $nCheckPos1, $nCheckPos2, $aMousePos = MouseGetPos(), $aMousePos2
    If $nTimeOut = -1 Then
        $nTimeOut = 2147483647
    Else
        $nTimeOut *= 1000
    EndIf
    
    While @error = 0
        If TimerDiff($nTimer) > $nTimeOut Then Return 0 ; Timed out
        
        For $i = 1 To $skeys[0]
            If _IsPressed($skeys[$i]) Then Return 1 ;_IsPressed ?
        Next
        
        If $bMouseCheck Then
            $nOldPos[0] = $aMousePos[0]
            $nOldPos[1] = $aMousePos[1]
            $aMousePos2 = MouseGetPos()
            $nCheckPos1 = $nOldPos[0] + $nOldPos[1]
            $nCheckPos2 = $aMousePos2[0] + $aMousePos2[1]
            
            If $nCheckPos1 <> $nCheckPos2 Then Return 1
        EndIf
        Sleep(50)
    WEnd
    Return SetError(1, 0, -1)
EndFunc   ;==>_InKey
Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
colafrysen

I really suggest changing this line:

$nTimeOut = $nTimeOut & "000"

To

$nTimeOut = $nTimeOut * 1000

Otherwise when you write for example 1.5 seconds it will be 1.5000 Milliseconds


[font="Impact"]Use the helpfile, It´s one of the best exlusive features of Autoit.[/font]http://support.microsoft.com/kb/q555375ALIBI Run - a replacement for the windows run promptPC Controller - an application for controlling other PCs[size="1"]Science flies us to the moon. Religion flies us into buildings.[/size][size="1"]http://bit.ly/cAMPZV[/size]

Share this post


Link to post
Share on other sites
JScript

I really suggest changing this line:

$nTimeOut = $nTimeOut & "000"

To

$nTimeOut = $nTimeOut * 1000

Otherwise when you write for example 1.5 seconds it will be 1.5000 Milliseconds

I agree with you... Thank's!

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
Xenobiologist

you could better write $nTimeOut *= 1000. >_


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
JScript

Or try my old _InKey( [ TimeOut = -1 [, MouseCheck = False ]] )) UDF:

#include-once
#Include <Misc.au3>

; #INDEX# ==========================================================================================================================================================
; Title .........: _InKey
; AutoIt Version : 3.2.10++
; Language ......: English
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; ==================================================================================================================================================================

; #CURRENT# ========================================================================================================================================================
;_InKey
; ==================================================================================================================================================================

; #INTERNAL_USE_ONLY#===============================================================================================================================================
;
; ==================================================================================================================================================================


; #FUNCTION# =======================================================================================================================================================
; Name...........: _InKey
; Description ...: Pauses the script until any key has been pressed or mouse activity.
; Syntax.........: _InKey( [ TimeOut = -1 [, MouseCheck = False ]] )
; Parameters ....: TimeOut      - [Optional] Timeout in seconds. After the timeout has elapsed the script will be continue.
;                                   The default is -1 (waits indefinitely for a keystroke).
;                  MouseCheck   - [Optional] If True, check mouse activity, default is False
; Requirement(s).:
; Return values .: Success - 1
;                  Failure - 0 If TimeOut
;                           -1 If error and set @error to 1
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......: Sleep and _IsPressed
; Link ..........:
; Example .......: _Inkey(5); Pauses script five seconds if no mouse activity or any key pressed...
; ==================================================================================================================================================================
Func _InKey($nTimeOut = -1, $bMouseCheck = False)
    Local $skeys[117] = [116, "01", "02", "04", "05", "06", "08", "09", "0C", "0D", "10", "11", "12", "13", "14", "1B", "20", "21", _
            "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "30", "31", "32", "33", "34", "35", "36", _
            "37", "38", "39", "41", "42", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", _
            "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", _
            "6A", "6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", _
            "7E", "7F", "80H", "81H", "82H", "83H", "84H", "85H", "86H", "87H", "90", "91", "A0", "A1", "A2", "A3", "A4", "A5"]
    Local $nTimer = TimerInit()
    If $bMouseCheck Then Local $nOldPos[2], $nCheckPos1, $nCheckPos2, $aMousePos = MouseGetPos(), $aMousePos2
    If $nTimeOut = -1 Then
        $nTimeOut = 2147483647
    Else
        $nTimeOut *= 1000
    EndIf
    
    While @error = 0
        If TimerDiff($nTimer) > $nTimeOut Then Return 0 ; Timed out
        
        For $i = 1 To $skeys[0]
            If _IsPressed($skeys[$i]) Then Return 1 ;_IsPressed ?
        Next
        
        If $bMouseCheck Then
            $nOldPos[0] = $aMousePos[0]
            $nOldPos[1] = $aMousePos[1]
            $aMousePos2 = MouseGetPos()
            $nCheckPos1 = $nOldPos[0] + $nOldPos[1]
            $nCheckPos2 = $aMousePos2[0] + $aMousePos2[1]
            
            If $nCheckPos1 <> $nCheckPos2 Then Return 1
        EndIf
        Sleep(50)
    WEnd
    Return SetError(1, 0, -1)
EndFunc   ;==>_InKey

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×