mdelatorre icon

Sends the ENTER key to a given window w/Autoit

mdelatorre | PRO | 12/08/24 04:18:58 AM UTC (Edited) | 0 ⭐ | 14181 👁️ | Never ⏰ | [windows, macro, AutoIT]
AutoIt |

2.25 KB

|

Source Code

|

0 👍

/

0 👎

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; AutoIt Version: 3.0
; Language:       English
; Platform:       Windows 2019/10/2016/8/2012/7/2008
; Author:         Core Technologies Consulting, LLC
;
; Script Function:
;  Sends the ENTER key to a given window
;
; Usage:
;   SendEnterKeyToWindow <window-name> [-d]
; where the optional -d flag specifies to log debugging
; information to C:\\autoit-log.txt.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
Func LogMessage($message, $withPrefix = 1)
    $file = FileOpen("C:\\autoit-log.txt", 1)
 
    ; Check if file opened for writing OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit(1)
    EndIf
 
    if ($withPrefix) Then
        $prefix = '[' & @MON & '/' & @MDAY & '/' & @YEAR & ' ' & @HOUR & ':' & @MIN & ':' & @SEC & '] '
        FileWrite($file, $prefix)
    EndIf
 
    FileWrite($file, $message & @CRLF)
    FileClose($file)
EndFunc
 
; Expect at least 1 argument, the window name
; Exit if less than 1 argument
if $CmdLine[0] < 1 Then
    MsgBox(0x40000, 'SendEnterKeyToWindow', 'Usage:' & @CRLF & @CRLF & 'SendEnterKeyToWindow <window-name> [-d]')
    Exit(1)
EndIf
 
$windowName = $CmdLine[1]
 
; If the 2nd argument is -d, then do logging.
$doLogging = "no"
if $CmdLine[0] >=2 Then
    if $CmdLine[2] = "-d" Then
        $doLogging = "yes"
    Endif
EndIf
 
if ($doLogging == "yes") Then
    LogMessage(@CRLF & '-------------------------------------------------------------------------------', 0)
    LogMessage('SendEnterKeyToWindow: Window name = "' & $windowName & '"')
EndIf
 
; Try to activate the window. May not succeed in Session 0:
; https://www.coretechnologies.com/blog/miscellaneous/running-autoit-session-0/
$result = WinActivate($windowName)
 
; Log the result if necessary
if ($doLogging == "yes") Then
    if ($result == 0) Then
        LogMessage('Window "' & $windowName & '" NOT found by WinActivate!')
    Else
        LogMessage('Window "' & $windowName & '" found by WinActivate.')
    EndIf
EndIf
 
; Send the key press
$result = ControlSend($windowName, "", "", "{ENTER}")
 
; Log the result if necessary
if ($doLogging == "yes") Then
    if ($result == 1) Then
        LogMessage('ControlSend succeeded.')
    Else
        LogMessage('ControlSend failed!')
    EndIf
EndIf
 
ORA:
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎