Jump to content

Arrow Up/Down key not able to select item from GUI ListView


Recommended Posts

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

Example()

Func Example()

    GUICreate("My GUI list") ; will create a dialog box that when displayed is centered

    Local $idMylist = GUICtrlCreateList("", 176, 32, 200, 97)
    GUICtrlSetData(-1, "Line 1 is selected.")
    GUICtrlSetData(-1, "Line 2 is selected.")
    GUICtrlSetData(-1, "Line 3 is selected.")
    Local $idLabel1 = GUICtrlCreatelabel("ListBox Message", 10, 72, 100, 25)

    Local $idListview = GUICtrlCreateListView("This is a ListView", 176, 232, 200, 150) ;,$LVS_SORTDESCENDING)
    Local $idItem1 = GUICtrlCreateListViewItem("Line 1 is selected.", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("Line 2 is selected.", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("Line 3 is selected.", $idListview)
    Local $idLabel2 = GUICtrlCreatelabel("ListView Message", 10, 272, 100, 25)
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idMylist
                Local $SelectItem1 = GUICtrlRead($idMylist)
                GUICtrlSetData($idLabel1, $SelectItem1)

            Case $idItem1, $idItem2, $idItem3       ; Only can be selected by Mouse, Arrow Up/Down key not working
                Local $SelectItem2 = ControlListView("My GUI list", "", $idListview, "GetSelected", 0)
                GUICtrlSetData($idLabel2, $SelectItem2)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Hi, I attached with the code at here. There are 1 ListBox and 1 ListView respectively. The ListBox is working perfect. I can select the items from both mouse click and arrow up/down key. But the ListView only can select items by mouse click. The arrow up/down key can't select items by keyboard. Unfortunately I must use ListView in my application. And I want both mouse and keyboard working in ListView selection. Can anybody help me how to solve? Thanks.

Link to post
Share on other sites

@kftang It works if we take care of the Up and Down keys (pressed while in the listview) by registering the WM_NOTIFY message :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <WinAPIvkeysConstants.au3>

Global $idListview, $idDummy_LV_Key

Example()

Func Example()

    GUICreate("My GUI list") ; will create a dialog box that when displayed is centered

    Local $idMylist = GUICtrlCreateList("", 176, 32, 200, 97)
    GUICtrlSetData(-1, "Line 0 is selected.")
    GUICtrlSetData(-1, "Line 1 is selected.")
    GUICtrlSetData(-1, "Line 2 is selected.")
    Local $idLabel1 = GUICtrlCreatelabel("ListBox Message", 10, 72, 100, 25)

    $idListview = GUICtrlCreateListView("This is a ListView", 176, 232, 200, 150) ;,$LVS_SORTDESCENDING)
    Local $idItem1 = GUICtrlCreateListViewItem("Line 0 is selected.", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("Line 1 is selected.", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("Line 2 is selected.", $idListview)
    Local $idLabel2 = GUICtrlCreatelabel("ListView Message", 10, 272, 100, 25)

    $idDummy_LV_Key = GUICtrlCreateDummy()

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $idMylist
                Local $SelectItem1 = GUICtrlRead($idMylist)
                GUICtrlSetData($idLabel1, $SelectItem1)

            Case $idItem1, $idItem2, $idItem3, $idDummy_LV_Key ; 3 for the mouse, 1 for the keyboard
                 Local $SelectItem2 = ControlListView("My GUI list", "", $idListview, "GetSelected", 0)
                 GUICtrlSetData($idLabel2, $SelectItem2)
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam

    Local $tNMHDR, $iIDFrom, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $iIDFrom
        Case $idListview
            Switch $iCode
                Case $LVN_KEYDOWN ; notifies a list-view control's parent window that a key has been pressed
                    Local $tInfo = DllStructCreate($tagNMLVKEYDOWN, $lParam)
                    Local $iVK = DllStructGetData($tInfo, "VKey")
                    Switch $iVK
                        Case $VK_UP, $VK_DOWN
                            GUICtrlSendToDummy($idDummy_LV_Key)
                    EndSwitch
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

If someone knows a solution not involving WM_NOTIFY, please share it here, thanks !

Link to post
Share on other sites

kftang, glad it worked for you :)

May I suggest to take care of 4 other important keys allowing you to jump from a listview item to another one. If they're not included in the script, then you won't get a correct result after keying any of them in the listview :

$VK_PRIOR (page up key) $VK_NEXT (page down key) $VK_HOME (home key) $VK_END (end key)

One line to change in the script to take care of them :

; Case $VK_UP, $VK_DOWN
Case $VK_UP, $VK_DOWN, $VK_PRIOR, $VK_NEXT, $VK_HOME, $VK_END

Also, in case you got plenty of listview items, creating each one immediately after the preceding one, resulting in a sequence of control id's with no gap inside the sequence, then one script line could be amended to make it easy :

; Case $idItem1, $idItem2, $idItem3, $idDummy_LV_Key ; 3 for the mouse, 1 for the keyboard
Case $idItem1 To $idItem3, $idDummy_LV_Key ; last one for the keyboard

Good luck


 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...