Advertisement
mdelatorre

Sends the ENTER key to a given window w/Autoit

Dec 7th, 2024 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.25 KB | Source Code | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;
  3. ; AutoIt Version: 3.0
  4. ; Language:       English
  5. ; Platform:       Windows 2019/10/2016/8/2012/7/2008
  6. ; Author:         Core Technologies Consulting, LLC
  7. ;
  8. ; Script Function:
  9. ;  Sends the ENTER key to a given window
  10. ;
  11. ; Usage:
  12. ;   SendEnterKeyToWindow <window-name> [-d]
  13. ; where the optional -d flag specifies to log debugging
  14. ; information to C:\\autoit-log.txt.
  15. ;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. Func LogMessage($message, $withPrefix = 1)
  19.     $file = FileOpen("C:\\autoit-log.txt", 1)
  20.  
  21.     ; Check if file opened for writing OK
  22.     If $file = -1 Then
  23.         MsgBox(0, "Error", "Unable to open file.")
  24.         Exit(1)
  25.     EndIf
  26.  
  27.     if ($withPrefix) Then
  28.         $prefix = '[' & @MON & '/' & @MDAY & '/' & @YEAR & ' ' & @HOUR & ':' & @MIN & ':' & @SEC & '] '
  29.         FileWrite($file, $prefix)
  30.     EndIf
  31.  
  32.     FileWrite($file, $message & @CRLF)
  33.     FileClose($file)
  34. EndFunc
  35.  
  36. ; Expect at least 1 argument, the window name
  37. ; Exit if less than 1 argument
  38. if $CmdLine[0] < 1 Then
  39.     MsgBox(0x40000, 'SendEnterKeyToWindow', 'Usage:' & @CRLF & @CRLF & 'SendEnterKeyToWindow <window-name> [-d]')
  40.     Exit(1)
  41. EndIf
  42.  
  43. $windowName = $CmdLine[1]
  44.  
  45. ; If the 2nd argument is -d, then do logging.
  46. $doLogging = "no"
  47. if $CmdLine[0] >=2 Then
  48.     if $CmdLine[2] = "-d" Then
  49.         $doLogging = "yes"
  50.     Endif
  51. EndIf
  52.  
  53. if ($doLogging == "yes") Then
  54.     LogMessage(@CRLF & '-------------------------------------------------------------------------------', 0)
  55.     LogMessage('SendEnterKeyToWindow: Window name = "' & $windowName & '"')
  56. EndIf
  57.  
  58. ; Try to activate the window. May not succeed in Session 0:
  59. ; https://www.coretechnologies.com/blog/miscellaneous/running-autoit-session-0/
  60. $result = WinActivate($windowName)
  61.  
  62. ; Log the result if necessary
  63. if ($doLogging == "yes") Then
  64.     if ($result == 0) Then
  65.         LogMessage('Window "' & $windowName & '" NOT found by WinActivate!')
  66.     Else
  67.         LogMessage('Window "' & $windowName & '" found by WinActivate.')
  68.     EndIf
  69. EndIf
  70.  
  71. ; Send the key press
  72. $result = ControlSend($windowName, "", "", "{ENTER}")
  73.  
  74. ; Log the result if necessary
  75. if ($doLogging == "yes") Then
  76.     if ($result == 1) Then
  77.         LogMessage('ControlSend succeeded.')
  78.     Else
  79.         LogMessage('ControlSend failed!')
  80.     EndIf
  81. EndIf
  82.  
  83. ORA:
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement