I have found the following script for dynamically assigning hotkeys to already open windows:
Code (Expand):
Loop 10
{
i := A_Index - 1
HotKey #^%i%,DynHotkey
HotKey #%i%, DynHotkey
HotKey #!%i%,DynHotkey
}
Exit
DynHotkey:
StringRight i, A_ThisHotKey, 1
StringMid what,A_ThisHotKey, 2, 1
var := var%i%
IfEqual what, ^, WinGet var%i%, ID, A ; Save ID
Else IfEqual what,!, WinMinimizeAll ; MinimizeAll
WinRestore ahk_id %var%
WinActivate ahk_id %var% ; Switch
Return
(the code was copied from this thread http://www.autohotkey.com/forum/topic38773.html&highlight=dynamic+hot+key)
With the above script you can:
Use Win+Ctrl+0..9 to attach hotkey to current active window.
Use Win+0..9 to switch to correspoding window.
However, if I assign a hotkey to a given window (using Win+Ctrl+0..9), and then I want I want to go back to that window (Win+0..9), the window is reset to a new size & location.
Is there way of saving the size & location of the window along with it's ID?
If so, what would the script look like?
I am running the above script on Windows 7 64-bit.
Thanks a lot,
You dont need to complicate the code :)
Quick question: if your window is minimized you dont have any problems right?
The "problem" on the code is the WinRestore.
The thing is that if the window is not minimized and then you do a WinRestore it will change the size and position to the "not maximized" version of it.
WinActivate automatically does a WinRestore only if the window is minimized, so you can safely remove line 16 (the WinRestore one) since WinActivate will do what you need.
--edit--
this is how the code should look:
Loop 10
{
i := A_Index - 1
HotKey #^%i%,DynHotkey
HotKey #%i%, DynHotkey
HotKey #!%i%,DynHotkey
}
Exit
DynHotkey:
StringRight i, A_ThisHotKey, 1
StringMid what,A_ThisHotKey, 2, 1
var := var%i%
IfEqual what, ^, WinGet var%i%, ID, A ; Save ID
Else IfEqual what,!, WinMinimizeAll ; MinimizeAll
WinActivate ahk_id %var% ; Switch
Return
I tested it, it works perfectly.
You can use WinGetPos to read the actual position and save it. Then you can use WinMove to set the position. Here is a function list: http://www.autohotkey.com/docs/commands.htm.
Related
I just started playing around with AutoHotKey today but got stuck at moving/resizing windows...
WinMove needs a WinTitle but I can't figure out how the WinTitle works.
When I try the following code:
#SingleInstance force
#y::
run, notepad
Sleep, 1000
WinGetTitle, window,, A
MsgBox, Active window: %window%
Sleep, 1000
WinMove, window,, 0, 0
MsgBox, %window% moved.
return
Notepad dosn't get moved to the top left corner, but why?
I have also tried storing the ID as a string: program := window WinMove, program,, 0, 0 but that didn't work either.
In your example "window" is a variable and "WinMove" is a command.
Commands always use "traditional syntax". Meaning: when you use a variable in a command, you have to enclose the variable in percent signs:
WinMove, %window%,, 0, 0
EDIT:
Btw.
WinGetTitle, window,, A
has to be
WinGetTitle, window, A
EDIT2:
#SingleInstance force
#y::
run, notepad
WinWait, Untitled - Notepad ; title - Use Window Spy to find the exact title of this window
; IfWinNotActive, Untitled - Notepad, ,WinActivate, Untitled - Notepad
; WinWaitActive, Untitled - Notepad
Sleep, 200
WinMove, Untitled - Notepad,, 0, 0
return
I need Firefox to always open Desktop in the Save as dialogue on saving files, so I can type in the name of the folder on the Desktop and save the file where I want. That would be a simple and very efficient way of grouping downloaded files. The problem is that Firefox opens the last save folder in the Save as dialogue window, and I can't do this in a reasonable number of steps. To get Desktop automatically opened in the Save as dialogue the best I could think of is this autohotkey script, and I have a problem with it:
!+^s::
MouseMove, %A_CaretX%, %A_CaretY%
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
SetMouseDelay, 2
MouseMove,445,46
Click Left
Send,Desktop
Send,{Enter}
MouseMove, %xpos%, %ypos%
Click Left
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
SetMouseDelay, 2
MouseMove,445,46
Click Left
MouseMove,%xpos%, %ypos%
Click Left
Input, L, V L1
Loop {
Input, L, V L1 T1.4
If (ErrorLevel = "Timeout")
Break
}
Send,^{Down}
Send,{Enter}
MouseClick,Left,720,473
MouseClick,Left,720,473
return
The problem with this script is the Input command - it doesn't wait for me to type in the name of the folder but executes the following command immediatelly.
EDITED: The script is now fully working (thanks to Forivin). An additional line with Input comamand "Input, L, V L1" was required for the script to pause and wait for the name of the folder to be typed in. I've used MouseClick command and coordinates that work for my monitor to confirm the dialogue box. Confirming the dialog box with Enter (4 times) doesn't work accurately on my computer for some reason. EDIT2: Added two lines, in order to make use of the drop down list folder name suggestions, so the whole folder name doesn't need to be typed in.
Using the Control*-commands would be a much more reliable way of doing this:
!+^s::
WinGet, hWnd, ID, A ;Get handle of active window
;Navigate the the users desktop folder
ControlFocus, ToolbarWindow324, ahk_id %hWnd%
ControlClick, ToolbarWindow324, ahk_id %hWnd%,,,2, NA
ControlSetText, Edit2, `%HOMEPATH`%\Desktop\, ahk_id %hWnd%
ControlSend, Edit2, {Enter}, ahk_id %hWnd%
;Set focus to the folder list
Sleep, 100
ControlFocus, DirectUIHWND2, ahk_id %hWnd%
Input, L, V L1 T2 ;wait until you start typing a folder name (if you just wait 2 seconds, the download will be canceled)
If (ErrorLevel = "Timeout") { ;if you waited too long:
ControlClick, Button2, ahk_id %hWnd%,,,, NA ;click the Cancel button
Return ;end of the hotkey
}
Loop { ;wait until you haven't typed a new letter for 0.4 seconds
Input, L, V L1 T0.4
If (ErrorLevel = "Timeout")
Break
}
ControlGetText, button1Text, Button1, ahk_id %hWnd%
If (button1Text = "&Open") { ;If your windows isn't English, you need to replace the word "Open", if you're confused remove the if statement (but leave the content)
ControlClick, Button1, ahk_id %hWnd%,,,, NA ;click the Open button
Sleep, 100
}
ControlClick, Button1, ahk_id %hWnd%,,,, NA ;click the Save button
Return
I'm trying to create a Hotkey (Win+Shift+Q) that toggles on/off another Hotkey that changes the right Alt key to a left mouse click; however, I can't get it to work.
Expected Behavior:
Pressing Windows+Shift+Q will initially toggle the Right-Alt key to act as a left mouse click.
Pressing Windows+Shift+Q again will toggle the Right-Alt key back to acting as a Right-Alt key.
Pressing Windows+Shift+Q again will revert to the left-click behavior (see #1). And so on.
Here's the most current iteration of my code:
Hotkey, RAlt, MyClick, On
#+Q:: ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
switch := !switch
MsgBox %switch%
Hotkey RAlt, % (switch ? "Off": "On")
Return
MyClick:
MouseClick
Return
When I run my script I get the following error after clicking OK on the MsgBox and the script quits:
Error: Nonexistent hotkey
Specifically: RAlt
Line#
141: Hotkey,RAlt,MyClick,On
143: switch:=!switch
144: MsgBox %switch%
-->145: Hotkey RAlt, % (switch ? "Off": "On")
146: Return
149: MouseClick
150: Return
The current thread will exit.
Most of the other posts that might relate (Can AutoHotKey toggle keymapping?, Autohotkey: Toggle a set of keybinds on and off) only deal with key to key mapping and not key to mouse mapping. I can't tell if that is the cause of my issues or not.
Previously I had this, but the Win+Shift+Q didn't toggle the behavior, RAlt always acted as a left-click so I commented it out:
#+Q:: ;Win+Shift+Q :: ::Right-Alt acts as a left mouse button click
RAlt::LButton
;Hotkey, RAlt, Toggle ;Does not work for some reason
int += 1
test := mod(int, 2) = 0
if (test) {
msgbox on
Hotkey, RAlt, On
}
else {
msgbox off
Hotkey, leftClick, Off
}
Return
I'll also add that I would like this behavior across Windows, not just a single application (which also seems to be a topic in other posts that allows for the #IfWinActive-type suggestions/solutions).
I tried your current iteration of code in AutoHotkey v1.1.13.01 Unicode 32-bit and I don't have any errors after pressing OK on the message box, the script works as advertised.
Try updating your AutoHotkey version here: http://ahkscript.org/download/ and see if the problem persists.
bState:=False
#If bState
RAlt::Click
#If
#+vk51:: ; win + shift + q
KeyWait, vk51
TrayTip, % "state of switch", % (bState:=!bState) ? "on":"off"
Return
I have a AutoHotKey script that asks me if I want to remap my Win keys to Ctrl or cancel their remapping, thus making them Win keys again.
However I cannot find a way to cancel the remap. If I use the command LWin::Lwin I get the error message that there is a "duplicate key".
I'm new to AutoHotKey, but I did search first, so please don't bite my head off is this is a stupid question. (It is a Lenovo laptop with Windows7-64).
Here's the script:
MsgBox, 4, , Remap CTRL for Desktop Keyboard?
IfMsgBox, Yes
LWin::LCtrl
RWin::RCtrl
return
; Otherwise, the user picked No
; LWin::LWin
; RWin::RWin
; return
Various ways.
Create a hotkey to close ahk, e.g. ^!x::ExitApp = [Ctrl]+[Alt]+[x]
Create a hotkey to disable/enable all hotkeys e.g. f12::suspend
Create hotkeys that ONLY work in a specific appliaction.
Here are all suggestions combined.
Under normal circumstances: LWin::LCtrl and RWin::RCtrl are active, Unless you pressed F12. You can in AHK_L set variables that can be used in #If (Var = 1), where you can define Hotkeys that only work when that variable is set to 1 (true).
SetTitleMatchMode, 2 ; Allow the use of a portion of the wintitle
F12::
Suspend
If A_IsSuspended
TrayTip, HotKeys, Off, 3, 0
Else
TrayTip, HotKeys, On, 3, 0
Return
^!x::ExitApp
LWin::LCtrl
RWin::RCtrl
F1::MsgBox, Normal Mode
#IfWinActive, Window title
F1::MsgBox, Window X is active
F2::MsgBox, You pressed F2 inside Window x
#IfWinActive
Toggle := False
F10::Toggle := !Toggle ; Turns Mouse button ON|Off
#if Toggle ; ONLY worls in AHK_L
LButton::Return ; Disables Mouse button
#if
Here's a version you can drive from the command line:
; Allow the script to be reloaded multiple times
#SingleInstance force
; Check the command line for input
NumberOfParameters = %0%
; If any command line param was passed then just unload the mappings
If ( NumberOfParameters > 0 )
{
MsgBox Command line parameter was passed, unloading...
ExitApp
}
Else
{
; Let's ask the user what they want to do
MsgBox, 4, , Remap CTRL for Desktop Keyboard?
IfMsgBox, Yes
{
; If yes, then remap
MsgBox Keys have been mapped.
}
Else
{
; If no, then unload
MsgBox Unloading mapping.
ExitApp
}
}
; Keys will be mapped so long as the script remains resident
LWin::LCtrl
RWin::RCtrl
In Windows 7, you can give focus to a window just by hovering over it with the mouse. This feature is not enabled by default, but you can enable it in the Control Panel. (Here is the path to take:
[Ease of Access Center-->Make the mouse easier to use-->check "Activate a window by hovering over it with the mouse"]).
I like this feature a lot, but sometimes it annoys me when I try to open a C# class in Visual Studio using Resharper. I'll hit CTRL+N and type the name of the class I want to see (for example, "MyWpfClass"). Resharper will then show a dropdown of suggestions with "MyWpfClass" on top. I hit return, and now Resharper opens a dropdown which lets me choose between "MyWpfClass.xaml" and "MyWpfClass.xaml.cs". However, if the mouse cursor is in the wrong place, the dropdown closes within a second and I'm back to square one. Is there a way to fix this without turning the focus-follows-mouse feature off?
I was having the same problem with MS Outlook: the auto-suggestions list for contacts would close automatically because Windows treated it as a window rather than part of the New Message window.
You could use NiftyWindows, which has the same option "Focus Follows Mouse", accessible through its context menu.
Alternatively, as it is written in Autohotkey, you could use extract and run its subroutine "XWN_FocusHandler" into a standalone script:
#Persistent
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn All, OutputDebug ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTimer, XWN_FocusHandler, 100
return
XWN_FocusHandler:
CoordMode, Mouse, Screen
MouseGetPos, XWN_MouseX, XWN_MouseY, XWN_WinID
If ( !XWN_WinID )
Return
If ( (XWN_MouseX != XWN_MouseOldX) or (XWN_MouseY != XWN_MouseOldY) )
{
IfWinNotActive, ahk_id %XWN_WinID%
XWN_FocusRequest = 1
Else
XWN_FocusRequest = 0
XWN_MouseOldX := XWN_MouseX
XWN_MouseOldY := XWN_MouseY
XWN_MouseMovedTickCount := A_TickCount
}
Else
If ( XWN_FocusRequest and (A_TickCount - XWN_MouseMovedTickCount > 500) )
{
WinGetClass, XWN_WinClass, ahk_id %XWN_WinID%
If ( XWN_WinClass = "Progman" )
Return
; checks wheter the selected window is a popup menu
; (WS_POPUP) and !(WS_DLGFRAME | WS_SYSMENU | WS_THICKFRAME)
WinGet, XWN_WinStyle, Style, ahk_id %XWN_WinID%
If ( (XWN_WinStyle & 0x80000000) and !(XWN_WinStyle & 0x4C0000) )
Return
IfWinNotActive, ahk_id %XWN_WinID%
WinActivate, ahk_id %XWN_WinID%
XWN_FocusRequest = 0
}
Return