views:

87

answers:

4

I'm trying to replicate an HTML SELECT element (drop-down-list or combobox) in Flash (AS3).

In most browsers, when you have one in focus and you type something, the combobox will try to find the value within its options and show the closest one. I was wondering what kind of algorithm is used for that. I don't think its Levenshtein or similar since it only works with the beginning of the string.

I'm thinking that it works by keeping a buffer of what has been written, and tries to find a string starting with that... if it doesn't find anything, it clears the buffer, and searches a string beginning with the last character pressed.

I already prototyped this, and it works quite ok, with one caveat... In HTML, when you repeatedly press the same key, it will rotate between all options starting with that character. I think I could fix this, but was wondering if someone has already done it, to compare the algorithms... its turning into quite a complex code to test and debug, and I'm not sure I'm covering all the possibilities...

A: 

I don't know what algorithm is used in the browsers but one that comes to mind that would do what you want is sequence alignment or a longest common subsequence algorithm. It would allow you to match any part of the string to any other part of the string (with possible gaps between the matched sub strings). It's not massively quick though.

http://en.wikipedia.org/wiki/Sequence_alignment

there's also some very good lecture videos online at MIT open course ware

http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/VideoLectures/detail/embed15.htm

Matti
+2  A: 

Just did some tests on firefox, and I noticed (this is not official information, just pure speculation):

  • Key pressed event:
    • Esc (firefox only): clear buffer
    • Arrow up/down: move up/down on the list, clear buffer
    • Page up/down: move up/down by 20 on the list, clear buffer
    • Home: move to the top of the list, clear buffer
    • End: move to the end of the list, clear buffer
    • Other:
      • Empty buffer?
        • Yes: add key to buffer
      • buffer.length == 1 AND key is same as last key pressed?
        • Yes: go on next item starting with key.
        • No: add key to buffer and search next item starting with buffer.
  • 1.5 seconds passed event: clear buffer

This will need a timer of course.

Alex Bagnolini
+4  A: 

The reaction of form widgets to keyboard interaction is not standardised and different browsers don't agree. This is always an issue when creating ersatz form controls from script.

In HTML, when you repeatedly press the same key, it will rotate between all options starting with that character.

This feature comes from Windows and is quite unintuitive. The exact rule isn't that exactly, is quite obscure, and gives different results in IE and Opera versus the other browsers.

IMO this behaviour is highly undesirable. Since no average user is going to be able to predict how the rule works, I personally would leave it out and simply select the first option to match the typed leftstring. This is easier for you to code and easier for the user to understand.

bobince
Yeah, I thought about this since I only use the first behaviour... but in some informal user testings for another project we found out that most users (specially inexpirienced ones) used exclusively the single character rotation... I guess its because most of them type slower than the buffer auto-clear. I think if I were to pick one of the two behaviours, the 2nd one seems a bit more intuitive and probably requires less expertice from the user. However, I still feel that both can work together seamlessly...
Cay
Well, they don't work together seamlessly in browsers, and it's really hard to see how it ever could. If the list contains "oats", "orange" and "oolong" and you hit O twice, what should be selected? Is it dependent on timing? Is this a good design?Either match on the full prefix or just on the first lett. Don't try to do both.
Bennett McElwee
@Bennett: it's dependent on listing order and browser. With that example in that order, `oo` would select ‘orange’ on Firefox/Webkit and ‘oolong’ on IE/Opera, if no option starting with ‘o’ was selected at the beginning. No, it is not in any way a good design, but that's the way it has always worked in Windows.
bobince
Yes. Since current behaviour is inconsistent anyway, the OP doesn't have to worry about being inconsistent with existing practice. So he should go with the simple approach. Either implement the control to match the full prefix, or implement it to match the first letter.
Bennett McElwee
A: 

In HTML, when you repeatedly press the same key, it will rotate between all options starting with that character. I think I could fix this, but was wondering if someone has already done it, to compare the algorithms

You might want to reset to the top of the dropdown after every key press and then search for the appended buffer.

Algorist