glacambre / firenvim

Embed Neovim in Chrome, Firefox & others.
GNU General Public License v3.0
4.81k stars 147 forks source link

Browser doesn't provide enough information about key presses #640

Open smackesey opened 4 years ago

smackesey commented 4 years ago

M- and A- mappings don't work in my Firenvim setup. I gather this has something to do with the way that the browser handles the alt key on MacOS. Some Neovim GUIs like VimR have solved this problem by providing a "use alt as meta" option at the application level. Is there any way to do something similar for FireNvim (or perhaps at the level of Chrome itself)?

glacambre commented 4 years ago

Does changing your alt setting to all fix this issue?

smackesey commented 4 years ago

Nope, no effect.

smackesey commented 4 years ago

This same problem exists by default in Neovim GUIs on mac. On Mac, by default option (i.e. alt) causes output of special characters. See here.

In terminal apps on mac (e.g. iTerm, Terminal) and some Vim/Neovim GUIs (Macvim, VimR), there is typically an option to "set option as meta", which changes the usual interpretation of the option key across the application. But I'm not sure how this is implemented.

To fix the problem for Firenvim, a similar translation layer may be needed. I'm interested in fixing this. But when I'm typing in a firenvim instance, are the keys being routed through the browser at all, or are they just being sent straight to Neovim?

glacambre commented 4 years ago

The browser sees the event, translates it into a representation Neovim understands and then sends it to neovim with nvim_input. This is done in two places:

So I guess you'll only need to apply changes to the keydown handler (I'm still a bit fuzzy on what exactly would need to be done, so I can't give more directions than that, sorry). I'd like to avoid adding a new setting, but if that's impossible you can add one by copying what happens here. I'm not sure you'll need it but here's documentation about what key results in what modifiers in browsers: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState .

Feel free to ask any other question you may have - the input-handling code is one of the first things I wrote and it's probably not as documented as it should be.

smackesey commented 4 years ago

OK, I investigated this problem for several hours and I'm not aware of any good solutions. The problem is that it is impossible to receive alt-modified regular character keydown events in the browser in MacOS by default. That's because alt- always generates special characters in the key field of the keydown event. I experimented with a solution that converted the keystroke event key to the "plain" character, but this is undesirable because the conversion is specific to a keyboard layout. Ideally there would be a way to prevent the special character functionality at the application level, which is what MacOS terminal apps and Vim GUIs do, but since this is a browser extension rather than a standalone application, we are restricted to the browser's key events, so I don't think this is possible.

I did find a workaround outside of the scope of firenvim, and I submitted a PR with some changes to the README section on MacOS special chars that explains the problem and the workarounds.