Need help finding or making a script or hotkey

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Driver-480
Posts: 2
Joined: 29 Nov 2023, 13:59

Need help finding or making a script or hotkey

Post by Driver-480 » 11 Dec 2023, 12:40

I need a macro/hotkey or a script (or both? brand new to AutoHotkey so I am not sure of the terminology) for my ebook readers. I have Kindle app, Adobe Digital Editions, and another ebook reader app for the non-Kindle books. All of them will page to the next page by pushing the spacebar key.

What I need to do is to have a script or 2 which let you choose whatever window you want, and then press the spacebar about every 60 seconds, and for it to execute the macro or script even if that window is not the active one --- you know how sometimes something will pop up from another program and you have to take care of it but it really doesn't block your view of the window you did have focused/working-in. So you don't realize that the reader window, or game window or whatever, is still running even though it is not the 'primary' one, it's in the background now.

I did a search on the forum looking for something but there were so many posts that came up under the search term, and I know nothing about AutoHotkey so I don't know how to even integrate a script/hotkey if I find one. AutoHotkey installed 2 programs with shortcuts on install, Windows Spy and Dash, so I don't really even understand which one to use and how to go about adding something --- let alone writing something. And I don't understand the tutorial very well. I have not used automation programs much in the past. But I saw from some of the posts, mostly about games, that it seems possible to do and people were answering the posts --- so I am guessing you can do this script/hotkey. Apparently, a lot of games use the spacebar for something in the game a lot, lol

So, does anyone know of a script/hotkey already done out there that I could install, and maybe give me some simple instructions how to install it? Or maybe a guru could fix one up for me fairly quick. I would greatly appreciate it. Thanks

User avatar
kunkel321
Posts: 1234
Joined: 30 Nov 2015, 21:19

Re: Need help finding or making a script or hotkey

Post by kunkel321 » 14 Dec 2023, 11:36

Hi Driver, Welcome to the forums. I'm not 100% sure I understand what you're after; but maybe this?

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; ebook spacebar presser

Seconds := 2  ; <--- Change to 60, or whatever. 
^Space:: ; ctrl+space
{	Global targetWindow := WinActive("A")  ; Get the handle of the currently active window
	SetTimer(pressIt, Seconds * 1000)	
}

pressIt()
{	HotIfWinNotactive(targetWindow)
		WinActivate(targetWindow) ; Keep making the window active again.  
	; SoundBeep ; for debugging.
	Send("{Space}")
}

^Esc::ExitApp ; Ctrl+Esc: Totally kill script.
Note that I'm still learning too, so there might be better ways to do this, but this seems to work.
name := "ste(phen|ve) kunkel"

User avatar
kunkel321
Posts: 1234
Joined: 30 Nov 2015, 21:19

Re: Need help finding or making a script or hotkey

Post by kunkel321 » 15 Dec 2023, 11:42

I decided to have some fun and experiment. This one has some extra features. You can test it in Notepad... It'll just slowly type spaces into the window.

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; eBook spacebar presser
; Puts little countdown tooltip near top/left of reader window.
; Ctrl+Space = Start
; Ctrl+Esc = Totally end/kill script
; Following hotkeys only work after script has been run at least once.
; Esc = Stop, but keep script ready
; Up Arrow = Go faster
; Down Arrow = Go slower
; Countdown tooltip updates when you add/subtract seconds.

Seconds := 6  ; <--- Change to 60, or whatever. 
isRunning := 0

^Space:: { ; ctrl+space
	Global isRunning := 1
	Global targetWindow := WinActive("A")  ; Get the handle of the currently active window.
	startTimer()
	doToolTip()
}

^Esc::ExitApp ; Ctrl+Esc: Kill the script.

Esc::
Up::
Down::
{	If (isRunning = 1) { ; isRunning gets changed to '1' when the script is started.  
		Global Seconds
		;msgbox A_ThisHotkey
		Switch A_ThisHotkey {
			Case "Esc" : Reload ; Reload the script.
			Case "Up" : Seconds-- ;  Press faster
			Case "Down" : Seconds++ ; Add seconds, ie press slower
		}
		Sleep 100
		startTimer() ; Start timer again so that new Seconds value is used. 
	}
}

startTimer() {
	SetTimer(pressIt, Seconds * 1000) ; Repeatedly call pressIt() function.
}

pressIt() {
	WinActivate(targetWindow) ; Keep making the window active again.  
	Send("{Space}") ; Simulate press space bar
	doToolTip()
}

doToolTip() {
	Loop Seconds {
		countDown := Seconds - A_Index	
		ToolTip(countDown " of " Seconds, 200, 5)
		Sleep 1000
	}
}

Last edited by kunkel321 on 16 Dec 2023, 10:22, edited 1 time in total.
name := "ste(phen|ve) kunkel"

Driver-480
Posts: 2
Joined: 29 Nov 2023, 13:59

Re: Need help finding or making a script or hotkey

Post by Driver-480 » 15 Dec 2023, 17:49

Hi Kunkel321,

Thank you so much for doing those 2 scripts for me!! I am very inexperienced with autoHotkey so I don't know for sure by looking at the code, but it sure looks like it would do exactly what I need it to do. All I need it to do is to always stay 'hooked' to the reader app I start it on (however you start it on something, brand new like I said haha) and hit the spacebar to turn my pages for me automatically. So what are the extra features in the 2nd script, and does it still just run a timer and hit the spacebar every so often in the same manner as the first script? Or did you change the code for hitting the spacebar every so often in the 2nd script?

What autoHotkey program do I activate to import and run this script? As I said in my OP, I am new to autoHotkey and the 2 different programs, Windows Spy and Dash confuse me a bit. And I am wondering if either one of those are what you actually need to be running to execute the script. I am brand new back to windows, and I am still getting adjusted to Win 10 since I have been on Linux so many years now and I used Win 7 before I switched, so if I was supposed to use another program to load and run the script I wouldn't even know where to look for it. Windows Spy and Dash are the only things autoHotkey pinned to my start menu when it installed itself.

Again, thanks for doing those scripts for me, they will be quite useful if they work like I hope!!! Best Regards, Steve

User avatar
ElVerdaderoJuan
Posts: 8
Joined: 14 Dec 2023, 15:04
Contact:

Re: Need help finding or making a script or hotkey

Post by ElVerdaderoJuan » 15 Dec 2023, 18:41

Search the documentation for the command ControlSend. Allows you to send keystrokes to windows or controls in a window without activating it.

:arrow: This example gets the ID of the active window and saves it to MyWindowID:

Code: Select all

MyWindowID := WinGetID("A")
:arrow: You can create hotkeys this way if you want to disable them without using variables:

Code: Select all

Hotkey "Space", MyFunction, "On" ; or "Off"

MyFunction(*)
{
    MsgBox "you pressed Space"
}
:arrow: Simulate space key in the selected window with ControlSend:

Code: Select all

ControlSend "{Space}",, "ahk_id " MyWindowID

User avatar
kunkel321
Posts: 1234
Joined: 30 Nov 2015, 21:19

Re: Need help finding or making a script or hotkey

Post by kunkel321 » 16 Dec 2023, 11:16

I like Juan's recommendation to use ControlSend. That causes the space-press to always be sent to the reader, even if another app is open and focused. In the below code, I have added that and commented out what I had before.

Do note, @ElVerdaderoJuan, the feature to 'stop' hotkey won't stop the code if it is already running. It only disables the hotkey. So pressing the hotkey will have no effect, if it is off.

@Driver-480, if you are seeing the Dashboard and WinSpy, then you probably have correctly installed AutoHotKey v2. The default behavior is to run a script when it is double-clicked. So in the below code window, click on Download. It will download "SpacePresser.ahk" to your Windows Download folder. Leave the file there, or put it about anywhere. Double-click it. It won't appear to do anything, but you can confirm that it is running by opening the Windows System Tray and looking for the little green "H" icon. Hover over it to see the name of the script.

To run the tool, open Kindle reader and make sure it's the active window (by clicking on it). Then press the hotkey (Ctrl+Space). You'll know the tool is running, because the tiny tooltip near the top/left of the window. The tooltip is a countdown timer. It also tells you the duration of time between page turns. For example, I have the default set for 10 seconds. (You can change it near the top of the code: Seconds := 10.) The tooltip will start at "10 of 10." It will get to "0 of 10" then press space. The up/down arrows change the duration. Up = faster, which means a shorter duration. So if you press the up arrow three times, the tooltip will (should) show "7 of 7." In your first post, you mentioned 60 seconds, so set the default to that. I.e. Seconds := 60.

In AHK code, you might guess that lines of code which start with a semicolon are comments. I put comments in the code with directions and tips, so check those out. Lines of computer code can also be "commented-out" by preceding them with a semicolon. I commented-out several lines of code, when I replaced them with Juan's recommendations.

Also, yeah, the three scripts I've posted all do the same thing. The first one just didn't have as many features. :geek:

Code: Select all

#SingleInstance
#Requires AutoHotkey v2+
; eBook spacebar presser tool from:
; https://www.autohotkey.com/boards/viewtopic.php?f=82&t=123934
; Click the reader window so it's active, then press Ctrl+Space to start the tool.
; It puts little countdown tooltip near top/left of reader window. 
; Pressing Ctrl+Esc = Totally end/kill script.
; The following hotkeys only work after script has been run at least once.
; Esc = Stop, but keep script ready (really, it reloads the script to default).
; Up Arrow = Go faster. 
; Down Arrow = Go slower
; Countdown tooltip updates when you add/subtract seconds.  This let's you spy how many seconds the durration is.  

Seconds := 10 ; <--- This is the time between presses/page turns.  Change to 60, or whatever.  Note that even if you use the arrows to change the durration, the script will always restart at this default.
isRunning := 0 ; Don't change value of isRunning.

^Space:: { ; ctrl+space
	Global isRunning := 1
	Global targetWindow := WinGetID("A")
	;Global targetWindow := WinActive("A")  ; Get the handle of the currently active window.
	startTimer()
	doToolTip()
}

^Esc::ExitApp ; Ctrl+Esc: Kill the script.

Esc::
Up::
Down::
{	If (isRunning = 1) { ; isRunning gets changed to '1' when the script is started.  
		Global Seconds
		;msgbox A_ThisHotkey
		Switch A_ThisHotkey { ; All three hotkeys activate this code block.  A_ThisHotKey tells us which was pressed. 
			Case "Esc" : Reload ; Reload the script.
			Case "Up" : Seconds-- ;  Press faster
			Case "Down" : Seconds++ ; Add seconds, ie press slower
		}
		Sleep 100
		startTimer() ; Start timer again so that new Seconds value is used. 
	}
}

startTimer() {
	SetTimer(pressIt, Seconds * 1000) ; Repeatedly call pressIt() function.
}

pressIt() {
	; WinActivate(targetWindow) ; Keep making the window active again.  
	; Send("{Space}") ; Simulate press space bar
	ControlSend "{Space}",, "ahk_id " targetWindow ; <-- Juan recommended. 
	doToolTip()
}

doToolTip() {
	Loop Seconds {
		countDown := Seconds - A_Index	
		ToolTip(countDown " of " Seconds, 200, 5) ; <--- Change location of tooltip with last two parameters.
		Sleep 1000
	}
}
EDIT: Another tip: Once you've downloaded and saved SpacePresser.ahk, you can right-click the file in your file explorer. The Windows context menu will have an option for "Edit script." You'll need to do that in order to change that Seconds default value to 60.
name := "ste(phen|ve) kunkel"

Post Reply

Return to “Ask for Help (v2)”