GUI Slider to Send Keystroke to Application?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

GUI Slider to Send Keystroke to Application?

03 Dec 2021, 19:04

I did some searching which led me to understand that AutoHotkey will let you run routines by manipulating various GUI options. One of the GUIs is a slider. I was wondering if AutoHotkey was able to do something like this:

Whenever my music/audiobook player is the active window, draw a slider on the screen. The slider should look like:

1.00x ---1.10x ---1.20x ---1.30x and so on in increments of .10 all the way up to 2.00x

The script would be for changing the playback speed in the audio player when I move the slider.

So the code might be:

Code: Select all

#IfWinActive ahk_exe MusicBee.exe
; insert the proper command to GUI Add a slider bar.
SliderPosition1.00x:
ControlSend,, {=}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe ; = is the hotkey to set the tempo back to its default level
Return

SliderPosition1.10x: 
ControlSend,, {=}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
Sleep, 200
ControlSend,, {]}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe ; = to set the tempo back to 1.00, ] to increase the tempo by .10.
Return

SliderPosition1.20x: 
ControlSend,, {=}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
Sleep, 200
ControlSend,, {]}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe ; = to set the tempo back to 1.00, ] to increase the tempo by .10.
Sleep 200
ControlSend,, {]}, ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe ; = to set the tempo back to 1.00, ] to increase the tempo by .10., for a second time, bringing the playback speed up to 1.20x.
Return
The idea makes sense, yes? Am I on the right track with the slider for this purpose or am I way off base? Ideally, it would be great if I could grab the slider in a window and drag the slider window around. Additionally, it might be helpful if there was a fair bit of space between each slider position because my hands shake due to a disability.

Any help is always appreciated. Thank you so much for your time.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

03 Dec 2021, 20:46

Here are a few ideas.

Code: Select all

winTitle = ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
x := A_ScreenWidth - 600, y := A_ScreenHeight - 200
Gui, +AlwaysOnTop +LastFound
Gui, Font, s20 w700
Gui, Color, FFFFC2
Gui, Add, Text, w335 Center Section vsVal, 1.00x
Gui, Font, s12 w400
Gui, Add, Text, vt1, 1.00x
Gui, Add, Text, vt2, 2.00x
Gui, Add, Slider, xm+40 ys+40 w250 Range0-10 Buddy1t1 Buddy2t2 TickInterval AltSubmit gAdjust vslide, 0
WinSet, Transparent, 200
Loop {
 WinWait, %winTitle%
 Gui, % "+Owner" WinExist()
 Gui, Show, x%x% y%y% h100 NoActivate, Playback speed
 WinWaitClose
 Gui, Hide
}
Adjust:
Gui, Submit, NoHide
GuiControl,, sVal, % Format("{:.2f}x", Round(slide / 10 + 1, 1))
If (A_GuiEvent != "Normal")
 Return
SoundBeep, 1900
ControlSend,, {=}, %winTitle%
ControlSend,, {] %slide%}, %winTitle%
Return
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

04 Dec 2021, 20:28

Eternally grateful. Unfortunately, when I inserted the code into a script file, no slider materialized while my audiobook player was playing. No slider appeared on my screen at any point.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

04 Dec 2021, 20:44

You used this exact script, with no other code? If true, then you can post a screenshot of your Window Spy.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

05 Dec 2021, 16:33

Hi. I pasted your code at the top of the script file you helped me with previously, just like this:

Code: Select all

SetTitleMatchMode 2



XButton1::
XButton2::Return



winTitle = ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
x := A_ScreenWidth - 600, y := A_ScreenHeight - 200
Gui, +AlwaysOnTop +LastFound
Gui, Font, s20 w700
Gui, Color, FFFFC2
Gui, Add, Text, w335 Center Section vsVal, 1.00x
Gui, Font, s12 w400
Gui, Add, Text, vt1, 1.00x
Gui, Add, Text, vt2, 2.00x
Gui, Add, Slider, xm+40 ys+40 w250 Range0-10 Buddy1t1 Buddy2t2 TickInterval AltSubmit gAdjust vslide, 0
WinSet, Transparent, 200
Loop {
 WinWait, %winTitle%
 Gui, % "+Owner" WinExist()
 Gui, Show, x%x% y%y% h100 NoActivate, Playback speed
 WinWaitClose
 Gui, Hide
}
Adjust:
Gui, Submit, NoHide
GuiControl,, sVal, % Format("{:.2f}x", Round(slide / 10 + 1, 1))
If (A_GuiEvent != "Normal")
 Return
SoundBeep, 1900
ControlSend,, {=}, %winTitle%
ControlSend,, {] %slide%}, %winTitle%
Return
Here is the content of window spy when I click on my audiobook player, good sir:
Listening-to-audiobook-title - Artist - - MusicBee
ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1
ahk_exe MusicBee.exe
ahk_pid 16060
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

05 Dec 2021, 16:53

You violated the auto-execute section. Before you change the script, why not try exactly what is posted? It will help you determine whether that can form a starting point for what you want to do.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

05 Dec 2021, 22:46

I did not intend to change anything. Let me copy and paste again.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

05 Dec 2021, 23:09

So when I took the code out of the script file where I have all the actions relating to my mouse, and I ran your code by itself, then it worked like a charm! Would it be permissible to make a couple of requests for alteration if you are not too busy?

Thank you thank you thank you thank you thank you thank you thank you thank you thank you.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

06 Dec 2021, 04:48

Feel free to ask. If you have code that needs to run when your script first runs, put it at the top of your script, above your hotkey definitions.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

10 Dec 2021, 00:13

Hello there. I appreciate your patience in my response, seeing as I was helping my dad set up a shiny new chrome book, which of course the poor fellow misplaced half the passwords for. But we figured most of it out!

Your script has been working wonders. Regarding the alterations, the first is that I only need the speed control panel to show when the music player window is active. (When I click on other applications, the speed control remains, and the minimized speed control window, while helpful, does get in the way a little bit tucked in the lower-left corner. So it would be great if the speed control window would just disappear when the audio player is not active. I also would like the speed control to automatically open when the music player opens and close when the player closes, which it seems to do generally.

Secondly, I would appreciate it so much if the speed control panel opened in a particular position. To that end, I have included a link to a screenshot of window spy with the mouse cursor in the correct spot for the speed control window to open: https://drive.google.com/file/d/1AhXgRoec5zuvaYRdPhw6BYXYupYnv8po/view?usp=sharing

I feel like I'm missing something but it's not coming to mind right now. Anyway, I hope you have a good night.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

10 Dec 2021, 06:37

Code: Select all

winTitle = ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
x := 1319, y := 591 ; GUI uses screen coordinates
Gui, +AlwaysOnTop +LastFound
Gui, Font, s20 w700
Gui, Color, FFFFC2
Gui, Add, Text, w335 Center Section vsVal, 1.00x
Gui, Font, s12 w400
Gui, Add, Text, vt1, 1.00x
Gui, Add, Text, vt2, 2.00x
Gui, Add, Slider, xm+40 ys+40 w250 Range0-10 Buddy1t1 Buddy2t2 TickInterval AltSubmit gAdjust vslide, 0
WinSet, Transparent, 200
Loop {
 WinWaitActive, %winTitle%
 Gui, % "+Owner" WinExist()
 Gui, Show, x%x% y%y% h100 NoActivate, Playback speed
 WinWaitNotActive   ; When MusicBee is no longer active,
 Gui, Hide          ;  hide the GUI
}
Adjust:
Gui, Submit, NoHide
GuiControl,, sVal, % Format("{:.2f}x", Round(slide / 10 + 1, 1))
If (A_GuiEvent != "Normal")
 Return
SoundBeep, 1900
ControlSend,, {=}, %winTitle%
ControlSend,, {] %slide%}, %winTitle%
Return
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

22 Dec 2021, 12:03

Hello there:

I have been away, listening to audiobooks, testing your awesome script. I have noticed a strange behavior, in that if I am listening to an audiobook with the music player open, when I switch to another window, such as a browser tab, the play speed panel persists. Is there any way to make it so the play speed panel only appears when Musicbee is active, and then goes away when any other window is open?

Thank you for your efforts. It has been fun being able to quickly switch playback speeds based on whatever audiobook I am currently listening to without having to go through a bunch of extra menus in the settings each time I swap back and forth between books. If I don't hear back from you (or anyone else on this forum) soon then I would like to wish anyone reading Happy Holidays!
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

22 Dec 2021, 12:30

That is what already happens at my end. Unfortunately, I did not reproduce your issue.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

23 Dec 2021, 03:52

At first, I could not reproduce the problem when I tried to trigger it again a few moments ago. It was working fine. Then the problem started again. I will try to experiment over the holidays, see if I can reliably replicate the issue, and report back after Christmas.
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

15 Jan 2022, 17:21

Hello! I am not sure if you'll see this but I hope your holidays and New Year's went well, provided you were celebrating this year.

Your script has been a lifesaver. And after some minor fiddling, I was able to get the playback speed window to disappear as intended. It turns out all I had to do was take the playback speed code out of my main mouse script. Instead, I have it running its own script with #NoTrayIcon to keep the playback speed control from taking up space in the system tray.

(It's a little strange that running the code in its own script solves the problem, especially since when I had the playback speed code with my other mouse AutoHotkey modifications in the combined mouse autohotkey script, I put the playback speed control right under set title match mode, so the playback speed was at the top of the script. Go figure.)

I was wondering, only if you had the time, would you consider altering the playback control script so that on either side of the 1.00x in the playback speed window there are Unicode fast forward and rewind buttons?

Then when you click on the fast forward button, it sends five consecutive keypresses of the period. This should move the audio track forward five minutes. When you click on the rewind button, it should send five consecutive keypresses of the comma, to move the trackback five minutes.

Again, only if you have time. You have already done more than enough to improve my listening experience. It was frustrating to have to readjust the tempo whenever I switched audiobooks by going through the labyrinthine audiobook player user interface menus since I can't easily press the keyboard hotkey.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

15 Jan 2022, 23:41

You may be able to adapt the following.

Code: Select all

winTitle = ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
x := 1360, y := 591 ; GUI uses screen coordinates
Gui, +AlwaysOnTop +LastFound
Gui, Color, FFFFC2
Gui, Font, s12 w400
Gui, Add, Button,       ym+5  w50                      , Rew
Gui, Font, s20 w700
Gui, Add, Text  , x+m   ym    w200 Center Section vsVal, 1.00x
Gui, Font, s12 w400
Gui, Add, Button, x+m   ym+5  w50                      , FF
Gui, Add, Text  ,                                 vt1  , 1.00x
Gui, Add, Text  ,                                 vt2  , 2.00x
Gui, Add, Slider, xm+40 ys+55 w250 Range0-10 Buddy1t1 Buddy2t2 TickInterval AltSubmit gAdjust vslide, 0
WinSet, Transparent, 200
Loop {
 WinWaitActive, %winTitle%
 Gui, % "+Owner" WinActive()
 Gui, Show, x%x% y%y% h100 NoActivate, Playback speed
 WinWaitNotActive   ; When MusicBee is no longer active,
 Gui, Hide          ;  hide the GUI
}
Adjust:
Gui, Submit, NoHide
GuiControl,, sVal, % Format("{:.2f}x", Round(slide / 10 + 1, 1))
If (A_GuiEvent != "Normal")
 Return
SoundBeep, 1900
ControlSend,, {=}        , %winTitle%
ControlSend,, {] %slide%}, %winTitle%
Return

ButtonFF:
ControlSend,, {. 5}      , %winTitle%
Return

ButtonRew:
ControlSend,, {, 5}      , %winTitle%
Return
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

19 Jan 2022, 13:37

I tried the code and unfortunately the forward and back buttons don't seem to do anything when clicked. I had to change the assigned hotkeys because of a conflict in the program but when I press the hotkeys on the keyboard the audiobook jumps backward and forward. But nothing happens when I click the buttons:

Code: Select all

winTitle = ahk_class WindowsForms10.Window.8.app.0.2bf8098_r7_ad1 ahk_exe MusicBee.exe
x := 1360, y := 591 ; GUI uses screen coordinates
Gui, +AlwaysOnTop +LastFound
Gui, Color, FFFFC2
Gui, Font, s12 w400
Gui, Add, Button,       ym+5  w50                      , REW
Gui, Font, s20 w700
Gui, Add, Text  , x+m   ym    w200 Center Section vsVal, 1.00x
Gui, Font, s12 w400
Gui, Add, Button, x+m   ym+5  w50                      , FF
Gui, Add, Text  ,                                 vt1  , 1.00x
Gui, Add, Text  ,                                 vt2  , 2.00x
Gui, Add, Slider, xm+40 ys+55 w250 Range0-10 Buddy1t1 Buddy2t2 TickInterval AltSubmit gAdjust vslide, 0
WinSet, Transparent, 200
Loop {
 WinWaitActive, %winTitle%
 Gui, % "+Owner" WinActive()
 Gui, Show, x%x% y%y% h100 NoActivate, Playback speed
 WinWaitNotActive   ; When MusicBee is no longer active,
 Gui, Hide          ;  hide the GUI
}
Adjust:
Gui, Submit, NoHide
GuiControl,, sVal, % Format("{:.2f}x", Round(slide / 10 + 1, 1))
If (A_GuiEvent != "Normal")
 Return
SoundBeep, 1900
ControlSend,, {=}        , %winTitle%
ControlSend,, {] %slide%}, %winTitle%
Return

ButtonFF:
ControlSend,, {'}      , %winTitle%
Return

ButtonRew:
ControlSend,, {;}      , %winTitle%
Return
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

19 Jan 2022, 19:28

I tried your exact script, and it worked in my test here. I'm not sure what might be different at your end. If your Window Spy shows a different WinTitle, then I would expect nothing to happen. The following one-liner should also work as a test.

Code: Select all

F3::ControlSend,, ', ahk_exe MusicBee.exe
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: GUI Slider to Send Keystroke to Application?

21 Jan 2022, 02:16

Thank you for your continued assistance – the test script worked as expected. Still no luck with the actual buttons, though.
User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: GUI Slider to Send Keystroke to Application?

21 Jan 2022, 06:26

Now that you have something that works, you can adjust your script. Use the exact ControlSend line that worked (no braces), and the WinTitle that worked (no class on the first line).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, skeerrt and 170 guests