katie hudson
katie hudson

Reputation: 2893

AutoIT execute application hotkey

I see there are a lot of guides about setting hotkeys with autoit. What I want to do though is execute an applications hotkey.

So for instance, I have this to load firefox

Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)

Now in firefox's menu, I can see that the combination of Ctrl + D will bookmark a page. Is there any way to perform this action once firefox has been loaded, via autoit?

Thanks

Upvotes: 0

Views: 423

Answers (3)

Rofellos
Rofellos

Reputation: 308

With the FireFox window active, just use the Send command.

Send("^d")

Upvotes: 1

Timothy Bomer
Timothy Bomer

Reputation: 510

There are a few ways to go about doing this. I will list a few different methods below.

Method One - Send Keys

; Below is simply the code you listed in the example to open Firefox and wait for it to load.
Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)

; Once FireFox is loaded, and you are at the page you want to bookmark, send Ctrl+D to the page to bookmark it. Since you started the browser Minimized, you will need to activate the page first.

; Activate the window
WinActivate("Mozilla Firefox")

; Send "Ctrl + D"
Send("^d")

Method Two - AutoIt HotKeys

; Create a HotKey controller
HotKeySet("^d", "bookmarkPage")

; Your code is below again.
Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)


; The function that is called when "Ctrl + D" is pressed.

Func bookmarkPage ()
    ; Activate the window
    WinActivate("Mozilla Firefox")

    ; Send they keys
    Send("^d")
EndFunc

Method Three - MouseMove (Not recommended)

; Your code below
Run(@ProgramFilesDir & "\Mozilla Firefox\firefox.exe", "", @SW_MINIMIZE)
Opt("WinTitleMatchMode", 2)
WinWait("Mozilla Firefox")
WinSetState("Mozilla Firefox", "", @SW_MINIMIZE)

; Use the mouse move function to move the cursor to the 'Bookmark' icon.
MouseMove(xxxx,xxxx)
Sleep(100)
MouseClick("left")

I HIGHLY recommend not using the last option. I hope one of these worked for you!

Upvotes: 0

Xenobiologist
Xenobiologist

Reputation: 2151

Just use the Send command. Also have a look at SendKeepActive.

Upvotes: 1

Related Questions