AHK with command ^!ENTER::Send {Media_Play_Pause} doesn't work in Windows 11 - autohotkey

I want to be able to play and stop a Youtube Video using Auto Hot Key while I am using the Vs Code editor, without the need to stop and play the video with the mouse. That will save some time.Any help would be appreciated.
I can rewind back and forth, but I want to be able to play/pause it as well.
^!ENTER::Send {Media_Play_Pause} ( Only this command does Not work )
So far I have this script:
#SingleInstance, Force
SetTitleMatchMode, 2
SetBatchLines, -1
SetWinDelay, -1
^!ENTER::Send {Media_Play_Pause} ; ( Only this command does Not work )
^!Left::
WinGet, Current_Window, ID, A
WinActivate, - YouTube
ControlGetPos, conX, conY, , , ChromeRenderWidgetHostHWD1
ControlClick,, A,,,, % " X" (conX+1) " Y" (conY+1)
ControlSend, , {Left 3}, A
WinActivate, % "AHK_ID" Current_Window
return
^!Right::
WinGet, Current_Window, ID, A
WinActivate, - YouTube
ControlGetPos, conX, conY, , , ChromeRenderWidgetHostHWD1
ControlClick,, A,,,, % " X" (conX+1) " Y" (conY+1)
ControlSend, , {Right 3}, A
WinActivate, % "AHK_ID" Current_Window
return

Related

ControlSend not sending to the non-topmost window

Is it possible to make ControlSend send / click to a background window?
This code finds a window by id / handler, but sends a click / keystroke only if the window is the topmost. ControlSend skips its action if the window is not topmost. ControlClick force brings window to top and then clicks.
#Singleinstance
DetectHiddenWindows, On
#SingleInstance Force
#MaxThreadsPerHotkey, 2
SendMode Input ; Recommended for new scripts
SetControlDelay -1
PostClick(x,y,win="A") {
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , %win% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , %win% ;WM_LBUTTONUP
}
RunAsAdmin() {
Loop, %0% {
param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
params .= A_Space . param
}
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
{
If A_IsCompiled
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
Else
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
ExitApp
}
}
RunAsAdmin()
F1::
Loop
{
; http://particle-clicker.web.cern.ch/particle-clicker/
WinGet, WinID, ID, Particle Clicker - Google Chrome
; Send keystrokes only if tab active and topmost, suspend if focus lost, continue when gain focue
; ControlSend, ahk_parent, {Space}, ahk_id %WinID%
; send clicks only if tab not mininised, if tab on background - force bring it to front
; ControlClick, x799 y449, ahk_id %WinID%
; same as above
; ControlClick, x799 y449, ahk_id %WinID%,,,,NA
; same as above
; Controlclick x799 y449,ahk_id %WinID%,,Left,1,NA
; same as above
PostClick(799,449,"Particle Clicker - Google Chrome")
sleep 1000
}
return
F12::ExitApp

AHK Script using PostClick and PostMessage

I'm trying to write a simple AHK script to type a few characters and make a few clicks at specific coordinates in a background window, but I can't seem to get it to work, and I'm not finding a lot of information on how to properly use PostClick.
I was able to get this to work using Click when the window is active. Also tried using ControlClick, which seemed to work, except the click was happening on the location where the mouse was rather than the coordinates provided (and also only when the window was activated again).
Can I please have some help / advice on how to get this to work?
Here's my current script:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
BreakLoop = 0
Loop, 3 {
if (BreakLoop = 1)
break
Sleep 1000
PostClick(clickVar,1055,525)
{
ControlSend,, 3, ahk_pid %pid%
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONUP
}
Sleep 1500
}
E::
BreakLoop = 1
return
Thanks ahead of time for the help.
I am going to assume you are attempting to use this function.
I am unsure how this function works, but I think what you want is something like:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
WinGetTitle, clickTitle, ahk_pid %pid%
WinGetClass, clickClass, ahk_pid %pid%
BreakLoop = 0
Loop, 3
{
if (BreakLoop = 1)
break
Sleep 1000
PostClick(1055,525,clickClass, clickTitle)
ControlSend,, 3, ahk_pid %pid%
Sleep 1500
}
E::
BreakLoop = 1
return
PostClick(x, y, class, title)
{
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, 1, %lParam%, %class%, %title% ;WM_LBUTTONDOWN
PostMessage, 0x202, 0, %lParam%, %class%, %title% ;WM_LBUTTONUP
}
You need to place the function somewhere in the same file or at least accessible by the current file. You can do #include <script name here>.ahk at the top of your file and place that function as a new file in the same directory if you want.
The function you are trying to use takes in an x and a y for where to click in that window. Then it also takes in a class and title in order to know which window to actually use. Hope this works or helps you in some way.

AHK! Can't get "Ctrl+Q then K" Input to work (Shortcut in Q-Dir tool)

Background:
So I use the Explorer tool Q-Dir (link to homepage).
In Q-Dir you can bookmark folder you want to access regularly.
You can shortcut to those bookmarks by pressing Ctrl + Q. This opens a jumplist of your bookmarked folders.
So I want to open my projects folder which is named "Kundenprojekte" (customer projects).
Pressing "K" while said jumplist is open, opens this folder in the current tab.
So my problem occurs here:
This whole process is what I want to map with AHK to let's say WinKey + B.
This is my code so far:
#b::
qdirId := WinExist("Q-Dir 5.98.9") ; I don't know if this works I got some
; inspiration by another AHK script
if (qdirId = 0) {
MsgBox Q-Dir ist nicht geöffnet. ; Q-Dir is not open.
IfWinExist, "Q-Dir 5.98.9"
{
WinActivate, "Q-Dir 5.98.9" ;I know this doesn't work plz help
SetKeyDelay 10,1000
Send, ^{Q}
SetKeyDelay, 10
Send {K}
return
}
}
return
Yes , so far there is no "else" to IfWinExist. And I don't think there will be because I would need to run Q-Dir as administrator (due to UAC preventing writing operation to my C: drive) But that is of no concern (because I usually start Q-Dir as my first programm after booting).
The main issue I have is getting "send Control + Q" then "K" to work.
I got it to send "K" but not once was "Ctrl + Q" recognized.
Any idea what I do wrong?
Thank you in advance :)
SetTitleMatchMode, 2 ; The window's title should contain "Q-Dir 5.98.9"
#b::
qdirId := WinExist("Q-Dir 5.98.9")
if (qdirId = 0)
{
MsgBox Q-Dir ist nicht geöffnet. ; Q-Dir is not open.
return ; stops code from going any further, to the lines below
}
; otherwise:
WinActivate, Q-Dir 5.98.9
WinWaitActive, Q-Dir 5.98.9 ; important
SetKeyDelay 10,1000
Send, ^{Q}
; Try also:
; Send, {CTRLDOWN}q{CTRLUP}
; SendEvent {CTRL DOWN}{q DOWN}{CTRL UP}{q Up}
SetKeyDelay, 10
Send {K}
return
https://autohotkey.com/docs/commands/SetTitleMatchMode.htm

What is the right way to send Alt + Tab in Ahk?

Ok. I know this is a very stupid question.
But I'm stuck already for an hour.
I got very little experience with ahk, however I made work every script until now with no problems. I explored the ahk tutorials but found no solution up to now.
I'm trying to switch to prev. app with a single numpad off key.
I've tried:
!{Tab}
,
{Alt down}{Tab}{Alt up}
I've tried it with Sleep delays, multiline, multiline inside brackets, with and without commas after commands, etc.
I'm quite sure is very simple but something I've not tried yet.
Any suggestion?
$F1:: AltTab()
$F2:: AltTabMenu()
; AltTab-replacement for Windows 8:
AltTab(){
list := ""
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
IfWinActive, ahk_id %this_ID%
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (!IsWindow(WinExist("ahk_id" . this_ID)))
continue
WinActivate, ahk_id %this_ID%
WinWaitActive, ahk_id %this_ID%,,2
break
}
}
; AltTabMenu-replacement for Windows 8:
AltTabMenu(){
list := ""
Menu, windows, Add
Menu, windows, deleteAll
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (!IsWindow(WinExist("ahk_id" . this_ID)))
continue
Menu, windows, Add, %title%, ActivateTitle
WinGet, Path, ProcessPath, ahk_id %this_ID%
Try
Menu, windows, Icon, %title%, %Path%,, 0
Catch
Menu, windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0
}
CoordMode, Mouse, Screen
MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
CoordMode, Menu, Screen
Xm := (0.25*A_ScreenWidth)
Ym := (0.25*A_ScreenHeight)
Menu, windows, Show, %Xm%, %Ym%
}
ActivateTitle:
SetTitleMatchMode 3
WinActivate, %A_ThisMenuItem%
return
;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------
IsWindow(hWnd){
WinGet, dwStyle, Style, ahk_id %hWnd%
if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {
return false
}
WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
if (dwExStyle & 0x00000080) {
return false
}
WinGetClass, szClass, ahk_id %hWnd%
if (szClass = "TApplication") {
return false
}
return true
}
EDIT (suggested by the user Ooker):
The script pops up a menu for you to choose.
This is what it looks like:
If you just want to switch back to the previous application, use Send, !{Esc}
You shouldn't manually send alt+tab as it is a special windows command, rather use the AltTab commands that do that for you.
AltTabMenu opens the tab menu and selects the program, whileAltTab, ShiftAltTab navigate through it.
h::AltTabMenu
n::AltTab
m::ShiftAltTab
There are some issues with Windows 8/10 and keys like ctrl-alt-del and alt-tab. Here is one solution:
F1::
{
Send {Alt Down}{Tab} ;Bring up switcher immediately
KeyWait, F1, T.5 ; Go to next window; wait .5s before looping
if (Errorlevel)
{
While ( GetKeyState( "F1","P" ) ) {
Send {Tab}
Sleep, 400 ; wait 400ms before going to next window
}
}
Send {Alt Up} ;Close switcher on hotkey release
}
return
My personal goal was to remap Alt-Tab to Win-Tab (because I'm using a Mac keyboard on a Windows 10) so I took what Stepan wrote above plus some documentation and here is is, working fine for me :
#Tab::
{
Send {LAlt Down}{Tab}
KeyWait, LWin ; Wait to release left Win key
Send {LAlt Up} ; Close switcher on hotkey release
}
return
Worked for me:
F1::
Send, {ALT DOWN}{TAB}{ALT UP}
return
It simulates the Alt + Tab behavior for F1 key.
Well, finally I found the reason and some "solutions" here and here.
It seems that Windows 8 blocks Ahk {Alt Down}{Tab} and AltTabMenu and some other keys.
For now I'm using this to scroll windows forward:
Send !{ESC}
This to display the AltTabMenu:
Run, "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
And this to switch to the previous app as suggested in one of the topics:
LCtrl & z:: ; AltTabMenu
state := GetKeyState("Capslock", "T")
if state = 1
SetCapsLockState, Off ; CapsLock On blocks Task Switching metro window
Send, !{Tab} ; prevents displaying inactive Task Switching metro window
run, Window Switcher.lnk ; must be in script directory otherwise include path
WinWait, Task Switching,, 2
KeyWait, Ctrl
Send, {Enter}
if state = 1
SetCapsLockState, On ; restores CapsLock State
state =
return
#IfWinActive, Task Switching
LCtrl & q::Send, {Right}
LCtrl & a::Send, {Left}
It would be great to get to the previous app with no AltTabMenu splashing.
In case you want to do multiple "tabs", then the below function should help doing that. This was at least own solution on my Windows 8.1 machine.
The approach is:
1) Get a list of all the windows
2) Loop 1:
find the index of the current window
set the index to switch to ("current" + "offset")
3) Loop 2:
loop until you hit the index to switch to, then switch window
AutoHotKey code sample below:
; Test switch of 1 window
F1::AltTabFunction(offset:=1)
; Test switch of 2 windows
F2::AltTabFunction(offset:=2)
AltTabFunction(offset:=1)
{
; ****************************
; Function for switching windows by ALT-TAB (offset = number of windows to "tab")
; ****************************
; Get list of all windows.
WinGet, AllWinsHwnd, List
WinGetTitle, active_title, A ; Get title of active window.
; Find index of the current window.
counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
Loop, % AllWinsHwnd
{
; Find title for window in this loop.
WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
; [1] : https://autohotkey.com/docs/commands/WinGet.htm
WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%
; Skip hidden windows by checking exStyle.
If !(exStyle & 0x100){
Continue
}
; Window is not hidden. Increase counter.
counter_of_none_hidden_windows := counter_of_none_hidden_windows+1
; Set flag.
titles_match := CurrentWinTitle = active_title
If (titles_match) {
window_index_to_switch_to := counter_of_none_hidden_windows+offset
break
}
}
; Find index of the window to switch to and do the actual switch
counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
Loop, % AllWinsHwnd
{
; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
; [1] : https://autohotkey.com/docs/commands/WinGet.htm
WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%
; Skip hidden windows by checking exStyle.
If !(exStyle & 0x100){
Continue
}
; Window is not hidden. Increase counter.
counter_of_none_hidden_windows := counter_of_none_hidden_windows+1
; Set flag.
found_window_to_switch_to := counter_of_none_hidden_windows = window_index_to_switch_to
; Switch window.
If (found_window_to_switch_to) {
; Get title.
WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
; Activate by title.
WinActivate, %CurrentWinTitle%
; Stop loop.
break
}
}
return ; Nothing to return
}
send {Alt down}{tab}
send {Alt up}
!{Tab} works to switch between windows if you add sleep before and after it.
Sleep 100
Send !{Tab}
Sleep 100
Please refer to this link: https://www.autohotkey.com/docs/Hotkeys.htm#alttab
To cancel the Alt-Tab menu without activating the selected window, press or send Esc. In the following example, you would hold the left Ctrl and press CapsLock to display the menu and advance forward in it. Then you would release the left Ctrl to activate the selected window, or press the mouse wheel to cancel. Define the AltTabWindow window group as shown below before running this example.
LCtrl & CapsLock::AltTab
#IfWinExist ahk_group AltTabWindow ; Indicates that the alt-tab menu is present on the screen.
MButton::Send {Blind}{Escape} ; The * prefix allows it to fire whether or not Alt is held down.
#If
I've modified the example from the help page on this found here: https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail
This was mainly to remap the Windows+Tab key to the Alt+Tab key in this example.
It opens the task view and waits for the user to click, escape or enter. The example from the help page has the alt key getting stuck for me so I changed it to work a little better.
Please let me know if this works for you all.
; Override the Left Win key and tab to Alt + Tab
; Help found here:
; https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail
; #IfWinExist ahk_group AltTabWindow
#NoEnv
#SingleInstance force
SendMode Input
LWin & Tab::Send {Alt down}{tab} ; Asterisk is required in this case.
!LButton::
{
Click
Send {Alt up} ; Release the Alt key, which activates the selected window.
}
!Enter::
{
Send {Alt up} ; Release the Alt key, which activates the selected window.
}
~*Esc::
{
Send {Alt up} ; When the menu is cancelled, release the Alt key automatically.
;*Esc::Send {Esc}{Alt up} ; Without tilde (~), Escape would need to be sent.
}
I got ALT TAB to work with F1
By pressing F1 you can switch to the last window
While F1 is kept pressed you can move around with arrow keys or tab to select the window you need.
Code:
`F1::
Send, {ALT DOWN}{TAB}{TAB UP} ; If F1 is down it invokes the menu for switching windows.
KeyWait, F1 ; Show menu for switching windows by keeping ALT down until user physically releases F1.
Send, {ALT UP} ; If F1 is released release ALT key
return`
Documentation links
KeyWait
KeyList
I think this question was meant to be a simple request of: how to alt tab in Win10 using AHK, since win10 changed things up? -> I found the most simple solution as shown below... the code makes it necessary to keep the alt key down while the Win10 emu is open - then use the arrow keys an additional number of tabs (if you need three alt tabs, then it's "alt tab, then right 2", see?
macro key name::
{
Sleep 100
Send, ^c
Sleep 1000
Send, {alt down}{tab}
Sleep 400
Send, {right 2}{alt up}
Sleep 400
Send, ^v
Sleep 400
}
So just play with this snip in your code and you can jump passed the 'next' window(s) open.
Rossman

Autohotkey: AlwaysOnTop, On, Run ahk_class SunAwtDialog

i want "Run ahk_class SunAwtDialog" window stayAlwaysOnTop. This time its not working. its working with other windows for e.g. google chrom. Here is the code:
CoordMode,Pixel
CoordMode,Screen
CoordMode,Mouse
run_Win = Run ahk_class SunAwtDialog
SetTitleMatchMode,1
IfWinNotExist,%run_Win%
{
MsgBox,:( Oops
Reload
}
SetTitleMatchMode,2
WinSet , AlwaysOnTop , Off , SciTE4AutoHotkey
SetTitleMatchMode,1
WinSet , AlwaysOnTop , On , %run_Win% ; dont work. window stays behind
WinGetPos,x,y,w,h,%run_Win%
MouseGetPos,xM,yM
MouseMove,% x, % y , 10 ; works: mouse is moving to left top of the window
Sleep,500
MouseMove,% xM, % yM , 10 ; works: restore mouse pos
MsgBox, , endOfProgramm1, endOfProgramm2 , 1
Your code snippet
run_Win = Run ahk_class SunAwtDialog
SetTitleMatchMode,1
IfWinNotExist,%run_Win%
Could be translated to
SetTitleMatchMode,1
IfWinNotExist,Run ahk_class SunAwtDialog
. IfWinNotExist, Run ahk_class ... does not make any sense. Remove the run in the contents of yourrun_Win variable.
(See WinTitle for information on how to catch the right window)