I am using this AutoIt code to send text to Notepad on the press of a button:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
Example(GUICtrlRead($Input))
EndSwitch
WEnd
Func Example($text)
Run("notepad.exe")
Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)
ControlSend($hWnd, "", "Edit1", $text)
EndFunc
It is working great. But now I want to send keystrokes as soon as I press them. Is there something like OnKeyDown in AutoIt? So I don't have to press the send button to send it to Notepad every time I type a character.
This is how I would do it.
Work pretty neat!
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Run("notepad.exe")
Global $hWnd = WinWait("[CLASS:Notepad]", "", 10)
$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(#SW_SHOW)
$OldText = ""
While 1
$nMsg = GUIGetMsg()
$NewText = GUICtrlRead($Input)
If $OldText <> $NewText Then
$OldText = $NewText
Example($NewText)
EndIf
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
Example(GUICtrlRead($Input))
EndSwitch
WEnd
Func Example($text)
ControlSetText($hWnd, "", "Edit1", $text)
EndFunc
Related
How can i get the Aero Glass effect for my Autoit GUI?
I am playing a bit with AutoIt to increase my knowledge about GUI stuff. Usually i just create scripts without the usage of a GUI, but i would like to have a nice looking areo glass GUI when i just start to work with.
I already experiment with WinSetTrans but this is not exactly what i want. It should look more like the image below.
My current code is:
#include <GUIConstants.au3>
$iWidthGui = 450
$iHeightGui = 300
$hGui = GUICreate("Glass GUI", $iWidthGui, $iHeightGui, -1, -1, -1, $WS_EX_TOPMOST)
$cExit = GUICtrlCreateButton("Exit", $iWidthGui / 2 - 50, $iHeightGui / 2 - 15, 100, 30)
GUISetState( #SW_SHOW )
WinSetTrans($hGui, "", 180)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $cExit
GUIDelete($hGui)
ExitLoop
EndSwitch
WEnd
Is it possible with Autoit? How can i do that?
Yes it is possible. This should work at least for Windows 7. I couldn't test the script on a Windows 10 machine.
Improved code:
#include-once
#include <GUIConstants.au3>
Global $iWidthGui = 450
Global $iHeightGui = 300
Global $hGui = GUICreate("Glass GUI", $iWidthGui, $iHeightGui, -1, -1, -1, $WS_EX_TOPMOST)
Global $cExit = GUICtrlCreateButton("Exit", $iWidthGui / 2 - 50, $iHeightGui / 2 - 15, 100, 30)
GUISetState( #SW_SHOW, $hGui )
Func _aeroGlassEffect( $hWnd, $iLeft = #DesktopWidth, $iRight = #DesktopWidth, $iTop = #DesktopWidth, $iBottom = #DesktopWidth )
$hStruct = DllStructCreate( 'int left; int right; int height; int bottom;' )
DllStructSetData( $hStruct, 'left', $iLeft )
DllStructSetData( $hStruct, 'right', $iRight )
DllStructSetData( $hStruct, 'height', $iTop )
DllStructSetData( $hStruct, 'bottom', $iBottom )
GUISetBkColor( '0x000000' )
Return DllCall( 'dwmapi.dll', 'int', 'DwmExtendFrameIntoClientArea', 'hWnd', $hWnd, 'ptr', DllStructGetPtr( $hStruct ) )
EndFunc
_aeroGlassEffect( $hGui )
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $cExit
GUIDelete($hGui)
ExitLoop
EndSwitch
WEnd
I switched WinSetTrans() for _aeroGlassEffect(). You can change the function parameters $iLeft, $iRight, $iTop, $iBottom.
I used simplespy.au3 to target a slider control in the Windows settings window using _UIA_action(). Asking the user to input a value I send left and right keys for the slider to move.
But I don't know how to get the current value of my slider. I tried GUICtrlRead() but it doesn't work. How do I get the current value of a slider control?
My code:
#RequireAdmin
#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <GuiSlider.au3>
#include <GUIConstants.au3>
#include "UISpy\UIAWrappers\UIAWrappers.au3"
Slider()
Func Slider()
;User Input
$Default = ""
$input = ""
While 1
$input = InputBox("Brightness", "Set Brightness to:", "", " M", -1, -1)
If #error Then ExitLoop
If $input < 0 Or $input > 100 Then
MsgBox(48, "Error!", "Minimum value for brightness is 0 and the Maximum brightness is 100")
$Default = $input
ContinueLoop
ElseIf StringLen($input) > 0 Or $input < 100 Then
ExitLoop
EndIf
If #error = 1 Then
Exit
EndIf
WEnd
;Start automation
Local $iTimeout = 1
Local $hWnd = WinWait("[Class:Shell_TrayWnd]", "", "[CLASS:Button; INSTANCE:1]")
ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]") ;open the start menu
Send("Display Settings", 10) ;Type the Display Settings in the start menu
Sleep(1000)
Send("{ENTER}")
AutoItSetOption("MustDeclareVars", 1)
Local $oP2 = _UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Settings;controltype:=UIA_WindowControlTypeId;class:=ApplicationFrameWindow", $treescope_children)
_UIA_Action($oP2, "setfocus")
Local $oP1 = _UIA_getObjectByFindAll($oP2, "Title:=Settings;controltype:=UIA_WindowControlTypeId;class:=Windows.UI.Core.CoreWindow", $treescope_children)
_UIA_Action($oP1, "setfocus")
Local $oP0 = _UIA_getObjectByFindAll($oP1, "Title:=;controltype:=UIA_PaneControlTypeId;class:=ScrollViewer", $treescope_children)
;First find the object in the parent before you can do something
; $oUIElement=_UIA_getObjectByFindAll("Changebrightness.mainwindow", "title:=Change brightness;ControlType:=UIA_SliderControlTypeId", $treescope_subtree)
Local $oUIElement = _UIA_getObjectByFindAll($oP0, "title:=Change brightness;ControlType:=UIA_SliderControlTypeId", $treescope_subtree)
_UIA_action($oUIElement, "setfocus")
Send("{LEFT $input}"`enter code here`)
Sleep(1000)
Local $value = GUICtrlRead($oUIElement)
MsgBox($MB_SYSTEMMODAL, "", "Brightness is at: " & $value, $iTimeout)
Local $setValue = GUICtrlSetData($oUIElement, 50)
;UI for verification
MsgBox(0, "Change Brightness", "Brightness Changed.", $iTimeout)
Sleep(1000)
WinClose("Settings") ;Close the active window
EndFunc ;==>Slider
Don't manipulate the slider. Use the WinAPI instead: _WinAPI_SetDeviceGammaRamp.
I am working on a program that pulls data from AD via a linked table in SQL and lets the user copy an email address to the clipboard. I am using an array to dynamically display a button beside each row. The problem is that, when I try and put labels or buttons inside the for loop, they don't show up. Is it just that I'm doing it wrong. My code is as follows:
#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
global $title = "E-Mail address lookup"
global $sqlCon = _MSSQL_Con("server", "user", "Directory3=", "password")
global $name = InputBox($title,"Please type the name of the person you wish to find")
global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
if StringLen(StringStripWS($name,3)) < 1 then
MsgBox(0, $title, "Name cannot be empty")
Else
Global $ControlID = GUICreate($title, 530, 500)
GUISetState(#SW_SHOW)
Local $iOldOpt = Opt("GUICoordMode", 2)
GUICtrlCreateLabel(" ", 0, 0, 80)
GUICtrlCreateLabel("E-Mail Address", 20, -1, 100)
GUICtrlCreateLabel("Name", 20, -1, 50)
GUICtrlCreateLabel("Department", 20, -1, 100)
GUICtrlCreateLabel("Telephone Number", 20, -1, 200)
for $i = 1 To UBound($result) Step 1
GUICtrlCreateButton("Copy", 0, $i, 30, 20)
Next
GUISetState()
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
EndIf
I would have expected one button to show up on a new line on every iteration of the loop
I recommend to use another mode:
#include <GUIConstantsEx.au3>
;~ #include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
Global $title = "E-Mail address lookup"
;~ global $sqlCon = _MSSQL_Con("server", "user", "Directory3=", "password")
;~ global $name = InputBox($title,"Please type the name of the person you wish to find")
;~ global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
Global $result = StringSplit('1,2,3,4,5,6,7,8', ',')
;~ if StringLen(StringStripWS($name,3)) < 1 then
;~ MsgBox(0, $title, "Name cannot be empty")
;~ Else
;~ _ArrayDisplay($result)
Global $ControlID = GUICreate($title, 530, 500)
;~ Local $iOldOpt = Opt("GUICoordMode", 2)
GUICtrlCreateLabel(" ", 0, 0, 80)
GUICtrlCreateLabel("E-Mail Address", 20, -1, 100)
GUICtrlCreateLabel("Name", 20, -1, 50)
GUICtrlCreateLabel("Department", 20, -1, 100)
GUICtrlCreateLabel("Telephone Number", 20, -1, 200)
For $i = 1 To UBound($result) - 1
GUICtrlCreateButton("Copy", 20, $i * 20, 350, 20)
Next
GUISetState()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
I don't know if it's possible cuz i couldn't find anywhere.
I have created a GUI with some buttons to start things.
I'm just wondering if it is possible to:
limit the number of openings of the GUI
limit the number of executions with the buttons
limit the time range so that after certain point, u can't use it
Global $explain = "help~~"
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $explain = "hmmm"
Global $Form1 = GUICreate("Yay", 328, 157)
Global $Label1 = GUICtrlCreateLabel("ID", 12, 14, 67, 20)
Global $Label2 = GUICtrlCreateLabel("Password", 12, 44, 67, 20)
Global $Label3 = GUICtrlCreateLabel("hello world", 225, 14, 90, 20)
Global $Input1 = GUICtrlCreateInput("", 76, 10, 105, 24)
Global $Input2 = GUICtrlCreateInput("", 76, 40, 105, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
Global $Input3 = GUICtrlCreateInput("", 228, 40, 70, 24)
Global $Button1 = GUICtrlCreateButton("Log In", 76, 69, 105, 30)
Global $Button2 = GUICtrlCreateButton("Connect", 212, 69, 100, 30)
Global $Checkbox1 = GUICtrlCreateCheckbox("hmm", 240, 113, 97, 17)
Global $Checkbox2 = GUICtrlCreateCheckbox("hmm2", 240, 134, 97, 17)
Global $Group1 = GUICtrlCreateGroup("", 5, -5, 190, 110)
Global $Edit1 = GUICtrlCreateEdit("", 5, 110, 228, 100)
GUICtrlSetData(-1, $explain)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
If (GUICtrlRead($Checkbox1) = $GUI_CHECKED) Then
Global $hmm = 1
EndIf
Case $Checkbox2
If (GUICtrlRead($Checkbox2) = $GUI_CHECKED) Then
Global $hmm2 = 0
EndIf
Case $Button1
Global $id = GUICtrlRead($Input1)
Global $pass = GUICtrlRead($Input2)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","possible to limit #of Execution?")
Case $Button2
Global $exnum = GUICtrlRead($Input3)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","time limit would be nice too! thnx!")
EndSwitch
WEnd
Has anyone tried this?
Will it require intense coding?
Could you provide a sample if it isn't too bad
Good afternoon Pita,
Yup! It's all definitely possible to do! There's multiple ways to do so, I'll try to name a few.
A good way for you to manage these requests, would be to make a properties file to manage everything. Take a look at my example below!
Global $explain = "help~~"
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; =================================================================================================================================================
#include <File.au3>
#Include <Date.au3>
; This is the directory that the temporary file will be stored.
$fileHandle = #AppDataDir & "\TestProgram\properties.txt"
; Checks to see if the properties file exists
if FileExists($fileHandle) Then
; START Check Run Limit
; Reads the first value in the properties file. In this example, it's the limit to how many times the program can be launched.
$runLimit = FileReadLine($fileHandle, 1)
; MsgBox(0,"Run Limit", $runLimit)
; If the run limit reaches zero (or less than by some glitch), do not launch the program.
if $runLimit <= 0 Then
MsgBox(16,"Run Limit Reached", "You have reached the maximum number of launches for this program!")
Exit
EndIf
; Subtract one from the launch limit.
$newLimit = $runLimit - 1
MsgBox(0,"New Run Limit", "You may launch this program " & $newLimit & " more times!", 3)
; Update the properties file.
_FileWriteToLine($fileHandle, 1, $newLimit, True)
; END Check Run Limit
; Start CHECK Expiration Date
$expDate = FileReadLine($fileHandle, 2)
; MsgBox(0,"Expiration Date", "This program expires on " & $expDate)
; Check to see if the expiration date has already passed
if $expDate < _NowDate() Then
MsgBox(16,"Program Expired","Your trial period has expired!")
Exit
EndIf
; END Check expiration date
Else
; If the file does not exists, create it and set it up
$propFile = FileOpen($fileHandle, 10)
; Sets the launch limit to 10. Change this value to set the launch limit
FileWrite($fileHandle, "10" & #CRLF)
; Sets the expiration date to a week (7 days) from the initial launch date.
FileWrite($fileHandle, #MON & "/" & (#MDAY + 7) & "/" & #YEAR & #CRLF)
; Sets the button limit for the login button to 3.
FileWrite($fileHandle, "3" & #CRLF)
FileClose($fileHandle)
#CS
NOTE: THIS IS JUST FOR DEMONSTRATION PURPOSES! THE USER CAN SIMPLY DELETE THE PROPERTIES FILE
IN ORDER TO RESET THE VALUES AND CONTINUE USING THE PROGRAM. THIS WAS DESIGNED SIMPLY TO
GET YOU ON THE RIGHT TRACK.
#CE
EndIf
; =================================================================================================================================================
Global $explain = "hmmm"
Global $Form1 = GUICreate("Yay", 328, 157)
Global $Label1 = GUICtrlCreateLabel("ID", 12, 14, 67, 20)
Global $Label2 = GUICtrlCreateLabel("Password", 12, 44, 67, 20)
Global $Label3 = GUICtrlCreateLabel("hello world", 225, 14, 90, 20)
Global $Input1 = GUICtrlCreateInput("", 76, 10, 105, 24)
Global $Input2 = GUICtrlCreateInput("", 76, 40, 105, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
Global $Input3 = GUICtrlCreateInput("", 228, 40, 70, 24)
Global $Button1 = GUICtrlCreateButton("Log In", 76, 69, 105, 30)
Global $Button2 = GUICtrlCreateButton("Connect", 212, 69, 100, 30)
Global $Checkbox1 = GUICtrlCreateCheckbox("hmm", 240, 113, 97, 17)
Global $Checkbox2 = GUICtrlCreateCheckbox("hmm2", 240, 134, 97, 17)
Global $Group1 = GUICtrlCreateGroup("", 5, -5, 190, 110)
Global $Edit1 = GUICtrlCreateEdit("", 5, 110, 228, 100)
GUICtrlSetData(-1, $explain)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
If (GUICtrlRead($Checkbox1) = $GUI_CHECKED) Then
Global $hmm = 1
EndIf
Case $Checkbox2
If (GUICtrlRead($Checkbox2) = $GUI_CHECKED) Then
Global $hmm2 = 0
EndIf
Case $Button1
; START Button Limit ================================================================================================================
if FileExists($fileHandle) Then
$buttonLimit = FileReadLine($fileHandle, 3)
if $buttonLimit <= 0 Then
MsgBox(16,"Button Limit Reached", "You have reached the maximum number of times you can hit this button!")
ELSE
$newButtonLimit = $buttonLimit - 1
MsgBox(0,"New Run Limit", "You may press this button " & $newButtonLimit & " more times!", 3)
_FileWriteToLine($fileHandle, 3, $newButtonLimit, True)
; START OLD CODE
Global $id = GUICtrlRead($Input1)
Global $pass = GUICtrlRead($Input2)
; WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","possible to limit #of Execution?")
; END OLD CODE ================================================================================================================
EndIf
EndIf
; END Button Limit
Case $Button2
Global $exnum = GUICtrlRead($Input3)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","time limit would be nice too! thnx!")
EndSwitch
WEnd
Upon launching the program, a property file is generated and stored in the appdata on the users computer.
On windows: Press Win+R and type %appdata%. If you left the example the same, there will be a folder called "TestProgram", if you changed the name, it will be whatever you called it. Inside of this folder, there will be the properties.txt file (Again, unless you changed the name).
I have made some comments in the example code to help explain what I did, but I'm not fantastic at explaining things so please let me know if you need some more help.
P.S. If you do use this method, I strongly suggest encrypting (Using Crypt maybe?) the properties file to make it a little less editable and designing some way to tell if the user deleted the file.
I hope this helps!,
Tim
My AutoIt script pastes strings when I press Ctrl + 1, Ctrl + 2, etc.
Func copyPasta1()
Send($texts[1], 1)
EndFunc
Func copyPasta2()
Send($texts[2], 1)
EndFunc
...
HotKeySet("^" & $keys_arr[2], "copyPasta" & ($i - 1))
Sometimes the Ctrl key gets locked so the whole system behaves as if I had Ctrl pressed continuously. Pressing Ctrl resolves it.
I tried using Shift instead, but then Shift gets locked. Anybody has a solution for this ? My script:
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <FileConstants.au3>
#include <Array.au3>
#include <Math.au3>
HotKeySet("^q", "end")
Func end()
SplashTextOn("", "Programm wird beendet", 200, 40, -1, -1, 1, "", 10, 600)
Sleep(1000)
SplashOff()
Exit
EndFunc
Func copyPasta1()
Send($texts[1], 1)
EndFunc
Func copyPasta2()
Send($texts[2], 1)
EndFunc
Func copyPasta3()
Send($texts[3], 1)
EndFunc
Func copyPasta4()
Send($texts[4], 1)
EndFunc
Func copyPasta5()
Send($texts[5], 1)
EndFunc
Func copyPasta6()
Send($texts[6], 1)
EndFunc
Func copyPasta7()
Send($texts[7], 1)
EndFunc
Func copyPasta8()
Send($texts[8], 1)
EndFunc
Func copyPasta9()
Send($texts[9], 1)
EndFunc
; read config File
Local $hFileOpen = FileOpen(#WorkingDir & "\config.cfg", $FO_READ)
If $hFileOpen = -1 Then
MsgBox(1, "", "Keine config Datei gefunden.")
Exit
EndIf
Global $texts[12]
$sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)
$arr1 = StringSplit($sFileRead, "#TASTE:", 1)
For $i = 2 To _Min($arr1[0], 10)
$arr2 = StringSplit($arr1[$i], "#SATZ:", 1)
If $arr2[0] == 2 Then
$keys_arr = StringSplit($arr2[1], '"', 1)
$texts_arr = StringSplit($arr2[2], '"', 1)
If $keys_arr[0] == 3 And $texts_arr[0] == 3 Then
HotKeySet("^" & $keys_arr[2], "copyPasta" & ($i - 1))
$texts[$i - 1] = $texts_arr[2]
EndIf
EndIf
Next
SplashTextOn("", "Programm gestartet", 200, 40, -1, -1, 1, "", 10, 600)
Sleep(1000)
SplashOff()
While 1
Sleep(1000)
WEnd
Exit
Here is the solution; you just have to add this function, which is from http://www.autoitscript.fr/wiki/FAQ#Pourquoi_la_touche_Ctrl_reste_enfonc.C3.A9e.2C_apr.C3.A8s_que_j.27ai_lanc.C3.A9_mon_script_.3F
#include < Misc.au3 >
;Envoi la chaîne $ss après que les touches Shift Alt et Ctrl sont relachées. Optionnellement, donne une erreur après 1 sec si aucune de ces touches reste enfoncée.
;Nécessite d'inclure misc.au3 dans le script pour la fonction _IsPressed.
Func _SendEx($ss, $warn = "")
Local $iT = TimerInit()
While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
If $warn <> "" And TimerDiff($iT) > 1000 Then
MsgBox(262144, "Avertissement", $warn)
EndIf
Sleep(50)
WEnd
Send($ss)
EndFunc ;==>_SendEx
The comments above can be translated:
Send the $ss string after Shift, Alt and Ctrl keys are released. Optionally return error after 1 second if none of these keys remain pressed.
Requires Misc.au3 for _IsPressed().
And use _SendEx("Your text") instead of Send("Your text").