Script key remappings works only within cmd.exe, Why?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
inoperable
Posts: 10
Joined: 16 Nov 2019, 10:30

Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 12:42

Hello everyone,

For some reason I don't understand the below script works as intended, but most of the simple remappings,
like > to ) or : to ; do work only in cmd.exe, and not in any other applications, and I would like those
to work everywhere. (o.O)

It's a modified/customized capslock script from https://github.com/Vonng/Capslock/blob/master/win/CapsLock.ahk for those interested

There are probably some dumb things and errors, would be cool if one can point out those if something does not makes sense.
Thank you in advance,
Best.
P.

Code: Select all

#SingleInstance force
SetTitleMatchMode(2)
PORTABLE := EnvGet("PORTABLE")
APPS := EnvGet("APPS")
USERPROFILE := EnvGet("USERPROFILE")
SetCapsLockState("AlwaysOff")
;______________________________________________________________________
~*CapsLock::
	SetCapsLockState("AlwaysOff")
;______________________________________________________________________
CapsLock & h::
	if GetKeyState("control") = 0
	{
	    if GetKeyState("alt") = 0
	        Send("{Left}")
	    else
	        Send("+{Left}")
	}
	else {
	    if GetKeyState("alt") = 0
	        Send("^{Left}")
	    else
	        Send("+^{Left}")
	}
	Return

;______________________________________________________________________
CapsLock & j::
	if GetKeyState("control") = 0
	{
	    if GetKeyState("alt") = 0{
	        Send("{Down}")
	    }
	    else{
	        Send("+{Down}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0{
	        Send("^{Down}")
	    }
	    else{
	        Send("+^{Down}")
	    }
	}
	Return

;______________________________________________________________________
CapsLock & k::
	if GetKeyState("control") = 0
	{
	    if GetKeyState("alt") = 0{
	        Send("{Up}")
	    }
	    else{
	        Send("+{Up}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0{
	        Send("^{Up}")
	    }
	    else{
	        Send("+^{Up}")
	    }
	}
	Return

;______________________________________________________________________
CapsLock & l::
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0{
	        Send("{Right}")
	    }
	    else{
	        Send("+{Right}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0 {
	        Send("^{Right}")
	    }
	    else{
	        Send("+^{Right}")
	    }
	}
	Return

;______________________________________________________________________
CapsLock & i::
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0 {
	        Send("{Home}")
	    }
	    else {
	        Send("+{Home}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0 {
	        Send("^{Home}")
	    }
	    else {
	        Send("+^{Home}")
	    }
	}
	Return

;______________________________________________________________________
CapsLock & o::
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0 {
	        Send("{End}")
	    }
	    else {
	        Send("+{End}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0 {
	        Send("^{End}")
	    }
	    else {
	        Send("+^{End}")
	    }
	}
	Return

;______________________________________________________________________
CapsLock & u::                                                        
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0{
	        Send("{PgUp}")
	    }
	    else {
	        Send("+{PgUp}")
	    }
	}
	else {
	    if GetKeyState("alt") = 0
	        Send("^{PgUp}")                                                 
	    else
	        Send("+^{PgUp}")
	}
	Return

;______________________________________________________________________
CapsLock & p::
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0
	        Send("{PgDn}")
	    else
	        Send("+{PgDn}")
	}
	else {
	    if GetKeyState("alt") = 0
	        Send("^{PgDn}")
	    else
	        Send("+^{PgDn}")
	}
	Return

;______________________________________________________________________
CapsLock & m::  
	if GetKeyState("control") = 0 {
	    if GetKeyState("alt") = 0
	        Send("{BS}")
	    else
	        Send("+{BS}")
	}
	else {
	    if GetKeyState("alt") = 0
	        Send("^{BS}")
	    else 
	        Send("^{BS}")
	} 
	Return

;______________________________________________________________________
CapsLock & n::
	if GetKeyState("control") = 0
	{
	    if GetKeyState("alt") = 0                                  
	        Send("+{BS}")                                             
	    else 
	        Send("{BS}")
	}		
	else {
	    if GetKeyState("alt") = 0
	        Send("+^{BS}")                                             
	    else
	        Send("^{BS}")
	}	 
	Return

;______________________________________________________________________________
CapsLock & z::
	Send("^z")                                              
	Return
;______________________________________________________________________________
CapsLock & x::
	Send("^x")
	Return

;______________________________________________________________________________
CapsLock & c::
	Send("^c")
	Return

;______________________________________________________________________________
CapsLock & v:: 
	Send("^v")
	Return 

;______________________________________________________________________________
CapsLock & a:: 
	Send("^a") 
	Return

;______________________________________________________________________________
CapsLock & y::  
	Send("^y") 
	Return

;______________________________________________________________________________
CapsLock & w::  
	Send("^{Right}")
	Return

;______________________________________________________________________________
CapsLock & b::  
	Send("^{Left}")
	Return

;______________________________________________________________________________
CapsLock & \::  
	Send("{AppsKey}")
	Return

;______________________________________________________________________
CapsLock & F1:: 
	Send("{Volume_Down}") 
	Return
CapsLock & F2:: 
	Send("{Volume_Up}")
	Return
CapsLock & F3:: 
	Send("{Volume_Mute}")  
	Return
CapsLock & F4:: 
	Send("{Media_Play_Pause}") 
	Return
CapsLock & F5:: 
	Send("{Media_Next}") 
	Return
CapsLock & F6:: 
	Send("{Media_Stop}")
	Return
;______________________________________________________________________
CapsLock & q::                    
    #if WinActive("ahk_exe cmd.exe")
        Send("exit{Enter}")
        Return
;______________________________________________________________________
CapsLock & `::
Run('C:\tools\portable\misc\PowerRun.exe C:\Windows\System32\cmd.exe') 
Return

;______________________________________________________________________
CapsLock & 1::
	Run('C:Windows\System32\cmd.exe')
	Return

;______________________________________________________________________
CapsLock & 2::
	Run('C:\tools\portable\tcup\tcup.exe') 
	Return

;______________________________________________________________________
CapsLock & 3::
	Run('C:\Windows\System32\cmd.exe /k %TOOLS%\bin\##.cmd') 
	Return

;______________________________________________________________________
CapsLock & 4::
	Run('C:\Windows\System32\cmd.exe /k C:\tools\scoop\apps\far\current\far.exe')
	Return

;______________________________________________________________________
CapsLock & 5::  
	Run('C:\tools\portable\subl\subl.exe') 
	Return

;______________________________________________________________________
CapsLock & 6::
	Run('C:\tools\portable\atom-nightly\atom-nightly.exe') 
	Return

;______________________________________________________________________
CapsLock & 9::
	Run(USERPROFILE . '\AppData\Local\Google\Chrome SxS\Application\chrome.exe')
	Return

;______________________________________________________________________
CapsLock & 0:: 
	Run("::{20d04fe0-3aea-1069-a2d8-08002b30309d}")
	Return

;______________________________________________________________________
;CapsLock & /::
;	Send("^e")
;	Send("c")
;	Return

;______________________________________________________________________
;CapsLock & \:: 
;	Send("^e")
;	Send("u")
;	Return

;______________________________________________________________________
;Tap ALT+Q twice to close a window, so you dont have to add to rsi with fucktarded windows shortcuts 
~!q::
    if (A_PriorHotkey != "~!q" or A_TimeSincePriorHotkey > 300) {
    	Send("!q") 
    }
    else {
    	Send("!{F4}") 
    }
Return
;______________________________________________________________________
; ` to tilde 
$`::~ 
;______________________________________________________________________
; `+shift to ` 
$+~::` 
;______________________________________________________________________
; semicolon to colon 
`;:::
;______________________________________________________________________
; backslash to pipe 
\::|
;______________________________________________________________________
; shift+semicolon to semicolon 
+;::`;
;______________________________________________________________________
; quote to duouble-quote 
'::"
;______________________________________________________________________
; shift+quote to quote 
+"::'
;______________________________________________________________________
; shift+backslash to backslash 
+\::\
;______________________________________________________________________
; shift+coma to leftparen 
+,::(
;______________________________________________________________________
; shift+point to rightparen 
+.::)
;______________________________________________________________________
; shift+9 to less 
+9::<
;______________________________________________________________________
; shift+0 to greater 
+0::>
;______________________________________________________________________
; win+[ to parenleft 
#[::(
;______________________________________________________________________
; win+] to parenright 
#]::)
;______________________________________________________________________
!,::<
;______________________________________________________________________
!.::>
;______________________________________________________________________
CapsLock & Shift::  
	Send("{ShiftDown}{CapsLock Down}{CapsLock Up}{Shift Up}") 
	Return

;______________________________________________________________________
CapsLock & .:: 
	Send("{Del}") 
	Return

;______________________________________________________________________
CapsLock & ,:: 
	Send("^{Del}") 
	Return

;______________________________________________________________________
AppsKey Up::
	ToolTip 
	Return

;______________________________________________________________________
~AppsKey & RCtrl:: 
	Send("!{Tab}") 
	Return

;______________________________________________________________________
~AppsKey & RAlt:: 
	Send("+!{Tab}") 
	Return

;______________________________________________________________________
;Send("{CtrlDown}{Tab}{CtrlUp}")
;Send("{CtrlDown}{ShiftDown}{Tab}{ShiftUp}{CtrlUp}")
If WinExist("ahk_class Chrome") {
	#if WinActive("Chrome")
    +WheelUp::
		Send("^{PgUp}") 
	+WheelDown::
    	Send("^{PgDn}") 
	CapsLock & ]::
    	Send("^{PgDn}") 
        Return
    CapsLock & [::
    	Send("^{PgUp}") 
        Return
    ![::
    	Send("{Browser_Back}")
    	Return
	!]::
    	Send("{Browser_Forward}")
    	Return
}
;______________________________________________________________________
; RWin & d::
; 	if GetKeyState("control") = 0 and GetKeyState("alt") = 0 {
; 	    ComObjCreate("Shell.Application").ToggleDesktop()
; 	} else {
; 	    Send("#{d}")
; 	}
; 	Return
;______________________________________________________________________
CapsLock & d::
	ComObjCreate("Shell.Application").ToggleDesktop()
	Return
;
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 12:51

on line 261: #if WinActive("ahk_exe cmd.exe")
also note on line 411: #if WinActive("Chrome") (and note that the if winexist(..) line on 410 has no effect)

See :arrow: #if.

Cheers.
inoperable
Posts: 10
Joined: 16 Nov 2019, 10:30

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 13:35

well, the hotkey on line 261 should be triggered only when cmd.exe is foremost, at least that's the idea,
and it works this way - so I need to "escape" the clause with an empty #if?

The basic idea is that the hotkey on 261 works only within cmd.exe, the rest everywhere and (obviously)
those within chrome only in chrome ;-) I've seen those double clauses and reused them

Code: Select all

if winexsist #if winactive
Can you be a bit more specific? the Syntax is really far from intuitive (at least from me) and you obviously
know what the problem is - yet I still don't ;-)
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 13:59

if CapsLock & q:: is supposed to only work when cmd.exe is foremost, the #if directive must be placed above it,

Code: Select all

;______________________________________________________________________
#if WinActive("ahk_exe cmd.exe")
CapsLock & q::                    
        Send("exit{Enter}")
        Return
;______________________________________________________________________
#if ; place this here to disable "#if WinActive("ahk_exe cmd.exe")" from affecting this and any other subsequent hotkeys:
CapsLock & `::
In

Code: Select all

return
If WinExist("ahk_class Chrome") {
	#if WinActive("Chrome")
    +WheelUp::
		Send("^{PgUp}") 
	+WheelDown::
    	Send("^{PgDn}") 
	CapsLock & ]::
    	Send("^{PgDn}") 
        Return
    CapsLock & [::
    	Send("^{PgUp}") 
        Return
    ![::
    	Send("{Browser_Back}")
    	Return
	!]::
    	Send("{Browser_Forward}")
    	Return
}
the outer if winExist(...) does nothing because it can never be executed due to the return above. Also note that you can't define hotkeys or #ifs conditionally (except if you use the hotkey command). You can combine the two conditions into one #if, that is, #if WinExist("ahk_class Chrome") && WinActive("Chrome"), if that makes sense, idk. And again, if you don't want the #if WinActive(...) to affect the last hotkey, place another #if before it.

Cheers.
inoperable
Posts: 10
Joined: 16 Nov 2019, 10:30

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 14:42

Thank you for the detailed explanation! This makes a lot more sense now.
inoperable
Posts: 10
Joined: 16 Nov 2019, 10:30

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 15:00

My malformed if clause you corrected, the

Code: Select all

#if WinExist("ahk_class Chrome") && WinActive("Chrome")
was essentially an trail & error result and gawking around in docs to get this to work.
I haven't got my head around how the control flow works and how to explicitly target a

Code: Select all

ahk_class Name
object, or foremost window and how when exit (return) the routines safely.
The Return statement above it must have gone unnoticed in my ill-fated trials :)

Anything else would change or point out to rethink? I"m still learning around AHK, it makes Windows so less Windowish and (almost) enjoyable to work with.

[Never thought I catch myself writing Windows and enjoyable in the same sentence]
inoperable
Posts: 10
Joined: 16 Nov 2019, 10:30

Re: Script key remappings works only within cmd.exe, Why?

22 Nov 2019, 15:10

Last question I wanted to ask, since it"s something I also can"t figure out alone:
I"d like to simulate the copy-on-selection behaviour thats in Xwindow/Xorg by default, but there is a bit of a problem since Windows
has only one Clipboard/Register and I don"t know how to set and "onTextSelected" Event-handler in AHK - so whenever a text is selected/marked
that selection is put into some variable and kept there until the next one is selected without overwriting the system clipboard
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script key remappings works only within cmd.exe, Why?

23 Nov 2019, 06:43

Regarding,

Code: Select all

#if WinExist("ahk_class Chrome") && WinActive("Chrome")
This might be preferable,

Code: Select all

#if WinActive("Chrome ahk_class Chrome")
Anyways, I know nothing about your last question, that seems to be better suited in a new topic.

Cheers.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 35 guests