hover mouse over window and move mouse to resize window using autohotkey - resize

I'm trying to creating a script that allows user to resize a window by hovering the mouse by pressing a keyboard shortcut without having to move a mouse to corner of a window each time.
This feature is currently available as part of BetterSnapTool for Mac OS X but I'm trying to develop the same feature for Windows 10 using AutoHotkey
I've developed a script for autohotkey that works well for most applications except for chrome and spotify (see below). It often gets stuck with the title bar menu open after hitting alt+space see video here
^+x::
MouseGetPos,,, hwnd
WinActivate, ahk_id %hwnd%
Send !{Space}
Sleep 1
Send m
sleep 1
send {down}
Return
Is there a more reliable way can do this?

I was able to solve this reliably by moving the mouse to the top right of the window next the minimize button and leaving the mouse button down while using blockinput.
Benefits:
This allows the the user to move the mouse freely to move the window.
It works for resizing as well.
being able to move windows if they're snapped fullscreen.
TIP: script has to be run as administrator for blockinput to work...
;Move Window Mode
^+x::
KeyWait Shift
KeyWait Alt
BlockInput, On
MouseGetPos,,, hwnd
WinActivate, ahk_id %hwnd%
WinGetPos, , , Width, Height, A
MouseMove, Width-186, 10
Click, down
Send {Shift Up}
Send {Ctrl Up}
BlockInput, Off
Return
;Resize Window Mode
^+z::
KeyWait Shift
KeyWait Alt
BlockInput, On
MouseGetPos,,, hwnd
WinActivate, ahk_id %hwnd%
WinGetPos, , , Width, Height, A
MouseMove, Width-3, Height-3
Click, down
Send {Shift Up}
Send {Ctrl Up}
BlockInput, Off
Return

The problem is that Sleep, 1 might not be enough time for the menu to appear on all applications.
The most reliable way to do it would be avoiding the menu altogether but that's quite some work to do through WinGetPos, MouseGetPos, and WinMove.
I guess you'll still be trying to use the menu, so my tip is to at least wait to be sure that the menu exists. You can do this getting the ProcessID of your hwnd, and wait so that first found window through that PID changes, that could be understood for us as a new window has appeared. In code means something like this:
WinGet, WinPID, PID, ahk_id %hwnd%
oldHwnd := WinExist("ahk_pid " WinPID)
Send !{Space}
newHwnd := oldHwnd
while (newHwnd == oldHwnd)
newHwnd := WinExist("ahk_pid " WinPID)
But you'd need some better exit conditions in case that the menu didn't happen to appear (a timeout?) to not get stuck there.
Still after that the Sleep, 1 might not be enough.

Related

AutoHotkey - XButton1 Not Working With Control

I'm playing a game that requires you to hold down the control button to jump. What I wanna do is make it so that the XButton1 (Mouse Button 4) holds down the Control, then when I release XButton1, Control also releases.
It's odd because the XButton2 (Mouse Button 5) works, but XButton1 doesn't. The XButton1 is the 'back' button on my mouse, and that function works.
If I use this code: XButton:: Send {Lctrl Down}, that works, but the Control doesn't release until I click the Left Control again.
Does anyone know what I can do to make this work?
-Edit-
I just tried the following code: XButton1::Soundbeep and it worked, which is weird. But assigning the Control to it doesn't, but assigning it to XButton2 does.
Especially for modifier keys, you need to address both Up and Down as follows:
XButton1::Send {LCtrl Down}
XButton1 Up::Send {LCtrl Up}
Edit:
If the above doesn't work on that particular game, try:
XButton1::SendInput {LCtrl Down}
XButton1 Up::SendInput {LCtrl Up}
or
XButton1::SendPlay {LCtrl Down}
XButton1 Up::SendPlay {LCtrl Up}

How to automate , to open a RSAToken application and simulate keystroke and to press Enter using AutoHotKey

I want to automate the following
1)Open the RSAToken Application
2)Simulate keystrokes to enter the PIN (say for eg : 223344 in that application)
3)Also to simulate the Enter keystroke upon entering the PIN
4)Copying passcode generated
i tried coding it by referring couple of articles but it was not working.
Following is the code
Run, "C:\Program Files (x86)\RSA SecurID Software Token\SecurID.exe"
;; 2222 is the PIN Code
Send, 2222
Sleep, 100
Send, {ENTER}
Sleep, 100
Send, ^c
Sleep, 100
Could anyone tell me , what am i missing ?
First of all, you need to wait for the application to start after running it. As I don't have the application, I can only suggest something like this:
WinWaitActive ahk_exe SecurID.exe
Then you need to copy the resulting code.
Ctrl+C only works if the code is selected, which it probably isn't. So Send ^c won't work.
Again, I don't have the application, but if it looks like this:
(source: rsa.com)
you need to move the keyboard focus to the Copy button and push it using the keyboard. That can probably be done either by
Send {Tab}{Enter}
or simply
Send {Enter}
if the button is already the default button.
In total, we have something like this:
Run, "C:\Program Files (x86)\RSA SecurID Software Token\SecurID.exe"
; Wait a while for the window to appear and become active.
timeoutSeconds:= 2
WinWaitActive ahk_exe SecurID.exe,, %timeoutSeconds%
if not ErrorLevel {
; The window is now active.
;; 2222 is the PIN Code
Send, 2222 ; Type the PIN code.
Sleep, 100
Send, {ENTER} ; Send the PIN code.
Sleep, 200 ; Wait for the program to generate the passcode.
Send {Tab} ; Move focus to the Copy button; this might not be needed.
Send {Enter} ; Press the Copy button.
Sleep, 100 ; Wait for the copy to happen; often unnecessary.
} else
MsgBox The window did not appear within %timeoutSeconds% seconds.
The general algorithm for writing a script like this is to first do the task manually using the keyboard only. When you can make that work, make a script that presses the same keys as you pressed manually.

Simulate mouse presses (BUTTON 4 and 5)

I'm trying to create a single file in BAT (Windows Command Prompt) to simulate a Mouse Back and Mouse Forward event (which is in fact just pressing down mouse button 4 and 5). I can't find anything on the internet on how to simulate these exact events. Most of them are about clicking the left mouse button but that's not what I need.
The program should be as simple as possible as I do not want heavy coding on this one. It should be straight forward and I hope one of you could help me out. Is this even possible (with BAT)?
The reason for this is that Logitech Gaming Software wrongly executes the back and forward buttons' up event twice, which results in annoying behaviours while for example browsing a webpage and trying to go back one page. It would then go back two pages.
Ok, I've been a little too quick with my question but it might help others. If you have the problem of your Logitech mouse doing double forward or backwards on browsers (Chrome, IE etc.) and in Windows Explorer please try the following.
First of all, you'll want to remove the currently mapped buttons. Be sure to select the desired profile first! So go over all three buttons (MOUSE 3, MOUSE 4 and MOUSE 5 (middle, tilt left, tilt right)) and remove their current mapping. By default (generic buttons) it will be "Middle click", "Back" and "Forward".
Now right click the profile at the top and select "Scripting". This is open a new window in which we can do some very advanced but still easy coding. Clear the whole thing and paste the code you find below in there. Press "Script" >> "Save" at the top of the window to save it and enjoy the working glory! You can easely test this by right clicking the profile again and selecting "Test profile". Now press any of the three buttons and you should get the outcome we want (down and up event only once on those exact buttons).
If you want to do some coding of your own feel free to change the script! The actual numbers to each mouse button can be found by clicking "Help" >> "Scripting API" at the top, along with some handy documentation. Keep in mind that you'll have to do this for every profile you desire. Doing this on the Default Profile seems the most reasonable for browsing.
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 4) then
OutputLogMessage("Pressing back button\n");
PressAndReleaseMouseButton(4);
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
OutputLogMessage("Pressing forward button\n");
PressAndReleaseMouseButton(5);
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == 3) then
OutputLogMessage("Pressing middle button...\n");
PressAndReleaseMouseButton(2);
end
--OutputLogMessage("event = %s, arg = %s\n", event, arg);
end

Switch to active window to left/right in taskbar

I have been using 7+ Taskbar Tweaker for a day and I'm blown away with two features. Mainly because windows alt+tab is not memorising order of open windows in it. There are two commands that I use constantly. 101 - Switch to the window which is located on the left to the active window on the taskbar and Switch to the window which is located on the right to the active window on the taskbar.
So when I do some programming and have to switch between windows fast, I just assign shortcuts for those 2 operations. It's making switching between windows so easy. One key is moving focus left and other to do right.
Is it possible to do this in autohotkeys? It would be so awesome. Because actually I don't need whole program, only this feature. I saw some code for it on web but it's not working in win 7/64. Basically I need script that will assign hotkey for moving focus from active window on taskbar to one to the left or one to the right. Also when it run out of left/right windows it should circle back to starting window(like normal alt tab is doing.)
Aw... yes I think I wrote a script could do just that some years ago, but I'm not sure how exactly I did it.
The real problem here is finding the order of the windows in the takbar.
But I can tell you there is no official api for it and from what I could tell using registry monitoring software, the order isn't stored in the registry.
For now, that's the only thing that comes to my mind:
F1::WinActivateByTbIndex(4) ;activate the fourth window in the task bar with F1
F2::WinActivateByTbIndex(2) ;activate the second window in the task bar with F2
WinActivateByTbIndex(index) {
WinActivate, ahk_class Shell_TrayWnd
ControlSend, MSTaskListWClass1, % "{Tab}{Right " index-1 "}", ahk_class Shell_TrayWnd
Send, {Up}{Enter}
}
But if you have multiple windows behind one taskbar icon, then you will just enable the first one, so that may not be too useful..
Windows also provides the hotkeys Win+1 up to Win+0 Win+2 ... upt to Win+0 as quick shortcuts to enable window 1-10, but it suffers from 2 limitations: It only works for the first 10 windows and you always activate the first window of a stack (just like my hotkeys).
Actually I was reading about this a lot, and the alt + esc and alt+ shift +esc that is native in windows is doing a pretty good job of functionality that I need. It's cycling through open windows, in order that they were open. Only downside is that if you have minimized windows it will select them and they will stay minimized, it will not put them to front. I was tripped by this until I figure it out. One shortcut is for moving left and other right, you just have to open windows in order in which you would like to cycle through.
So.. alt+esc and alt+ shift+esc are working, but they are so hard to press, especially if you want to do that fast. Along came autohotkey, I tested this code and it's working flawlessly.
!w::
sendInput {alt down}{esc}{alt up}
return
!e::
sendInput {alt down}{shift down}{esc}{alt up}{shift up}
return

AutoHotkey background clicking and typing

I'm trying to use AutoHotkey to do some background clicking and typing while I'm doing other stuff in the foreground.
I've gotten used to Send but I haven't figured out how ControlSend works yet. Can anyone give me an example using something simple like MSPaint in the background and changing the color of paint.
Is this even possible to do? I have a script currently that pulls from daily Excel report, assigns each row a variable and punches it into another program, but I need it to click and type some canned messages as well.
The first question should probably be why use mouse control? Keyboard control is sooo much easier and often more reliable. What is it that you try to do with a mouseclick that can't be done through keyboard commands?
Also mouse clicks normally activate the hidden app.
OK you wanted an example...
+Home:: ;Shift + Home = stop/start radio
SetControlDelay -1
ControlClick, x384 y143, Radioplayer
Return
Here i click on the play/pause button of a streaming radioplayer. The mouse coordinates are found with the aforementioned Windows Spy and the title of the browser (you might have to use SetTitleMatchMode). Why not look at the AutoHotKey Command list and check out the examples there....
Let me know if this is (roughly) what you are looking for....
SendMode Input ; Recommended for new scripts
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 2 ; A window's title can contain the text anywhere
!F1:: ; Press Alt F1 to execute
IfWinExist, Microsoft Excel
{
ControlSend ,, 1000{Enter}{Down}, Microsoft Excel
}
return
Here is some code I used to first record the mouse position (relative mouse position did not work since the window was tiled and the click position could be changed by moving the tiles). Immediately after that I click the mouse at the recorded position (and redo this later on in the code as well).
SplashTextOn, 200, 100, Script Preparations, Please Click on the Person `"Search Term`" link (1) in SAP. ; Show new instructions to the user
WinMove, Script Preparations,, (A_ScreenWidth/2)-150, (A_ScreenHeight/2)-200 ; Move the text instructions window with the name "Script Preparations" 150 pixels right of the center of the screen and 200 pixels up SoundBeep 600, 300 ; Wake up user
; From here on the left mouse button will temporarily be disabled
Hotkey, LButton, on ; Turn Left Mouse Button OFF, to capture the Mouse click
KeyWait, LButton, D ; Wait for LeftMouseButton click Down
MouseGetPos, xposS ,yposS ; Store the position where the mouse was clicked (Search Box)
MouseClick, left, %xposS% ,%yposS% ; Perform the mouse click on the captured mouse location
Hope this helps.
In your situation, I assume that you only need to determine the mouse position with Window Spy.