GroupAdd (exclusions) not working as expected Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 06 May 2022, 10:28

I have the following script which I found on this forum though I added the "GroupAdd" instructions to the script myself.

Code: Select all

; This script will automatically capitalise the first character of a new sentence.
; Original script was by someone called "Laszlo" but was posted by "Benirons".
; Source: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=21858 (Second entry, poster name guest3456.)

#SingleInstance force
#NoEnv
Process Priority,,High
SetBatchLines -1

Menu, Tray, Icon, AutoCaps.ico

; Specify what programs the script will be active in.
GroupAdd, AutoCaps, ahk_exe firefox.exe
GroupAdd, AutoCaps, ahk_exe betterbird.exe
GroupAdd, AutoCaps, ahk_exe thunderbird.exe
GroupAdd, AutoCaps, ahk_exe treepadbiz.exe
; GroupAdd, AutoCaps, ahk_exe soffice.bin
; GroupAdd, AutoCaps, ahk_exe winword.exe
GroupAdd, AutoCaps, ahk_exe notepad.exe
GroupAdd, AutoCaps, ahk_exe wordpad.exe
GroupAdd, AutoCaps, ahk_exe stickies.exe
GroupAdd, AutoCaps, ahk_exe FSCapture.exe
#IfWinActive ahk_group AutoCaps
; Note: The #IfWinActive instruction also sets a context for all the code that follows.
; To terminate that context need to use #IfWinActive without parameters.


Loop {
   Input key, I L1 V,
(  Join
{ScrollLock}{CapsLock}{NumLock}{Esc}{BS}{PrintScreen}{Pause}{LControl}{RControl}
{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}
{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}
)
   If StrLen(ErrorLevel) > 3  ; NewInput, EndKey
      state =
   Else If InStr(".!?",key)   ; Sentence end
      state = 1
   Else If InStr("`t `n",key) ; White space
      state += (state = 1)    ; state1 -> state2
   Else {
      StringUpper key, key
      If state = 2            ; End-Space*-Letter
         Send {BS}{%key%}     ; Letter -> Upper case
      state =
   }
}

~LButton::
~RButton::
~MButton::
~WheelDown::
~WheelUp::
~XButton1::
~XButton2::State =
The problem I have is that even though I have not allowed the script to run in soffice.exe or in winword.exe the script actually does run in them. So what is the problem here? I don't want to the script to run in windows belonging to those two programs.

(This has me baffled as I have used the GroupAdd feature in another script and it works as expected: Programs not included in the GroupAdd definitions don't have the script running in them, which is what I want to achieve for the above script.)

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 06 May 2022, 10:36

#If applies only to hotkeys & hotstrings. See documentation. #If is not If.

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 06 May 2022, 11:56

No, I'm not going to get this. I have tried stuff using 'if' and am just tying myself in knots with frustration.

Does anyone know how I get this script to only work in specified programs/windows?

I've tried googling this too but all the advice is use #IfWinactive ... And so on. Driving me nuts with frustration.

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected  Topic is solved

Post by mikeyww » 06 May 2022, 12:41

An idea is below.

Code: Select all

GroupAdd, nocaps, ahk_exe WINWORD.exe
winTitle = ahk_group nocaps
Loop {
 WinWaitNotActive, %winTitle%
 SoundBeep, 1500
 SetTimer, Letters, -1
 WinWaitActive, %winTitle%
 SoundBeep, 1000
} 
Letters:
While !WinActive(winTitle) {
 Input, key, VL1, % "{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}"
                  . "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
                  . "{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}"
                  . "{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}"
 Switch
 { Case WinActive(winTitle)
      , StrLen(ErrorLevel) > 3: state =
   Case Instr(".!?"  , key)   : state = 1
   Case Instr("`t `n", key)   : state += (state = 1)
   Default                    : Send % state = 2 ? Format("`b{:U}", key) : ""
                                state =
 }
}
Return
#IfWinNotActive ahk_group nocaps
~LButton::
~RButton::
~MButton::
~WheelDown::
~WheelUp::
~XButton1::
~XButton2::
state =
Input
SoundBeep, 900
Return
#IfWinNotActive

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 06 May 2022, 13:46

Thanks very much Mike, that works! There is no way I would have stumble-stepped my way to that!

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 06 May 2022, 13:57

I imagine you are right. I had to futz around with it for a while. :) :thumbup:

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 07 May 2022, 08:39

Well last night I thought that it would work better for me if I could make a group of programs for ahk_exe inclusion (not ahk_exe exclusion) in this script. That meant I would have to reverse the logic of the script. Tried for two hours and couldn't do it so went to bed. Got up this morning and tried again — ten minutes I worked it out just fine. :D

Then I came back to this thread to mark the 'solution' as 'ticked'. Then, while doing that noticed that Mike had altered the code somewhat. Why the changes 'if' to 'switch'? What difference do the changes make?

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 07 May 2022, 08:40

Switch is merely a convenience. Take your pick.

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 07 May 2022, 08:45

Thank you again. :thumbup:

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 21 May 2022, 06:08

I am having a problem with this script:

Code: Select all

; This script will automatically capitalise the first character of a new sentence.
; It is a modification of a script supplied by "mikeyww" at https://www.autohotkey.com/boards/viewtopic.php?f=76&t=103709
; Basically I just reversed the logic of the script Mike supplied and got the GroupAdd to allow 'inclusions' not 'exclusions'.

#SingleInstance force
#NoEnv
Process Priority,,High
Menu, Tray, Icon, AutoCaps.ico

GroupAdd, AutoCaps, ahk_exe firefox.exe
GroupAdd, AutoCaps, ahk_exe betterbird.exe
GroupAdd, AutoCaps, ahk_exe thunderbird.exe
GroupAdd, AutoCaps, ahk_exe treepadbiz.exe
; GroupAdd, AutoCaps, ahk_exe soffice.bin
; GroupAdd, AutoCaps, ahk_exe winword.exe
GroupAdd, AutoCaps, ahk_exe notepad.exe
GroupAdd, AutoCaps, ahk_exe wordpad.exe
GroupAdd, AutoCaps, ahk_exe stickies.exe
GroupAdd, AutoCaps, ahk_exe FSCapture.exe
winTitle = ahk_group AutoCaps



Loop {
 WinWaitActive, %winTitle%
 ; SoundBeep, 1500
 SetTimer, Letters, -1
 WinWaitNotActive, %winTitle%
 ; SoundBeep, 1000
} 
Letters:
While WinActive(winTitle) {
 Input, key, VL1, % "{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}"
                  . "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
                  . "{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}"
                  . "{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}"
 Switch
 { Case !WinActive(winTitle)
      , StrLen(ErrorLevel) > 3: state =
   Case Instr(".!?"  , key)   : state = 1
   Case Instr("`t `n", key)   : state += (state = 1)
   Default                    : Send % state = 2 ? Format("`b{:U}", key) : ""
                                state =
 }
}
Return
#IfWinActive ahk_group nocaps
~LButton::
~RButton::
~MButton::
~WheelDown::
~WheelUp::
~XButton1::
~XButton2::
state =
Input
; SoundBeep, 900
Return
#IfWinActive
What happens is that it seems the script deactivates itself after a while if I haven't been entering text and hence have not been accessing the functionality of the script. I can't establish how long the interval is at which point the script stops functioning, certainly many, many minutes, maybe hours, . I haven't managed to establish a time frame for when or how it does this. I don't think I will be able to establish a time-frame.

When it does deactivate I have to click on the System Tray icon for the script and click "Reload this Script". Once I do that the script starts running again as expected.

I did think at one point that perhaps it was me sending the computer to sleep, and maybe when the PC wakes again the script isn't functioning. However, on doing several tests for this sleeping the computer is not at the root of the problem.

So, any ideas on how to fix this?

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 21 May 2022, 06:20

If you restore the beeps, or display message boxes, you can understand what is happening.

Input:
If this and the other parameters are omitted, any Input in progress in another thread is terminated and its ErrorLevel is set to the word NewInput. By contrast, the ErrorLevel of the current command will be set to 0 if it terminated a prior Input, or 1 if there was no prior Input to terminate.
After your first Input statement, you can also add code to display the value of the key as well as the ErrorLevel.

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 22 May 2022, 08:35

Thanks Mike but after trying to research what you are saying I am none the wiser — getting old now and it shows at times.

However, I think I am one step forward on working out what is going wrong, but I am clueless as to how to fix it. This morning I spent a couple of hours just typing and see if I could find whatever is triggering the script to not capitalise the first word of a sentence. Then I made a discovery that suggests that this has nothing at all to do with the script 'switching itself off'. The problem is arising from the use of the keyboard by the user. What happens is this:

(1) I type a sentence and terminate the sentence with a period and a space character (i.e. I am now ready to enter the first letter of a new sentence and the script should automatically capitalise that first character).

(2) Now before entering that first character of a new sentence I inadvertently press down, and immediately release, one of the Shift keys.

(3) In follow up to that I then type in the first character of the new sentence but it does not get capitalised by the script i.e. the script fails.

(4) For the 'Input' line of the script the following is done (I do not understand what is being done only that the problem lies here):

Code: Select all

While WinActive(winTitle) {
 Input, key, VL1, % "{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}"
                  . "{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}"
                  . "{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}"
                  . "{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}"
 Switch
 { Case !WinActive(winTitle)
      , StrLen(ErrorLevel) > 3: state =
   Case Instr(".!?"  , key)   : state = 1
   Case Instr("`t `n", key)   : state += (state = 1)
   Default                    : Send % state = 2 ? Format("`b{:U}", key) : ""
                                state =
 }
}
Return
(5) If from the code given above I remove the {LShift}{RShift} keys, save and then reload the script and then repeat the procedure given above (1)–(3) then the script functions just fine and the first word of the new sentence does get capitalised (even though I had pressed and released the shift key before typing the first character of the new sentence).

(6) I tried the same experiment by pressing and immediately releasing some of the other {keys} statements e.g. {Del}, {Ins}, {CapsLock} etc. The result is the same as pressing and immediately releasing the shift key — the script fails to capitalise the first character of the new sentence. But I have no understanding as to why.

In any case, my original theory that the script was failing because of some 'time factor' seems to have been wrong. The script is failing because of user interaction with the keyboard before typing the first character of a new sentence. Well that's at the keyboard level of explanation. At the script level it would seem that there is something wrong with 'Switch'/'Case' part of the script that is causing it to not capitalise the first character of a sentence if any of the 'Input' keys had been pressed (and released) prior to entering the first character of the new sentence. So does anyone know how to fix this?

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 22 May 2022, 09:02

The script is doing what it is programmed to do, according to the EndKeys.
A list of zero or more keys, any one of which terminates the Input when pressed
I think you'll want to read :arrow: Input, and then adjust your Input statement according to what you want to happen. You can delete EndKeys in the statement, handle them as noted through the ErrorLevel value returned, etc. If you do not want any EndKeys, then you do not need to use them.

User avatar
Buddha Nature
Posts: 34
Joined: 13 Feb 2022, 06:42

Re: GroupAdd (exclusions) not working as expected

Post by Buddha Nature » 22 May 2022, 10:22

mikeyww wrote:
22 May 2022, 09:02
You can delete EndKeys in the statement... If you do not want any EndKeys, then you do not need to use them.
Ah, a light-bulb switched on at that!

I was worried about just deleting some of the {keys} willy-nilly without knowing what I am doing. However I have now deleted several of them that I think amount to being problematic and things are working just fine.

Part of my problem is that I can/do touch-type and I'm having to teach myself not to automatically press a Shift key down when keying in the first character of a new sentence, as the script is meant to do that for me (metaphorically speaking).

Thanks very much Mike. After twenty minutes of testing with the {keys} deleted I think my problem with this is now fixed.

Thanks again. :thumbup:

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: GroupAdd (exclusions) not working as expected

Post by mikeyww » 22 May 2022, 10:24

I taught myself to press Shift to start a new sentence. It's been working really well so far! :lol:

Have fun.

Post Reply

Return to “Ask for Help (v1)”