Jump to content
Sign in to follow this  
ycomp

wait for any key press?

Recommended Posts

ycomp

Hi, how can I have autoit wait for any keypress before continuing? e.g. to prevent script from exiting until key has been pressed... like pause in a batch file.

I was looking for something like a getKey() function but I couldn't find it in the help.. must be doing something wrong when I was searching... how do I get the current key?

Share this post


Link to post
Share on other sites
Drew

HotKeySet

Look that up in the helpfile.

EDIT: Here's the Example the HelpFile offers:

; Press Esc to terminate script, Pause/Break to "pause"

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ;Shift-Alt-d

;;;; Body of program would go here;;;;
While 1
    Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc

Func ShowMessage()
    MsgBox(4096,"","This is a message.")
EndFunc
Edited by Drew

Share this post


Link to post
Share on other sites
ChrisL

This is something piccaso helped me out with a while ago.

This code is mainly his I've just modified it for your needs.

Global Const $WH_KEYBOARD_LL = 13
Global Const $HC_ACTION = 0

ToolTip("When your ready press a key")

$pllkb = DllCallbackRegister ("_llkb", "lparam", "int;wparam;lparam")
$hModule = DllCall("kernel32.dll", "ptr", "GetModuleHandle", "ptr", 0)
$hModule = $hModule[0]
$hHkk = DllCall("user32.dll", "ptr", "SetWindowsHookEx", "int", $WH_KEYBOARD_LL, "ptr", DllCallbackGetPtr($pllkb), "ptr", $hModule, "dword", 0)
$hHkk = $hHkk[0]

$AKeyWasPressed = 0

While 1
    Sleep(100)
    If $AKeyWasPressed = 1 then Exitloop
WEnd
    ToolTip("")
    
Msgbox(0,"","A key was pressed")


Func _llkb($nCode, $wParam, $lParam)
    If $nCode = $HC_ACTION Then $AKeyWasPressed = 1
    Return _CallNextHookEx($hHkk, $nCode, $wParam, $lParam)
EndFunc ;==>_llkb

Func _CallNextHookEx($hhk, $nCode, $wParam, $lParam)
    Local $aTmp = DllCall("user32.dll", "lparam", "CallNextHookEx", "ptr", $hhk, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
    Return $aTmp[0]
EndFunc ;==>_CallNextHookEx
Edited by ChrisL

Share this post


Link to post
Share on other sites
newbiescripter

You could might also take a look for _IsPressed in the help file..

Regards

EDIT:

#include <misc.au3>
Do
    Sleep(50)
Until _IsPressed("20")

This will pause until spacebar is pressed

Edited by newbiescripter

Share this post


Link to post
Share on other sites
zorphnog

If you're using a GUI you can do this:

#include <WindowsConstants.au3>
#include <GUIConstants.au3>
GUICreate("Test", 300, 200)
GUIRegisterMsg($WM_KEYDOWN, "WM_KEYDOWN")
GUISetState()
$iCount = 0
Do
    $nMsg = GUIGetMsg()
Until $nMsg = $GUI_EVENT_CLOSE

;== WM_KEYDOWN ==================================
Func WM_KEYDOWN($hWnd, $iMsg, $iwParam, $ilParam)
    ;$iwParam = virtual-key code
    ;$ilParam = Specifies the repeat count, scan code, extended-key flag, context code, 
    ;           previous key-state flag, and transition-state flag
    ConsoleWrite(StringFormat("->[%03d]WM_KEYDOWN Received (%s, %s)\n", $iCount, $iwParam, $ilParam))
    $iCount += 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_KEYDOWN
Edited by zorphnog

Share this post


Link to post
Share on other sites
DaRam

Or this, for any Key Down or Mouse Click:

While 1
  If Check4KbdOrMousePress() Then ExitLoop
WEnd

Func Check4KbdOrMousePress()
Local $i
Local $Keycodes = StringSplit("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|43|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" & _
    "|90|91|A0|A1|A2|A3|A4|A5", "|")
    For $i in $Keycodes
        If _IsPressed(String($i)) Then Return 1
    Next
    Return 0
EndFunc

Share this post


Link to post
Share on other sites
amokoura

Or this, for any Key Down or Mouse Click:

While 1
  If Check4KbdOrMousePress() Then ExitLoop
WEnd

Func Check4KbdOrMousePress()
Local $i
Local $Keycodes = StringSplit("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|43|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" & _
    "|90|91|A0|A1|A2|A3|A4|A5", "|")
    For $i in $Keycodes
        If _IsPressed(String($i)) Then Return 1
    Next
    Return 0
EndFunc
I'm no guru but that seems quite expensive, doesn't it?

Share this post


Link to post
Share on other sites
DaRam

I'm no guru but that seems quite expensive, doesn't it?

Probably, this is code from an About (Splash) screen with no close controlbox or button, so I was not bothered by the overhead.

Should it come with a warning ? :)

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  

×