v1 → v2 for OnMessage

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kdaube
Posts: 86
Joined: 02 Nov 2015, 03:11

v1 → v2 for OnMessage

03 Mar 2023, 11:03

Dear experts
When converting a short script to v2 I struggle with "Invalid callback function":
V1 version:

Code: Select all

; Listen to the Windows power event "WM_POWERBROADCAST" (ID: 0x218):
OnMessage(0x218, "WM_POWERBROADCAST")
Return

WM_POWERBROADCAST(wParam, lParam) {
  FormatTime, now, , ddd yyyy-MM-dd HH:mm:ss
  FileAppend `n%now%%A_TAB%WM_POWERBROADCAST wParam = %wParam%, D:\System_ddd\logs\_system_log.txt
  Return
}
v2 version

Code: Select all

#Requires AutoHotkey v2.0
OnMessage(0x218, WM_POWERBROADCAST)  ; ← Invalid callback function.
Return

WM_POWERBROADCAST(wParam, lParam) {
  now := FormatTime(, "ddd yyyy-MM-dd HH:mm:ss")
  FileAppend("`n" now  A_TAB "WM_POWERBROADCAST wParam = " wParam, "D:\System_ddd\logs\_system_log.txt")
  Return
}
How can I define this correctly?
Klaus Daube, Zürich, CH
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: v1 → v2 for OnMessage

03 Mar 2023, 11:20

Code: Select all

#Requires AutoHotkey v2.0
Persistent
OnMessage 0x218, WM_POWERBROADCAST

WM_POWERBROADCAST(wParam, lParam, msg, hwnd) {
 MsgBox 123
}
Explained: OnMessage
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: v1 → v2 for OnMessage

03 Mar 2023, 11:37

Or WM_POWERBROADCAST(wParam, lParam, *)
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: v1 → v2 for OnMessage

03 Mar 2023, 11:44

If you only need wParam you can do this:

Code: Select all

OnMessage(0x218, WM_POWERBROADCAST)
Return

WM_POWERBROADCAST(wParam, *) {
if wParam = 10
	msgbox "Power status has changed"
}
(which works when I unplug my laptop)
14.3 & 1.3.7
User avatar
kdaube
Posts: 86
Joined: 02 Nov 2015, 03:11

Re: v1 → v2 for OnMessage

03 Mar 2023, 12:10

Thank you, mikeyww - it works now.
Klaus Daube, Zürich, CH

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], Draken, macromint, songdg and 72 guests