[Resolved] My Scroll Lock toggle is inconsistent and only works the first couple times. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

[Resolved] My Scroll Lock toggle is inconsistent and only works the first couple times.

13 Jun 2017, 10:46

Hi guys,

I'm trying to repurpose my Scroll Lock to toggle my lights and monitors in my office. When Scroll Lock is on, I want my lights and monitors to switch off (the LED indicator will be nice to see in the dark).

Here is the code, I appreciate any help.

Code: Select all

; stop AHK by pressing the PAUSE key:
; http://www.autohotkey.com/docs/commands/Suspend.htm
#NoEnv
SendMode Input 
#SingleInstance ignore
#persistent
#r::Reload
#NoTrayIcon

ScrollLock:
~ScrollLock::
If GetKeyState( "ScrollLock", "T" )

 {
SoundPlay, C:\Users\hayes\Storage\Tech Stuff\Butler\immediately_sir.wav

    run "https://autoremotejoaomgcd.appspot.com/url-to-turn-off-lights"
    sleep, 1000 ;
    send, ^w ;
    run "C:\Users\hayes\Storage\Tech Stuff\nircmd.exe" cmdwait 1000 monitor off"

SoundPlay, C:\Users\hayes\Storage\Tech Stuff\Notifications\mechanical-switch.wav
 }

 Else

 {

 SoundPlay, C:\Users\hayes\Storage\Tech Stuff\Notifications\mechanical-switch.wav

 run "https://autoremotejoaomgcd.appspot.com/url-to-turn-on"
    sleep, 3000 ;
    send, ^w ;

SoundPlay, C:\Users\hayes\Storage\Tech Stuff\Butler\there_we_are.wav
 }

Return
Last edited by robhayes on 13 Jun 2017, 11:29, edited 1 time in total.
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: My Scroll Lock toggle is inconsistent and only works the first couple times.

13 Jun 2017, 11:00

A simple fix easy to test would be to change the hotkey from ~ScrollLock to $ScrollLock. Use Send to send ScrollLock so that the system updates. I would give a 1/4 second delay (Sleep, 250) then do the GetKeyState() test. What I am thinking is when using the tilde it may be a timing issue. The state may change before or after the GetKeyState() test. But I am just guessing. But it is way easy to test my theory. :)
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

Re: My Scroll Lock toggle is inconsistent and only works the first couple times.

13 Jun 2017, 11:09

Thanks for the reply, MilesAhead. I hacked this script together based on another I found.

I do not understand what changing ~ScrollLock to $ScrollLock does, but I will give it a try.

Can you please elaborate on "use Send to send ScrollLock"? What do I need to change, and where?

I will add the sleep before GetKeyState() like you suggested.

edit: changes did not work.
robhayes
Posts: 19
Joined: 28 Dec 2015, 15:35

Re: My Scroll Lock toggle is inconsistent and only works the first couple times.  Topic is solved

13 Jun 2017, 11:22

So, I discovered it only works when Chrome is in the foreground. What gives?

edit: it was some other scripts I had on top of this one. I made a seperate .ahk file and it works fine. Case closed.
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: My Scroll Lock toggle is inconsistent and only works the first couple times.

13 Jun 2017, 15:22

robhayes wrote:So, I discovered it only works when Chrome is in the foreground. What gives?

edit: it was some other scripts I had on top of this one. I made a seperate .ahk file and it works fine. Case closed.
It is easy to leave a #IfWinActive hanging effecting all code below it. That is why many ahk coders get in the habit of putting an #IfWinActive with nothing after it on the line after the last Return in the hotkey handler, to set it back to all windows. In fact I should make a macro to do that myself. :)
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead

Return to “Ask For Help”

Who is online

Users browsing this forum: levav and 52 guests