简体   繁体   中英

How do i detect if the down arrow key is pressed for quite some time?

I have a problem that I am not sure how to solve... I have a listbox that has potentially a lot of items.

When the user selects an item, i make an async call and get some data and populate a screen. The user can press the DOWN arrow key or the UP arrow key to navigate between items, the problem is when the user KEEPS the DOWN arrow key pressed, the selection changes fast and my application runs into problems....is there a clean way to detect that the user has kept the down key pressed for sometime, suspend updating the view, and update it only after he releases it?

I'm sorry if i was not clear in explaining this,

Are you trying to asynchronously ...

  1. ... update the collection populating your listbox, or ...
  2. ... update a separate view based on the currently selected item in your listbox?

I am guessing at #2 and that you run into problems when you start a lot of asynchronous calls to update the separate view. As @Ramhound points out, there is no difference between changing selection very quickly by holding down the button and by tapping the same button rapidly (or just by clicking away as a korean brood war pro-gamer). To fix both you can do the following:

  1. Set up a timer that you reset on selection changed. Only start your asynchronous query after it times out. This will filter out the very rapid selection changes. AND
  2. Make sure you only have one ongoing query at a time, so before starting a new asynchronous call, cancel and clean up the previous one.

This way the user can do whatever with the selection, you still only update once she has actually picked something. Tweak around with the timings to make sure that the UI is still snappy :)

If the control is focusable you can override UIElement.OnKeyDown

So in this case, that would mean essentially creating a subclass of the listbox, and then overriding the OnKeyDown function. You could catch it this way, but that makes things a lot more messy.

Or you can just query the key state using Keyboard.GetKeyStates .

IMHO though, you should just preload the data. ListBoxes use a 'virtualizing stack panel' which are smart enough not to display information that isn't needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM