Jump to content

how to know "if any key is pressed"


Recommended Posts

Try this:

$hGui = GUICreate("Press any key to exit...", 400, 100)
GUISetState()
$sKeyboardState = _WinAPI_GetKeyboardState(1)
While _WinAPI_GetKeyboardState(1) = $sKeyboardState
    Sleep(100)
WEnd
GUIDelete($hGui)

Exit

; #FUNCTION# ====================================================================================================================
; Name...........:  _WinAPI_GetKeyboardState
; Description ...:  Returns the status of the 256 virtual keys
; Syntax.........:  _WinAPI_GetKeyboardState($iFlag=0)
; Parameters ....:  $iFlag   - Return Type
;                   0 Returns an array[256]
;                   1 Returns a string
; Return values .:  Success  - Array[256] or String containing status of 256 virtual keys
;                   Failure  - False
; Author ........:  Eukalyptus
; Modified.......:
; Remarks .......:  If the high-order bit is 1, the key is down; otherwise, it is up.
;                   If the key is a toggle key, for example CAPS LOCK, then the low-order bit is 1
;                   when the key is toggled and is 0 if the key is untoggled
; Related .......:  _IsPressed
; Link ..........;
; Example .......;
; ===============================================================================================================================
Func _WinAPI_GetKeyboardState($iFlag = 0)
    Local $aDllRet, $lpKeyState = DllStructCreate("byte[256]")
    $aDllRet = DllCall("User32.dll", "int", "GetKeyboardState", "ptr", DllStructGetPtr($lpKeyState))
    If @error Then Return SetError(@error, 0, 0)
    If $aDllRet[0] = 0 Then
        Return SetError(1, 0, 0)
    Else
        Switch $iFlag
            Case 0
                Local $aReturn[256]
                For $i = 1 To 256
                    $aReturn[$i - 1] = DllStructGetData($lpKeyState, 1, $i)
                Next
                Return $aReturn
            Case Else
                Return DllStructGetData($lpKeyState, 1)
        EndSwitch
    EndIf
EndFunc   ;==>_WinAPI_GetKeyboardState

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

after searching I found another answer.....

#include <WindowsConstants.au3>


GUIRegisterMsg($WM_KEYDOWN, "IsPressed")

$hGui = GUICreate("Press any key within 10s...", 400, 100)
GUISetState()
Sleep(10000)
GUIDelete($hGui)
Exit

Func IsPressed()
    MsgBox(4096, "Test", "Key is pressed!", 2)
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...