0

Is there a good way, in JavaScript, to recognize pressing the up arrow key when the cursor is on the first line of a text area?

Imagine that I want to mimic the behaviour of a terminal, where pressing up will cycle back through previously entered commands, but only if the cursor is on the top line of a potentially multi-line command.

Image
  • 1
  • 1
  • 1
    Does this answer your question? [Test if cursor is on the first line in a textarea (with soft-newlines)](https://stackoverflow.com/questions/11851266/test-if-cursor-is-on-the-first-line-in-a-textarea-with-soft-newlines) – iamcastelli May 13 '20 at 22:02
  • I saw that before posting this question, but both proposed examples have issues because they rely on pixel counting and assumptions about width of text. Since the "cols" attr on a textarea is based on the average character width, the first solution will fail on text containing mostly w's or mostly i's. The second relies on the pixel height of a width of text which is not really a constant. – Image May 14 '20 at 16:53

0 Answers0