Need help converting AHK 1 to AHK 2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Need help converting AHK 1 to AHK 2

Post by tiska » 10 Apr 2024, 22:38

I found a useful little script written by 0xB0BAFE77 that shows an image while holding down a key. I use it to lookup keyboard shortcuts. https://www.reddit.com/r/AutoHotkey/comments/uczou4/show_image_while_holding_key_and_close_image_when/

Code: Select all

#SingleInstance Force                                       ; One instance
make_gui()                                                  ; Create the gui
Return


make_gui() {
    Static pic_path := "C:\Users\TE\Desktop\HotKeys.png"    ; Path to the pic
         , key       := "a"                                 ; Key you want to use to hide/show
         , key_send  := true                                ; True if you want key to still fire
    
    Gui, New, +AlwaysOnTop -Caption +HWNDgui_hwnd +Border   ; New window (gui) to host the image
    Gui, Margin, 0, 0                                       ; Set default margin size
    Gui, Add, Picture, +HWNDpic_hwnd, % pic_path            ; Add picture to gui
    OnMessage(0x0201, "WM_LBUTTONDOWN")                     ; Allows for click+drag moving
    
    bf := Func("toggle_gui").bind(gui_hwnd, 1, pic_hwnd)    ; Create boundfunc for key down
    Hotkey, % (key_send ? "~" : "") "*" key, % bf           ; Create key down hotkey to show image
    bf := Func("toggle_gui").bind(gui_hwnd, 0)              ; Create boundfunc for key up
    Hotkey, % (key_send ? "~" : "") "*" key " Up", % bf     ; Create key up hotkey to hide image\
}

toggle_gui(hwnd, key_state, pic_hwnd:="") {                 ; Handles toggling window view
    Gui, % HWND ":" (key_state ? "Show" : "Hide")
}

WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {                 ; Handles click+dragging
    SendMessage, 0x00A1, 0x2
}
I'd like to integrate it into an existing AHK 2 script so I used the AHK-v2-script-converter tool, but that did not work.
Any help would be greatly appreciated :D

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Need help converting AHK 1 to AHK 2

Post by Seven0528 » 11 Apr 2024, 01:33

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
myGui:=gui()
myGui.opt("+AlwaysOnTop +Border -Caption")
myGui.MarginX:=0, myGui.MarginY:=0
download("https://www.autohotkey.com/boards/download/file.php?avatar=154873_1711854038.png",A_ScriptDir "\154873_1711854038.png")
picCntl:=myGui.addPicture(,A_ScriptDir "\154873_1711854038.png")
myGui.show()
onMessage(0x0201, WM_LBUTTONDOWN)
WM_LBUTTONDOWN(wParam, lParam, msg, hWnd) {
    static WM_NCLBUTTONDOWN:=0x00A1, HTCAPTION:=2
    sendMessage WM_NCLBUTTONDOWN, HTCAPTION
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Need help converting AHK 1 to AHK 2

Post by tiska » 11 Apr 2024, 01:54

Your script performs a very different function, but thanks for sharing.

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Need help converting AHK 1 to AHK 2

Post by Seven0528 » 11 Apr 2024, 02:04

 ...?
I provided only a partial translation of the GUI-related parts because I found it too cumbersome to translate everything.
Your task is to directly translate the Hotkey section yourself.

I believe that you could study the fact that, unlike in v1, when registering a function object, you pass the function object itself instead of a string, using the script I provided.
In other words, I thought you could handle the rest on your own.
Personally, I'm not particularly fond of shallow questions.
Especially when someone doesn't try to study, explore, or attempt something on their own and then doesn't share the results (even if it doesn't work).
It's hard to see the provided code as an attempt to translate v2.

BoundFunc
Gui
Hotkey
OnMessage
Please at least read the official documentation and try.
While this might seem like one question, it actually contains several mixed together.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Need help converting AHK 1 to AHK 2

Post by Seven0528 » 11 Apr 2024, 02:20

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
myGui:=gui()
myGui.opt("+AlwaysOnTop +Border -Caption")
myGui.MarginX:=0, myGui.MarginY:=0
download("https://www.autohotkey.com/boards/download/file.php?avatar=154873_1711854038.png",A_ScriptDir "\154873_1711854038.png")
picCntl:=myGui.addPicture(,A_ScriptDir "\154873_1711854038.png")
onMessage(0x0201, WM_LBUTTONDOWN)
hotkey "*~a", toggleGuiVisible.bind(myGui, true)
hotkey "*~a Up", toggleGuiVisible.bind(myGui, false)
toggleGuiVisible(guiObj, onoff, hotkeyName)    {
    guiObj.show(onoff?"NoActivate":"Hide")
}
WM_LBUTTONDOWN(wParam, lParam, msg, hWnd) {
    static WM_NCLBUTTONDOWN:=0x00A1, HTCAPTION:=2
    sendMessage WM_NCLBUTTONDOWN, HTCAPTION
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

niCode
Posts: 315
Joined: 17 Oct 2022, 22:09

Re: Need help converting AHK 1 to AHK 2

Post by niCode » 11 Apr 2024, 02:34

Code: Select all

#SingleInstance Force                                                       ; One instance
make_gui()                                                                  ; Create the gui


make_gui() {
    static pic_path  := 'C:\Users\TE\Desktop\HotKeys.png'                   ; Path to the pic
         , key       := 'a'                                                 ; Key you want to use to hide/show
         , key_send  := true                                                ; True if you want key to still fire

    myGui := Gui('+AlwaysOnTop -Caption +Border')                           ; New window (gui) to host the image
    myGui.MarginX := 0, myGui.MarginY := 0                                  ; Set default margin size
    myGui.AddPicture(, pic_path)                                            ; Add picture to gui
    OnMessage(0x0201, (*) => SendMessage(0x00A1, 0x2))                      ; Allows for click+drag moving

    Hotkey((key_send ? '~' : '') '*' key, toggle_gui)                       ; Create key down hotkey to show image
    Hotkey((key_send ? '~' : '') '*' key ' Up', toggle_gui)                 ; Create key up hotkey to hide image

    toggle_gui(*) => GetKeyState(key, 'P') ? myGui.Show() : myGui.Hide()    ; Handles toggling window view
}

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Need help converting AHK 1 to AHK 2

Post by tiska » 11 Apr 2024, 02:39

I appreciate your effort in providing a partial translation of the script and thank you for pointing out the difference in registering a function object compared to v1.
I'm still a novice when it comes to AHK coding :D

User avatar
Seven0528
Posts: 395
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Need help converting AHK 1 to AHK 2

Post by Seven0528 » 11 Apr 2024, 02:46

 @tiska
I think I was irritable yesterday because I couldn't even sleep for four hours.
My words might have been a bit sharp. I'm glad my code was helpful.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
tiska
Posts: 26
Joined: 21 Mar 2024, 10:48

Re: Need help converting AHK 1 to AHK 2

Post by tiska » 11 Apr 2024, 02:50

@niCode thank you, both versions work great! Super useful to popup a cheatsheet with list of hotkeys etc. :)

Post Reply

Return to “Ask for Help (v2)”