Sign in to follow this  
Followers 0
lyledg

Hotkey and Control focus

5 posts in this topic

#1 ·  Posted (edited)

Hi all

I am trying to activate the hotkey function with using the delete key, but only want it usable if an item is highlighted and in focus in the listview

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Opt('MustDeclareVars', 1)

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

Global $LISTVIEW
Global Const $Yes = 6, $No = 7

HotKeySet("{Delete}", "_Removeitem")

_Main()

Func _Main()

    Local $GUI, $hImage
    $GUI = GUICreate("(External) ListView Create", 400, 300)

    $LISTVIEW = _GUICtrlListView_Create ($GUI, "", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle ($LISTVIEW, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState()
    
    
    ; Load images
    $hImage = _GUIImageList_Create ()
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0xFF0000, 16, 16))
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0x00FF00, 16, 16))
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList ($LISTVIEW, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn ($LISTVIEW, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn ($LISTVIEW, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn ($LISTVIEW, 2, "Column 3", 100)

    ; Add items
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 0, "Row 1: Col 2", 1)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 0, "Row 1: Col 3", 2)
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 1, "Row 2: Col 2", 1)
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 3: Col 1", 2)

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main
Func _Removeitem()
Local $win, $CurrentIndexSelected

    $Win = WinActive("(External) ListView Create")
    
    If $Win <> 0 Then
        
        $CurrentIndexSelected = _GUICtrlListView_GetNextItem($LISTVIEW) ;Get the item index in the listview
        If $CurrentIndexSelected <= -1 Then
            MsgBox(262192, "No items", "No items to delete")
        Else
            If MsgBox(262180, "Remove", "Are you sure you want to delete the highlighted item(s)?") = $Yes Then
                _GUICtrlListView_DeleteItemsSelected(ControlGetHandle("", "", $LISTVIEW))
            Else
                Return
            EndIf
        EndIf
        
    Else
        HotKeySet("{Delete}")
    EndIf
EndFunc

When I try and use the delete key to delete some data on say an input box whilst the gui is running, The Hotkey wants to delete am item out of the Listview

basically, how do I ensure that the Hotkey is ONLY used and associated with the listview items?

Edited by lyledg

Share this post


Link to post
Share on other sites



~ bump

Share this post


Link to post
Share on other sites

Unfortunately that does not work

Adjusted code below

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>

Opt('MustDeclareVars', 1)

$Debug_LV = False ; Check ClassName being passed to ListView functions, set to True and use a handle to another control to see it work

Global $LISTVIEW
Global Const $Yes = 6, $No = 7

HotKeySet("{Delete}", "_Removeitem")

_Main()

Func _Main()

    Local $GUI, $hImage
    $GUI = GUICreate("(External) ListView Create", 600, 500)

    $LISTVIEW = _GUICtrlListView_Create ($GUI, "", 2, 2, 394, 268)
    _GUICtrlListView_SetExtendedListViewStyle ($LISTVIEW, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))
    GUISetState()
    
    
    ; Load images
    $hImage = _GUIImageList_Create ()
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0xFF0000, 16, 16))
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0x00FF00, 16, 16))
    _GUIImageList_Add ($hImage, _GUICtrlListView_CreateSolidBitMap ($LISTVIEW, 0x0000FF, 16, 16))
    _GUICtrlListView_SetImageList ($LISTVIEW, $hImage, 1)

    ; Add columns
    _GUICtrlListView_InsertColumn ($LISTVIEW, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn ($LISTVIEW, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn ($LISTVIEW, 2, "Column 3", 100)

    ; Add items
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 0, "Row 1: Col 2", 1)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 0, "Row 1: Col 3", 2)
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem ($LISTVIEW, 1, "Row 2: Col 2", 1)
    _GUICtrlListView_AddItem ($LISTVIEW, "Row 3: Col 1", 2)

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main
Func _Removeitem()
Local $win, $CurrentIndexSelected, $State

    $Win = WinActive("(External) ListView Create")
    
    If $Win <> 0 Then
        $State = GUICtrlGetState($LISTVIEW)
    msgbox (0,"state",StringFormat("GUICtrlRead=%d\nGUICtrlGetState=%d",GUICtrlRead($LISTVIEW),GUICtrlGetState($LISTVIEW)))
        
        $CurrentIndexSelected = _GUICtrlListView_GetNextItem($LISTVIEW) ;Get the item index in the listview
        If $CurrentIndexSelected <= -1 Then
            MsgBox(262192, "No items", "No items to delete")
        Else
            If MsgBox(262180, "Remove", "Are you sure you want to delete the highlighted item(s)?") = $Yes Then
                _GUICtrlListView_DeleteItemsSelected(ControlGetHandle("", "", $LISTVIEW))
            Else
                Return
            EndIf
        EndIf
        
    Else
        HotKeySet("{Delete}")
    EndIf
EndFunc

Also, see attached document for screenshots

_GUICtrlListView_Create_focus.doc

Share this post


Link to post
Share on other sites

Solved!

#include <GuiConstants.au3>
$GUI = GUICreate("Test Edit")
$Edit = GUICtrlCreateEdit("", 20, 50, 150, 150)
$Button = GUICtrlCreateButton("Button", 20, 210)
GUICtrlSetState(-1, $Gui_Focus)
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
    EndSwitch
    If _IsFocused($GUI, $Edit) Then
        Sleep(20)
        HotKeySet("{TAB}", "TabPress")
    Else
        HotKeySet("{TAB}")
    EndIf
WEnd

Func TabPress()
    ;$OldClip = ClipGet()
    ;ClipPut(@TAB)
    ;Send("+{INSERT}")
    ;ClipPut($OldClip)
   
    Send("^{TAB}") ;This is the tip from Siao - Thanks!
EndFunc

Func _IsFocused($hWnd, $nCID)
    Return ControlGetHandle($hWnd, '', $nCID) = ControlGetHandle($hWnd, '', ControlGetFocus($hWnd))
EndFunc

Thanks to MSCreator and Siao

http://www.autoitscript.com/forum/index.ph...p;hl=_IsFocused

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