Windows 如何在自动热键脚本中响应WM_设置更改

Windows 如何在自动热键脚本中响应WM_设置更改,windows,environment-variables,autohotkey,Windows,Environment Variables,Autohotkey,(此问题类似于但适用于语言。这不是关于从内部自动热键发送WM_设置更改。) 在另一个Windows进程(“发件人”)中,我通过修改HK_CURRENT_用户注册表来更改PATH环境变量。然后,我使用SendMessageTimeout API发送/发布WM_SETTINGCHANGE消息 我同时运行的自动热键脚本(“receiver”),我用它作为程序启动器,似乎没有意识到这个变化。我想捕获此消息,以便刷新脚本的PATH变量的本地副本。可能吗 例如,“发送者”可以是,或者是: 或其他方便的第三方

(此问题类似于但适用于语言。这不是关于从内部自动热键发送WM_设置更改。)

在另一个Windows进程(“发件人”)中,我通过修改HK_CURRENT_用户注册表来更改PATH环境变量。然后,我使用SendMessageTimeout API发送/发布WM_SETTINGCHANGE消息

我同时运行的自动热键脚本(“receiver”),我用它作为程序启动器,似乎没有意识到这个变化。我想捕获此消息,以便刷新脚本的PATH变量的本地副本。可能吗

例如,“发送者”可以是,或者是:

或其他方便的第三方Windows二进制文件,如:

或一些:

使用该功能对消息作出响应

下面是一个示例脚本

;;; This is an AutoHotKey -*- ahk -*- script 
;;;
;;; ABOUT
;;;  Respond to WM_SETTINGCHANGE messages and update this process's PATH
;;;  environment variable.
;;;
;;; USAGE
;;;  Run the script directly (e.g. double-click) or drag and drop onto
;;;  the AutoHotKey application.
;;;
;;; DEBUG
;;;  Optionally define a key binding to debug_show_recv_count, e.g.:
;;;    #space:: debug_show_recv_count()
;;;
;;; AUTHOR
;;;  piyo @ StackOverflow
;;;

;;
;; Register an AHK function as a callback.
;;
OnMessage( (WM_SETTINGCHANGE:=0x1A), "recv_WM_SETTINGCHANGE")

;;
;; Respond to the WM_SETTINGCHANGE message.
;;
recv_WM_SETTINGCHANGE(wParam, lParam, msg, hwnd)
{
  global g_recv_WM_SETTINGCHANGE_count
  g_recv_WM_SETTINGCHANGE_count := g_recv_WM_SETTINGCHANGE_count + 1
  ;;debug;; ToolTip Received a WM_SETTINGCHANGE !
  reset_env_path_from_registry()
}

;;
;; Import the recently changed Path environment variable from the
;; Windows Registry. Import from the System and User environments.
;;
reset_env_path_from_registry()
{
  sys_path := ""
  sys_subkey := "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
  RegRead, sys_path, HKEY_LOCAL_MACHINE, %sys_subkey%, Path
  cu_path := ""
  cu_subkey := "Environment"
  RegRead, cu_path, HKEY_CURRENT_USER, %cu_subkey%, Path
  new_path := sys_path . ";" . cu_path
  ;;debug;; MsgBox,% new_path
  EnvSet, PATH,% new_path
}

;;;

; Debug var for interactive sanity checking
g_recv_WM_SETTINGCHANGE_count := 0

; Debug function for interactive sanity checking
debug_show_recv_count() {
  global g_recv_WM_SETTINGCHANGE_count
  path := ""
  EnvGet, path, PATH
  msg := "g_recv_WM_SETTINGCHANGE := " . g_recv_WM_SETTINGCHANGE_count
  msg := msg . "!`n" . path
  MsgBox,% msg
}

;;; end
使用该功能对消息作出响应

下面是一个示例脚本

;;; This is an AutoHotKey -*- ahk -*- script 
;;;
;;; ABOUT
;;;  Respond to WM_SETTINGCHANGE messages and update this process's PATH
;;;  environment variable.
;;;
;;; USAGE
;;;  Run the script directly (e.g. double-click) or drag and drop onto
;;;  the AutoHotKey application.
;;;
;;; DEBUG
;;;  Optionally define a key binding to debug_show_recv_count, e.g.:
;;;    #space:: debug_show_recv_count()
;;;
;;; AUTHOR
;;;  piyo @ StackOverflow
;;;

;;
;; Register an AHK function as a callback.
;;
OnMessage( (WM_SETTINGCHANGE:=0x1A), "recv_WM_SETTINGCHANGE")

;;
;; Respond to the WM_SETTINGCHANGE message.
;;
recv_WM_SETTINGCHANGE(wParam, lParam, msg, hwnd)
{
  global g_recv_WM_SETTINGCHANGE_count
  g_recv_WM_SETTINGCHANGE_count := g_recv_WM_SETTINGCHANGE_count + 1
  ;;debug;; ToolTip Received a WM_SETTINGCHANGE !
  reset_env_path_from_registry()
}

;;
;; Import the recently changed Path environment variable from the
;; Windows Registry. Import from the System and User environments.
;;
reset_env_path_from_registry()
{
  sys_path := ""
  sys_subkey := "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
  RegRead, sys_path, HKEY_LOCAL_MACHINE, %sys_subkey%, Path
  cu_path := ""
  cu_subkey := "Environment"
  RegRead, cu_path, HKEY_CURRENT_USER, %cu_subkey%, Path
  new_path := sys_path . ";" . cu_path
  ;;debug;; MsgBox,% new_path
  EnvSet, PATH,% new_path
}

;;;

; Debug var for interactive sanity checking
g_recv_WM_SETTINGCHANGE_count := 0

; Debug function for interactive sanity checking
debug_show_recv_count() {
  global g_recv_WM_SETTINGCHANGE_count
  path := ""
  EnvGet, path, PATH
  msg := "g_recv_WM_SETTINGCHANGE := " . g_recv_WM_SETTINGCHANGE_count
  msg := msg . "!`n" . path
  MsgBox,% msg
}

;;; end

自动热键论坛上的用户NoobSwee发布了此消息

我的AutoHotKey.ahk脚本在每个Run语句之前调用该函数,以便从AutoHotKey启动的任何应用程序都获得当前系统环境,而不是启动时AutoHotKey捕获的环境

例如:

+#b::
RefreshEnvironment()
run c:\cygwin\bin\run.exe c:\cygwin\bin\rxvt.exe -geometry 80x40
return

+#g::
RefreshEnvironment()
run c:\gnu\emacs\bin\runemacs.exe
return

自动热键论坛上的用户NoobSwee发布了此消息

我的AutoHotKey.ahk脚本在每个Run语句之前调用该函数,以便从AutoHotKey启动的任何应用程序都获得当前系统环境,而不是启动时AutoHotKey捕获的环境

例如:

+#b::
RefreshEnvironment()
run c:\cygwin\bin\run.exe c:\cygwin\bin\rxvt.exe -geometry 80x40
return

+#g::
RefreshEnvironment()
run c:\gnu\emacs\bin\runemacs.exe
return
+#b::
RefreshEnvironment()
run c:\cygwin\bin\run.exe c:\cygwin\bin\rxvt.exe -geometry 80x40
return

+#g::
RefreshEnvironment()
run c:\gnu\emacs\bin\runemacs.exe
return