I have a bunch of multiline contenteditable divs arranged vertically, and I want to allow for natural navigation between them by arrow keys (as if it was one document). For that, on keydown
event I need to:
- Know current row of caret and number of rows to determine if we need to move up (first line and ↑ key pressed) or down (last line and ↓ key)
- Know current character (position in a shown string) to determine if we need to move up (position==0 and ← key pressed) or down (position==text.length and → pressed)
- The process should not stop between switching elements when the key is being held and not released (hence
keydown
event, notkeyup
) - Preferrably: The event should stop propagating (for example, if I'm on the first column on last row and I press ↓ key, it should not jump to the last character on the line and then go down)
- Preferrably (would be really awesome): After we jump to the next element, we would not just
.focus()
the element, but emulate a click in the same vertical position as where we were before, so that it would feel natural, like in text editors.
All scripts/libraries I had found to date are either not doing all things I need or buggy. Please include demos in your suggestions, so that I can test without incorporating in my code first. Thanks!
Update: Visual explanation - note that there are more than 2 divs and 'arrow down key on the last line' is just one of the four triggers