Adding hotkey to script blocks script execution of code after the hotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Pythonic
Posts: 4
Joined: 21 Mar 2019, 15:06
Contact:

Adding hotkey to script blocks script execution of code after the hotkey

25 Oct 2021, 19:44

I'm writing an AHK script that writes to a named pipe. A separate program on my computer connects and listens to the named pipe. When the hotkey is pressed, the AHK script will write to the named pipe.

This all works, however, the main script defined after the hotkey does not execute.

Code: Select all

#NoEnv

class PipeServer
{
  __New(name)
  {
    this.name := name
    this.pipe := DllCall("CreateNamedPipe", "Str",  this.name, "UInt", 3, "UInt", 6, "UInt", 1, "UInt", 65536, "UInt", 65536, "Ptr",  0, "Ptr",  0)
  }

  Connect()
  {
      DllCall("ConnectNamedPipe", "Ptr", this.pipe, "Ptr", 0)

  }

  Close()
  {
      DllCall("CloseHandle", "Ptr", this.pipe)
  }

  Write(message)
  {
      StrLen := StrLen(message) * (A_IsUnicode ? 2 : 1)
      DllCall("WriteFile", "Ptr", this.pipe, "Str", message, "UInt", StrLen, "Ptr", 0, "Ptr", 0)
  }

}

Pipes := []

pipe := New PipeServer("\\.\pipe\fooname")
pipe.Connect()
Pipes.push(pipe)

OnExit(pipe.Close)

MsgBox, "I do Execute"

#n::
    pipe_index := 1
    pipe := Pipes[pipe_index]
    pipe.Write("1")
    MsgBox, "This Also Executes when the hotkey is pressed"
    Return

MsgBox, "I do not execute"

; This won't execute
Loop {
    ; main script logic
}
[Mod edit: quote tags removed, [code][/code] tags added.]

But if I remove the hotkey definition, the main script logic does get executed, but of course then I don't have the HotKey I need. What am I missing?
Rohwedder
Posts: 7672
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Adding hotkey to script blocks script execution of code after the hotkey

26 Oct 2021, 00:55

Hallo,
adding hotkey to script ends the Auto-execute Section!
see https://www.autohotkey.com/docs/Scripts.htm#auto

Code: Select all

; .....
Pipes.push(pipe)
OnExit(pipe.Close)
MsgBox, "I do Execute"
; << End of the Auto-execute Section because there is a hotkey in the next code line: (#n::)
#n::
pipe_index := 1
pipe := Pipes[pipe_index]
pipe.Write("1")
MsgBox, "This Also Executes when the hotkey is pressed"
Return
; << This code will never be executed because there is no reason to read it.
MsgBox, "I do not execute"
; This won't execute
Loop
{
	; main script logic
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 393 guests