none
Select an item in ListBox through keyboard

    Question

  • In a Metro/XAML app. We're trying to let user being able to navigate a ListBox control with keyboard and select an item pressing Enter Key. We have implemented KeyDown event, but Enter key is not intercepted at all.

    Any suggestion?

    Thanks in advance,

    Ivan

    Wednesday, June 06, 2012 2:32 PM

Answers

  • Great points Stefan!

    Ivan - note that our controls are keyboard accessible by default (we have gone through great lengths!). To make focus move independently from selection you must hold down the Ctrl key. This is more of a legacy Win32 behavior. For the list controls in XAML there isn't a property to "by-default" make selection move separately from focus (though it is something I hope can come sometime after Windows 8). Note when you SelectionMode=Multiple the default behavior does have focus moving independently from selection. You could use Multiple selection mode and write code to try and keep only one item selected. Just a thought if you don't like the Ctrl key moving focus behavior.

    Hope this helps,
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

    Thursday, June 07, 2012 8:08 PM

All replies

  • Hey Ivan,

    Have a look at the quick Debug traces below of random key presses :) It seems weird but the KeyDown does not fire for special keys. Maybe you can use KeyUp event.

    ListBox Key Up:Enter
    ListBox Key Up:Up
    ListBox Key Up:Enter
    ListBox Key Up:Down
    ListBox Key Up:Down
    ListBox Key Down:F
    ListBox Key Up:F
    ListBox Key Down:D
    ListBox Key Up:D
    ListBox Key Up:Enter
    ListBox Key Up:Down
    ListBox Key Up:Down

    Hope it helps.


    Can Bilgin
    Blog CompuSight


    • Edited by Can Bilgin Wednesday, June 06, 2012 8:07 PM additional info
    • Proposed as answer by Can Bilgin Thursday, June 07, 2012 4:40 PM
    Wednesday, June 06, 2012 2:59 PM
  • Not sure I understand. Is the request to have focus and selection be separate from each other in ListBox? Do you want single selection or multiple selection? 

    You won't get _KeyDown_ for the Enter key (or alot of keys) since the ListBox handles that key stroke to select.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"
    Wednesday, June 06, 2012 7:39 PM
  • Mark,

    I think that he wants to create a keyboard accessible application, which I understand. If you have single selection and the user moves up and down the list, when they decide which item they want to do some work with they might press enter, which is the equivalent of maybe double-clicking or clicking.  When enter is pressed he needs to be notified so that he can then process the users action. If enter is eaten by the control then that is not possible. However I assume that AddHandler() would work to get the event, even if it has been handled, which would be a solution in this particular case.

    But I think I understand what he is trying to do, and it would be great to make that easy, because keyboard accessibility is something that seems to have been forgotten with all the work around supporting touch.

    ...Stefan

    Thursday, June 07, 2012 7:31 PM
  • Is there a specific reason for not using key up? Am i missing something :)

    Can Bilgin
    Blog CompuSight

    Thursday, June 07, 2012 7:57 PM
  • Great points Stefan!

    Ivan - note that our controls are keyboard accessible by default (we have gone through great lengths!). To make focus move independently from selection you must hold down the Ctrl key. This is more of a legacy Win32 behavior. For the list controls in XAML there isn't a property to "by-default" make selection move separately from focus (though it is something I hope can come sometime after Windows 8). Note when you SelectionMode=Multiple the default behavior does have focus moving independently from selection. You could use Multiple selection mode and write code to try and keep only one item selected. Just a thought if you don't like the Ctrl key moving focus behavior.

    Hope this helps,
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

    Thursday, June 07, 2012 8:08 PM
  • @Can,

    I think the thing about using key up, is it depends on what the user is expecting.  If they press the key down and hold down and nothing happens until they let go of the key, then it might just be slightly confusing.  I agree, most times no one is really likely to notice, but it is better to have the behaviour the way the user expects. AddHandler() should resolve that.

    ...Stefan

    Thursday, June 07, 2012 8:14 PM