2nd Script removes functions of 1st Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozzynotwood
Posts: 30
Joined: 16 Nov 2017, 19:19

2nd Script removes functions of 1st Script

17 Mar 2018, 21:52

Hello

I can't work this out, I don't have much programming knowledge, I basically use AHK to create keystroke macros, anything above that I copy'paste/manipulate code from forums like this :)

Below is the AHK code for the Desktop Switcher, I use 6 desktops on Windows 10 & use the following keys to select the desktop I need:
Numpad1 = Screen 1
Numpad2 = Screen 2
Numpad3 = Screen 3
NumpadEnter & Numpad1 = Screen 4
NumpadEnter & Numpad2 = Screen 5
NumpadEnter & Numpad3 = Screen 6

As you can see, it's a good ergonomic setup, I can hit the keys & have my pinky finger on NumPadEnter, ready when I need it.

My problem is when I load the 2nd script that I use to kill notepad. Both scripts work on their own, but when loaded at the same time the Desktop Switches "NumpadEnter & NumpadX" screens fails to execute. The Desktop Switchers "NumpadEnter" key has no effect, so when I hit "NumPadEnter & Numpad1" I only get the the screen assigned to "Numpad1".

The code in the second script is 1 line:
NumpadEnter & Numpad9:: Run, %windir%\system32\taskkill.exe /IM notepad.exe /F

I do need the notepad killer on a second script so I can build on it later, how can I make my notepad (or future scripts) use the NumPadEnter key & have the 2 scripts co-exist?

Code: Select all

Menu, Tray, Icon, Desktop Switcher.ico

; Globals
;DesktopCount = 2 ; Windows starts with 2 desktops at boot
CurrentDesktop = 1 ; Desktop count is 1-indexed (Microsoft numbers them this way)
;
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
;
mapDesktopsFromRegistry() {
 global CurrentDesktop, DesktopCount
 ; Get the current desktop UUID. Length should be 32 always, but there's no guarantee this couldn't change in a later Windows release so we check.
 IdLength := 32
 SessionId := getSessionId()
 if (SessionId) {
 RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
 if (CurrentDesktopId) {
 IdLength := StrLen(CurrentDesktopId)
 }
 }
 ; Get a list of the UUIDs for all virtual desktops on the system
 RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
 if (DesktopList) {
 DesktopListLength := StrLen(DesktopList)
 ; Figure out how many virtual desktops there are
 DesktopCount := DesktopListLength / IdLength
 }
 else {
 DesktopCount := 1
 }
 ; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
 i := 0
 while (CurrentDesktopId and i < DesktopCount) {
 StartPos := (i * IdLength) + 1
 DesktopIter := SubStr(DesktopList, StartPos, IdLength)
 OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%.
 ; Break out if we find a match in the list. If we didn't find anything, keep the
 ; old guess and pray we're still correct :-D.
 if (DesktopIter = CurrentDesktopId) {
 CurrentDesktop := i + 1
 OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%.
 break
 }
 i++
 }
}
;
; This functions finds out ID of current session.
;
getSessionId()
{
 ProcessId := DllCall("GetCurrentProcessId", "UInt")
 if ErrorLevel {
 OutputDebug, Error getting current process id: %ErrorLevel%
 return
 }
 OutputDebug, Current Process Id: %ProcessId%
 DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
 if ErrorLevel {
 OutputDebug, Error getting session id: %ErrorLevel%
 return
 }
 OutputDebug, Current Session Id: %SessionId%
 return SessionId
}
;
; This function switches to the desktop number provided.
;
switchDesktopByNumber(targetDesktop)
{
 global CurrentDesktop, DesktopCount
 ; Re-generate the list of desktops and where we fit in that. We do this because
 ; the user may have switched desktops via some other means than the script.
 mapDesktopsFromRegistry()
 ; Don't attempt to switch to an invalid desktop
 if (targetDesktop > DesktopCount || targetDesktop < 1) {
 OutputDebug, [invalid] target: %targetDesktop% current: %CurrentDesktop%
 return
 }
 ; Go right until we reach the desktop we want
 while(CurrentDesktop < targetDesktop) {
 Send ^#{Right}
 CurrentDesktop++
 OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop%
 }
 ; Go left until we reach the desktop we want
 while(CurrentDesktop > targetDesktop) {
 Send ^#{Left}
 CurrentDesktop--
 OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop%
 }
}
;
; This function creates a new virtual desktop and switches to it
;
createVirtualDesktop()
{
 global CurrentDesktop, DesktopCount
 Send, #^d
 DesktopCount++
 CurrentDesktop = %DesktopCount%
 OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop%
}
;
; This function deletes the current virtual desktop
;
deleteVirtualDesktop()
{
 global CurrentDesktop, DesktopCount
 Send, #^{F4}
 DesktopCount--
 CurrentDesktop--
 OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
}
; Main
SetKeyDelay, 75
mapDesktopsFromRegistry()
OutputDebug, [loading] desktops: %DesktopCount% current: %CurrentDesktop%
; User config!
; This section binds the key combo to the switch/create/delete actions
Numpad1::switchDesktopByNumber(1)
Numpad2::switchDesktopByNumber(2)
Numpad3::switchDesktopByNumber(3)
NumpadEnter & Numpad1::switchDesktopByNumber(4)
NumpadEnter & Numpad2::switchDesktopByNumber(5)
NumpadEnter & Numpad3::switchDesktopByNumber(6)
;LWin & 7::switchDesktopByNumber(7)
;LWin & 8::switchDesktopByNumber(8)
;LWin & 9::switchDesktopByNumber(9)
;CapsLock & 1::switchDesktopByNumber(1)
;CapsLock & 2::switchDesktopByNumber(2)
;CapsLock & 3::switchDesktopByNumber(3)
;CapsLock & 4::switchDesktopByNumber(4)
;CapsLock & 5::switchDesktopByNumber(5)
;CapsLock & 6::switchDesktopByNumber(6)
;CapsLock & 7::switchDesktopByNumber(7)
;CapsLock & 8::switchDesktopByNumber(8)
;CapsLock & 9::switchDesktopByNumber(9)
;CapsLock & n::switchDesktopByNumber(CurrentDesktop + 1)
;CapsLock & p::switchDesktopByNumber(CurrentDesktop - 1)
;CapsLock & s::switchDesktopByNumber(CurrentDesktop + 1)
;CapsLock & a::switchDesktopByNumber(CurrentDesktop - 1)
;CapsLock & c::createVirtualDesktop()
;CapsLock & d::deleteVirtualDesktop()
; Alternate keys for this config. Adding these because DragonFly (python) doesn't send CapsLock correctly.
;^!1::switchDesktopByNumber(1)
;^!2::switchDesktopByNumber(2)
;^!3::switchDesktopByNumber(3)
;^!4::switchDesktopByNumber(4)
;^!5::switchDesktopByNumber(5)
;^!6::switchDesktopByNumber(6)
;^!7::switchDesktopByNumber(7)
;^!8::switchDesktopByNumber(8)
;^!9::switchDesktopByNumber(9)
;^!n::switchDesktopByNumber(CurrentDesktop + 1)
;^!p::switchDesktopByNumber(CurrentDesktop - 1)
;^!s::switchDesktopByNumber(CurrentDesktop + 1)
;^!a::switchDesktopByNumber(CurrentDesktop - 1)
;^!c::createVirtualDesktop()
;^!d::deleteVirtualDesktop()
Hotte
Posts: 32
Joined: 03 Jan 2018, 14:39

Re: 2nd Script removes functions of 1st Script

19 Mar 2018, 07:50

Not going into details just one hint - maybe it helps: Autohotkey does not do real multitasking. It cannot execute 2 scripts at the same time. If thread (= active script) A runs and thread B drops in, thread A will halt and continue only when thread B has terminated.
ozzynotwood
Posts: 30
Joined: 16 Nov 2017, 19:19

Re: 2nd Script removes functions of 1st Script

19 Mar 2018, 16:03

Hotte wrote:Not going into details just one hint - maybe it helps: Autohotkey does not do real multitasking. It cannot execute 2 scripts at the same time. If thread (= active script) A runs and thread B drops in, thread A will halt and continue only when thread B has terminated.
I currently run 4 scripts with no issues until now. The 2 I didn't mention are only word replacers. The 2 scripts above don't share any hotkey key combinations. Any ideas anyone?
Guest

Re: 2nd Script removes functions of 1st Script

19 Mar 2018, 16:44

it seems only last activated script has the combination hotkey active
#include or #if getkeystate() might work for you

Code: Select all

#if GetKeyState("NumpadEnter", "P")  ;or #Include C:\notepadkill
Numpad1::ms(1)
#if

ms(x){
MsgBox,% x
}

Code: Select all

#if GetKeyState("NumpadEnter", "P")
Numpad9::Run, %windir%\system32\taskkill.exe /IM notepad.exe /F
#if

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 343 guests