Need help with solving two problem in binding script Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Boopyboop
Posts: 5
Joined: 21 Feb 2018, 08:12

Need help with solving two problem in binding script

21 Feb 2018, 08:56

Hello! In advance I apologize for my bad English.
I have this code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

 ;;;Core Code
#If
WatchProg_1 = ahk_exe Notepad++.exe
WatchProg_2 = ahk_exe WINWORD.exe

#Persistent
SetTimer, WatchMouse, 10
Return

WatchMouse:
 ;;Key Remapper for Notepad++
#If WinActive(WatchProg_1) 
	WinGet, WinID_1, ID, % WatchProg_1
	MouseGetPos,,, CurID_1,, 1
w::
	If (CurID_1 = WinID_1) {
	Send Any a text (%CurID_1% = %WinID_1%)
	} Else {
	Send Any b text (%CurID_1% = %WinID_1%)
	}
Return

 ;;Key Remapper for Doc
#If WinActive(WatchProg_2)
	WinGet, WinID_2, ID, % WatchProg_2
	MouseGetPos,,, CurID_2,, 1
w::
	If (CurID_2 = WinID_2) {
	Send Any c text (%CurID_2% = %WinID_2%)
	} Else {
	Send Any d text (%CurID_2% = %WinID_2%)
	}
Return
 ;;;


 ;;;Suspend hotkeys
#If
Scrolllock::
	Send {Scrolllock}
	Suspend
	Return
 ;;;
I'm new to ahk, I tried to figure it out myself, but in the end I ran into two problems, in which I need help:
1)If I move the code from Suspend hotkeys section above Core Code section, then binding on W key stops working. How I can fix it without move Suspend hotkeys section below Core Code section?
2)Code in the Key Remapper for Doc section doesn't working, why and how solve this problem? The code in Key Remapper for Notepad++ section is working fine (As I understand it, the problem is that CurID_2 and WinID_2 doesn't get values, but I don't understand why)

I'm more than sure that I make very stupid errors in the code, but I can't find them very long time, please help.
Thanks for reading.
User avatar
evilC
Posts: 4372
Joined: 27 Feb 2014, 12:30

Re: Need help with solving two problem in binding script

21 Feb 2018, 11:36

You are using #if wrong.

Code: Select all

#If WinActive(WatchProg_2)
	WinGet, WinID_2, ID, % WatchProg_2
	MouseGetPos,,, CurID_2,, 1
#if is purely to turn on/off hotkeys due to context.
Code inside an #if block does not run when the condition is true, it just affects whether a hotkey declaration inside the #if block is active or not.
Boopyboop
Posts: 5
Joined: 21 Feb 2018, 08:12

Re: Need help with solving two problem in binding script

21 Feb 2018, 15:25

evilC wrote:You are using #if wrong.

Code: Select all

#If WinActive(WatchProg_2)
	WinGet, WinID_2, ID, % WatchProg_2
	MouseGetPos,,, CurID_2,, 1
#if is purely to turn on/off hotkeys due to context.
Code inside an #if block does not run when the condition is true, it just affects whether a hotkey declaration inside the #if block is active or not.
Hello again. I tried fix my code,

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


 ;;;Core Code
#If
GroupAdd, WatchProg_1, ahk_exe Notepad++.exe
GroupAdd, WatchProg_2, ahk_exe WINWORD.exe

#Persistent
SetTimer, WatchMouse, 10
Return

WatchMouse:
	WinGet, WinID_1, ID, ahk_group WatchProg_1
	MouseGetPos,,, CurID_1,, 1

	WinGet, WinID_2, ID, ahk_group WatchProg_2
	MouseGetPos,,, CurID_2,, 1
Return
 ;;;

 ;;Key Remapper for Notepad++
#IfWinActive, ahk_group WatchProg_1
w::
	If (CurID_1 = WinID_1) {
	Send Any a text (%CurID_1% = %WinID_1%)
	} Else {
	Send Any b text (%CurID_1% = %WinID_1%)
	}
Return

 ;;Key Remapper for Doc
#IfWinActive, ahk_group WatchProg_2
w::
	If (CurID_2 = WinID_2) {
	Send Any c text (%CurID_2% = %WinID_2%)
	} Else {
	Send Any d text (%CurID_2% = %WinID_2%)
	}
Return
 ;;;


 ;;;Suspend hotkeys
#If
Scrolllock::
	Send {Scrolllock}
	Pause
	Suspend
	Return
 ;;;
Problem №2 is solved now, thanks for help. But can you explain about Problem №1?
User avatar
evilC
Posts: 4372
Joined: 27 Feb 2014, 12:30

Re: Need help with solving two problem in binding script

22 Feb 2018, 05:06

You are still using #if for random stuff I have no clue why you are using it.

Why did you use #if for these bits of code?

Code: Select all

#If
GroupAdd, WatchProg_1, ahk_exe Notepad++.exe
GroupAdd, WatchProg_2, ahk_exe WINWORD.exe

Code: Select all

#If
Scrolllock::
Boopyboop
Posts: 5
Joined: 21 Feb 2018, 08:12

Re: Need help with solving two problem in binding script

22 Feb 2018, 08:20

evilC wrote:You are still using #if for random stuff I have no clue why you are using it.

Why did you use #if for these bits of code?

Code: Select all

#If
GroupAdd, WatchProg_1, ahk_exe Notepad++.exe
GroupAdd, WatchProg_2, ahk_exe WINWORD.exe

Code: Select all

#If
Scrolllock::
Isn't easy for me understand this, sorry for my stupidity. First If is unnecessary, but without second If Scrollllock will stop working anywhere except programs WatchProg_1 and WatchProg_2

P.S. I have new question: Any binds stop working when active window of TaskManager, Don't Starve Together and Dark Souls 3, how I can fix it?
User avatar
evilC
Posts: 4372
Joined: 27 Feb 2014, 12:30

Re: Need help with solving two problem in binding script

22 Feb 2018, 08:58

If games don't see the input AHK sends, then you probably need to run the script as admin.

The ONLY thing inside an #if statement should be hotkeys.

Code: Select all

#if
GroupAdd, WatchProg_1, ahk_exe Notepad++.exe
This is totally not valid.
1) #if on it's own CLOSES an #if block. This line is before any other #if statements, so makes no sense.
2) If it IS meant to be "inside" an #if block, you cannot just put code in an #if block like that.

CORRECT usages of #if

Turning on/off hotkeys depending on value of a variable:

Code: Select all

f12_on := 0

F11::
	F12_on := !F12_on	; Toggle value of F12_on between 1 and 0
	return

#if F12_on		; Only enable hotkeys inside this block if the variable F12_on is 1
; <-- YOU CANNOT PUT NORMAL CODE HERE!! HOTKEYS ONLY!
F12::
	msgbox
	if (something){
		; You CAN put normal code inside the hotkey
	}
	return
#if			; close the #if block

F10::	; This hotkey is not affected by the #if block
	msgbox
	return
Turning on/off hotkeys depending on active window:

Code: Select all

#ifWinActive, blah
; <-- YOU CANNOT PUT NORMAL CODE HERE!!
F12::
	msgbox
	return
#ifWinActive ; close the #if block
Boopyboop
Posts: 5
Joined: 21 Feb 2018, 08:12

Re: Need help with solving two problem in binding script

22 Feb 2018, 18:00

evilC wrote:If games don't see the input AHK sends, then you probably need to run the script as admin.

The ONLY thing inside an #if statement should be hotkeys.

Code: Select all

#if
GroupAdd, WatchProg_1, ahk_exe Notepad++.exe
This is totally not valid.
1) #if on it's own CLOSES an #if block. This line is before any other #if statements, so makes no sense.
2) If it IS meant to be "inside" an #if block, you cannot just put code in an #if block like that.

CORRECT usages of #if

Turning on/off hotkeys depending on value of a variable:

Code: Select all

f12_on := 0

F11::
	F12_on := !F12_on	; Toggle value of F12_on between 1 and 0
	return

#if F12_on		; Only enable hotkeys inside this block if the variable F12_on is 1
; <-- YOU CANNOT PUT NORMAL CODE HERE!! HOTKEYS ONLY!
F12::
	msgbox
	if (something){
		; You CAN put normal code inside the hotkey
	}
	return
#if			; close the #if block

F10::	; This hotkey is not affected by the #if block
	msgbox
	return
Turning on/off hotkeys depending on active window:

Code: Select all

#ifWinActive, blah
; <-- YOU CANNOT PUT NORMAL CODE HERE!!
F12::
	msgbox
	return
#ifWinActive ; close the #if block
Now is this correct code or I still doing something wrong?

Code: Select all

#NoEnv
SendMode Input
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%
Menu, Tray, Icon, Imageres.dll, 174
;#Persistent

 ;----------------------------------Run as Administrator
full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}
 ;----------------------------------Run as Administrator

  ;;;Core Code
GroupAdd, WatchProg_1, ahk_exe vagante.exe
GroupAdd, WatchProg_2, ahk_exe Notepad++.exe

SetTimer, WatchMouse, 10
Return

WatchMouse:
	MouseGetPos,,, CurID,, 1

	WinGet, WinID_1, ID, ahk_group WatchProg_1
	WinGet, WinID_2, ID, ahk_group WatchProg_2
Return
 ;;;

  ;;;Binding Code
 ;;Key Remapper for Vagante
#IfWinActive, ahk_group WatchProg_1
с::
	If (CurID = WinID_1) 
	{
	Send {r down}
	KeyWait, w
	Send {r up}
	}
Return
#IfWinActive

 ;;Key Remapper for Notepad++
#IfWinActive, ahk_group WatchProg_2
c::
	If (CurID = WinID_2) 
	{
	Send {r}
	}
Return
#IfWinActive
  ;;;


   ;--------------------; Settings for Useless Key ;--------------------;   
 ;;Launch TaskManager,		Clear
Launch_App1::
	Run, C:\Windows\System32\Taskmgr.exe
	Return

 ;;Launch SystemExplorer,		Shift
+Launch_App1::
	Run, D:\Game Files\Windows Programs\System Explorer\System Explorer\SystemExplorer.exe
	Return

 ;;Window AlwaysOnTop,		Ctrl+Alt
^!Launch_App1::
	WinGetTitle, currentWindow, A
	IfWinExist %currentWindow%
	{
		WinSet, AlwaysOnTop, toggle, %currentWindow%
	}
	Return

 ;;Window Transparent,		Alt
!Launch_App1::
	WinGetTitle, currentWindow, A
	IfWinExist %currentWindow%
	{
		WinGet, Transparent, Transparent, %currentWindow% 
		If (Transparent = 255)
		{
			WinSet, Trans, 180, %currentWindow%
		}
		else
		{
			WinSet, Trans, 255, %currentWindow%
		}
	}
	Return

 ;;Window Kill,		Shift+Alt
+!Launch_App1::
	WinGetTitle, currentWindow, A
	IfWinExist %currentWindow%
	{
		WinKill %currentWindow%
	}
	Return

 ;;Standart Action,		Ctrl
^Launch_App1::
	Send {Launch_App1}
	Return
   ;;;


 ;;;Suspend hotkeys
Scrolllock::
	Send {Scrolllock}
	Suspend
	Pause
	Return
 ;;;
P.S. Run the script as admin is solved my problem, thanks.
User avatar
evilC
Posts: 4372
Joined: 27 Feb 2014, 12:30

Re: Need help with solving two problem in binding script  Topic is solved

22 Feb 2018, 19:32

Looks a lot better.
About the only issue I see is that this code is a little risky:

Code: Select all

с::
	If (CurID = WinID_1) 
	{
	Send {r down}
	KeyWait, w   ; <--- pausing mid-hotkey like this *can* cause problems
	Send {r up}
	}
Return
Be aware that this technique (waiting in a hotkey indefinitely) causes issues if you try to do it with more than one hotkey.
Splitting the code into a press hotkey and a release hotkey is better:

Code: Select all

с::
	If (CurID = WinID_1) 
	{
	Send {r down}
	}
	return

w up::
	If (CurID = WinID_1) 
	{
	Send {r up}
	}
	return
Boopyboop
Posts: 5
Joined: 21 Feb 2018, 08:12

Re: Need help with solving two problem in binding script

23 Feb 2018, 04:29

evilC wrote:Looks a lot better.
About the only issue I see is that this code is a little risky:

Code: Select all

с::
	If (CurID = WinID_1) 
	{
	Send {r down}
	KeyWait, w   ; <--- pausing mid-hotkey like this *can* cause problems
	Send {r up}
	}
Return
Be aware that this technique (waiting in a hotkey indefinitely) causes issues if you try to do it with more than one hotkey.
Splitting the code into a press hotkey and a release hotkey is better:

Code: Select all

с::
	If (CurID = WinID_1) 
	{
	Send {r down}
	}
	return

w up::
	If (CurID = WinID_1) 
	{
	Send {r up}
	}
	return
Thanks for help with key holding, this much help me. And thanks for taking the time.

Return to “Ask For Help”

Who is online

Users browsing this forum: euras, Google [Bot], hd0202, kentpachi, tmplinshi and 39 guests