1

How to repeat behaviour CMD+arrowLeft (Home) and CMD+arrowRight (End) for fn+arrowLeft and fn+arrowRight as well. Please write code if existed another approach to move caret (cursor) to the begin or to the end of input field use combination of keys. I use MacOs.

  • 36 - event.key for fn+arrowLeft
  • 35 - event.key for fn+arrowRight
const handleKey = (e) => {
    if (e.metaKey || e.altKey || e.ctrlKey) {
      e.preventDefault();
    }
    if (e.key === 'Home') {
      // code here
    }
    if (e.key === 'End') {
      // code here
    }
  };
<input type="text" onkeydown="handleKey()">

As alternative way but doesn’t cover my need: Set keyboard caret position in html textbox

I assumed that good way but don’t know how use right KeyboardEvent object: how to set keycode value while triggering keypress event manually in pure javascript?

  • Could you explain the *"doesn’t cover my need"* part? – T J May 03 '21 at 22:27
  • @TJ I mean theirs approaches are close to resolve my problem but I can’t apply it to my code, for example: **KeyboardEvent** object read-only however **CMD+arrowLeft** should replace by **fn+arrowLeft** - that is problem. – Maxim Gordiyenko May 03 '21 at 23:06
  • 1
    I don't think the OS (or the browser) can "see" the fn key. The keyboared does translation and then sends those results to the OS (and browser). So, if fn+left gets you a home key press, you'd want to detect a home key press (without meta, alt, or ctrl). https://keycode.info/ can tell you what the browser is seeing. – Ouroborus May 04 '21 at 04:08

0 Answers0