Sign in to follow this  
Followers 0
dieterhatn

Context Menu on Hotkey

7 posts in this topic

Hi there! I'm currently working on a clipboard manager, and it's working like a charm so far. The last problem that occured is the fact that autoit is missing any menu-like function. I'd like to avoid using a GUI, but if that's the only thing that works I would even use that. My Script saves the last 10 Entries of a clipboard in an array. When you Press Ctrl+Shift+V, it lists them up and offers you a way to put one of those entries into your clipboard and paste it. Currently im using tooltips with @CRLF to make a menu like interface, but that fact that I cannot click it makes it quite bad. I hope one of you guys has got another idea for me, thanks

Share this post


Link to post
Share on other sites



I did something with this to create context menus launched from buttons in an AutoIt GUI.

GUICtrlCreateContextMenu( [controlID] )

 

I could try to work out an example, but I've only used it in one case and it was a long time ago.  I think that you would create an AutoIt GUI to use the GUICtrlCreateContextMenu, maybe just create the GUI where you want the menu.


I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here is an example I can improve upon:

 

 

$hotkeydummy= guictrlcreatedummy() ;hotkey context menu, data for [>]dropdown list ; -
$hotkeycontext= guictrlcreatecontextmenu($hotkeydummy) ; -
$hotkeyinsert= guictrlcreatemenuitem("Choose key with virtual keyboard", $hotkeycontext) ; -
$hotkeytime= guictrlcreatemenuitem("Use system time to trigger function", $hotkeycontext) ; -
$hotkeydelay= guictrlcreatemenuitem("Repeat Delay", $hotkeycontext) ; -
$hotkeychangemultiable= guictrlcreatemenuitem("Change multiable", $hotkeycontext) ; -
$hotkeyremove= guictrlcreatemenuitem("Remove hotkey", $hotkeycontext) ; -

$buttonhotkey= guictrlcreatebutton(">", 129, 71+$i*$hotkeygroupheight, 20, 15)

do
    $runnermsg= guigetmsg()
    switch $runnermsg
    case $buttonhotkey
        showmenu($hgui, $runnermsg, $hotkeycontext)
    endswitch
    sleep(40); don't over heat
until $msg= $gui_event_close

Func ShowMenu($hwnd, $CtrlID, $nContextID); Show a menu in a given GUI window which belongs to a given GUI ctrl                                 -----
    local $arPos, $x, $y
    local $hMenu = GUICtrlGetHandle($nContextID)

    $arPos = ControlGetPos($hwnd, "", $CtrlID)

    $x = $arPos[0]
    $y = $arPos[1] + $arPos[3]

    ClientToScreen($hwnd, $x, $y)
    TrackPopupMenu($hwnd, $hMenu, $x, $y)
    ;if $hwnd= $hgui then guictrlsetdata($runnercontrols[7], "  Use Hotkeys")
EndFunc; end ShowMenu()

Func ClientToScreen($hWnd, ByRef $x, ByRef $y); Convert the client (GUI) coordinates to screen (desktop) coordinates                            -----
    local $stPoint = DllStructCreate("int;int")

    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    dllcall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))

    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc; end ClientToScreen()

Func TrackPopupMenu($hWnd, $hMenu, $x, $y); Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd) --
    dllcall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc; end TrackPopupMenu()
Edited by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

That looks promising!

Never worked with a dummy. How can I trigger the context menu?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Still working it out recheck example above.

I think I have everything in there now. I'll see if I can run the example.

Edited by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This should run the example.

#include <GUIConstants.au3>;gui_event_close

$hgui= guicreate("", 320, 200); Make GUI

$hotkeydummy= guictrlcreatedummy() ;hotkey context menu, data for [>]dropdown list ; -
$hotkeycontext= guictrlcreatecontextmenu($hotkeydummy) ; -
$hotkeyinsert= guictrlcreatemenuitem("Choose key with virtual keyboard", $hotkeycontext) ; -
$hotkeytime= guictrlcreatemenuitem("Use system time to trigger function", $hotkeycontext) ; -
$hotkeydelay= guictrlcreatemenuitem("Repeat Delay", $hotkeycontext) ; -
$hotkeychangemultiable= guictrlcreatemenuitem("Change multiable", $hotkeycontext) ; -
$hotkeyremove= guictrlcreatemenuitem("Remove hotkey", $hotkeycontext) ; -

$buttonhotkey= guictrlcreatebutton(">", 129, 71, 20, 15)
guisetstate(); Show GUI

do
    $runnermsg= guigetmsg()
    switch $runnermsg
    case $buttonhotkey
        showmenu($hgui, $runnermsg, $hotkeycontext)
    endswitch
    sleep(40); don't over heat
until $runnermsg= $gui_event_close

Func ShowMenu($hwnd, $CtrlID, $nContextID); Show a menu in a given GUI window which belongs to a given GUI ctrl -----
    local $arPos, $x, $y
    local $hMenu = GUICtrlGetHandle($nContextID)

    $arPos = ControlGetPos($hwnd, "", $CtrlID)

    $x = $arPos[0]
    $y = $arPos[1] + $arPos[3]

    ClientToScreen($hwnd, $x, $y)
    TrackPopupMenu($hwnd, $hMenu, $x, $y)
    ;if $hwnd= $hgui then guictrlsetdata($runnercontrols[7], " Use Hotkeys")
EndFunc; end ShowMenu()

Func ClientToScreen($hWnd, ByRef $x, ByRef $y); Convert the client (GUI) coordinates to screen (desktop) coordinates -----
    local $stPoint = DllStructCreate("int;int")

    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    dllcall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))

    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc; end ClientToScreen()

Func TrackPopupMenu($hWnd, $hMenu, $x, $y); Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd) --
    dllcall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc; end TrackPopupMenu()
Edited by Xandy

I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

How about something like this, based on Holger's example @ 

Creates a tray menu that will popup on the screen wherever your cursor is when you press your hotkey combo. I used the concept to create an app that would paste predefined items into different programs

#include <Constants.au3>

Opt("TrayMenuMode", 1)
Opt("WinTitleMatchMode", 4)

Global Const $TPM_BOTTOMALIGN = 0x0020

$item1 = TrayCreateItem("item_1")
$item2 = TrayCreateItem("item_2")
TrayCreateItem("") ; spacer
$aboutItem = TrayCreateItem("About")
$exitItem = TrayCreateItem("Exit")

HotKeySet("^+v", "ShowTrayMenu") ; Ctrl + Shift + V

While 1
    $Msg = TrayGetMsg()
    Switch $Msg
        Case $item1
            MsgBox(4096, "", "Item 1...")
        Case $item2
            MsgBox(4096, "", "Item 2...")
        Case $exitItem
            ExitLoop
        Case $aboutItem
            MsgBox(4096, "Info", "Just for fun...")
    EndSwitch
WEnd

Exit

Func ShowTrayMenu()
    Local $stPoint = DllStructCreate("int;int")
    DllCall("user32.dll", "int", "GetCursorPos", _
            "ptr", DllStructGetPtr($stPoint))

    DllCall("user32.dll", "int", "TrackPopupMenuEx", _
            "hwnd", TrayItemGetHandle(0), _
            "int", $TPM_BOTTOMALIGN, _
            "int", DllStructGetData($stPoint, 1), _
            "int", DllStructGetData($stPoint, 2), _
            "hwnd", WinGetHandle("classname=AutoIt v3"), _
            "ptr", 0)
EndFunc   ;==>ShowTrayMenu
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0