Hold RMB after button and click - autohotkey

I can't figure this out.
I'm trying to accomplish something like this:
Press a GUI button
When I click the mouse next hold the RMB
When I click again release the RMB
Current work:
Toggle=0
GUI, Add, Button, w50 h50, Nbutton
GUI, Show, x50 y50
return
ButtonNButton:
{
Toggle:=!Toggle
}
if GetKeyState("LButton","P")
if (Toggle == 1)
MsgBox, Do
Toggle:=!Toggle
return

This should work to accomplish what you want.
Note that right now, this example script is limited to only work in Notepad. You should update the script to target whatever program/game/window you want the hotkey to be active in.
Toggle := 0
GUI, Add, Button, w50 h50, Nbutton
GUI, Show, x50 y50
return
ButtonNButton:
Toggle:=!Toggle
; Update this line to match whatever window you want the hotkey active
; in, or delete this line to make the hotkey active everywhere.
Hotkey, IfWinActive, ahk_class Notepad
if Toggle
{
Hotkey, ~*LButton Up, RBSwitch, On
}
else
{
if GetKeyState("RButton") ;release RButton when hotkey is disabled
Send {RButton Up}
Hotkey, ~*LButton Up, RBSwitch, Off
}
RETURN
RBSwitch:
if GetKeyState("RButton")
Send {RButton Up}
else
Send {RButton Down}
RETURN

Related

Left Mouse Click Not Work if Gui Follow The Mouse Pointer Autohotkey

Left Mouse Click Not Work if Gui Follow The Mouse Pointer Autohotkey.
i Have a Ahk Script that is able that my Gui with Picture Follow the Mouse Pointer.
But The Left Mouse Click does not work. (i can click/run notepad but i can not select Text in notepad!)
i do not now why it does do that? (the Gui+Picture is not on the Mouse Pointer.)
Follow-Pointer.ahk
#SingleInstance force
CoordMode, Mouse, screen
CoordMode, ToolTip, screen
IfNotExist, c5sc29f.gif ;c5sc29f.gif File is roket.gif
URLDownloadToFile, https://i.imgur.com/c5sc29f.gif, c5sc29f.gif
Gui -Caption +LastFound +ToolWindow +AlwaysOnTop
Gui, Margin, 0, 0
Gui, Color, FFFFFF
Gui, Add, Picture, x100 y100 w-1 h50 +BackgroundTrans, c5sc29f.gif ;
;WinSet, Transcolor, FFFFFF
WinSet, ExStyle, +0x20 ; set click through style
gosub, F1
return
F1:: SetTimer Draw, % (switch:= !switch) ? "20" : "-20"
Draw:
MouseGetPos, x, y
y+=10, x+=10
If switch {
Gui Show, x%x% y%y%
;~ ToolTip,% "x" x " y" y,% x+0,% y+50
} else {
Gui Cancel
ToolTip,
}
return
esc::ExitApp
ExitApp
return
Try
Gui Show, x%x% y%y% NoActivate
NoActivate avoids deactivating the currently active window.

Input text on any application with autohotkey gui

I'm trying to find a solution to add predefined text to any application so instead of remembering a hotkey/hotstring combination I'll just need to click on the GUI button for the text.
This is what I have right now:
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
; Generated using SmartGUI Creator 4.0
Gui, Show, x152 y89 h131 w222, New GUI Window
Return
ButtonTitle:
Send Title
return
ButtonParagraph:
Send Paragraph
return
GuiClose:
ExitApp
My problem is that I'm not able to make it work properly. I just want to click the button and that word gets shot to the notepad/word/any application.
The solution would be to always keep track of the current and the last active window. To achieve that you could use a shell hook to get notified when the active window changes: https://autohotkey.com/board/topic/66726-method-to-detect-active-window-change/
So you could have two variables currentWin and lastWin and when the active window changes you set lastWin := currentWin and currentWin := activeWin.
Soemthing like this maybe:
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
; Generated using SmartGUI Creator 4.0
Gui, Show, x152 y89 h131 w222, New GUI Window
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage(wParam, lParam) {
If (wParam=4) { ;HSHELL_WINDOWACTIVATED
lastWin := currentWin
currentWin := "ahk_id " . lParam
}
}
ButtonTitle:
WinActivate, % lastWin
Send Title
return
ButtonParagraph:
WinActivate, % lastWin
Send Paragraph
return
GuiClose:
ExitApp
(completely untested)
Hi Stephan and Forivin,
Thank you for your help. I used the alt+tab approach to solve the problem though I need to ensure that I get notepad on alt+tab keystroke or else it goes to another application. The below solves the problem.
Gui, Add, Button, x22 y20 w120 h40 , Title
Gui, Add, Button, x22 y70 w120 h40 , Paragraph
Gui, Show, x152 y89 h131 w222, New GUI Window
Return
ButtonTitle:
Send, !{Esc}
Send Title
return
ButtonParagraph:
Send, !{Esc}
Send Paragraph
return
GuiClose:
ExitApp

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

Show the GUI every time the Notepad window is opened

I am trying to create the code, that will show the foo window every time the Notepad is opened. The problem is, that if I close foo once, it will not be shown again (when I open Notepad next time). Currently I am using the following code:
SetTitleMatchMode, 2
WinWaitActive, Notepad
Gui, Add, Button, w200 h25 gTest1 , button 1
Gui, Add, Button, w200 h25 gTest2 , button 2
Gui, Show,, foo
Return
Test1:
Run test1.ahk
Return
Test2:
Run test2.ahk
Return
#Persistent
Gui, Add, Button, w200 h25 gTest1 , button 1
Gui, Add, Button, w200 h25 gTest2 , button 2
SetTimer, Show_Gui, 300
return
Show_Gui:
IfWinNotExist, ahk_class Notepad
{
Gui, cancel
return
}
; Otherwise:
SetTimer, Show_Gui, off
Gui, Show,, foo
WinWaitClose, ahk_class Notepad
SetTimer, Show_Gui, on
Return
Test1:
; do sth
return
Test2:
; do sth
return
https://autohotkey.com/docs/commands/SetTimer.htm#Examples

How can an AutoHotkey GUI handle the {Return} key without using a default button?

I'm using a AutoHotkey GUI + ListBox to select an option from various choices, but I cannot find a way to make the RETURN key to close the GUI without creating an additional default button with an associated ButtonOk event. Here's my code using a default button:
Gui, +LastFound +AlwaysOnTop -Caption
Gui, Add, ListBox, vMyListBox gMyListBox w300 r10
Gui, Add, Button, Default, OK
GuiControl,, MyListBox, Option 1
GuiControl,, MyListBox, Option 2
GuiControl,, MyListBox, Option 3
Gui, Show
return
MyListBox:
if A_GuiEvent <> DoubleClick
return
ButtonOK:
GuiControlGet, MyListBox
Gui Hide
MsgBox %MyListBox% selected!
ExitApp
GuiClose:
GuiEscape:
ExitApp
You can use the Hotkey command to temporarily enable Return as a hotkey when the GUI is visible and disable it when the gui is closed.
You can monitor for the WM_KEYDOWN message. In the auto-execute section:
OnMessage(0x100, "OnKeyDown")
Then elsewhere in the script:
OnKeyDown(wParam)
{
if (A_Gui = 1 && wParam = 13) ; VK_ENTER := 13
{
GuiControlGet, MyListBox
Gui Hide
MsgBox %MyListBox% selected!
ExitApp
}
}