I'm trying to implement Vim-like hotkeys for my project; first and foremost, I've decided to realise j and k bindings, so I've wrote this KeyboardEvent
handler:
window.onkeydown = function( event ) {
if (event.code === 'KeyJ')
window.scrollBy({ top: 128, behavior: 'smooth' });
else if (event.code === 'KeyK')
window.scrollBy({ top: -128, behavior: 'smooth' });
};
Seems legit, right? Actually — no: when I holding j or k the scrolling process becomes disjointed and teared (also it's seems like the scroll speed is cutted in a half, like if 64
would be added to scrollTop
instead of 128
).
Can I somehow directly map k to ↑ and j to ↓ using plain JavaScript?
I've tried this solution:
const ArrowUp = new KeyboardEvent( 'keydown', { code: 'ArrowUp' } );
const ArrowDown = new KeyboardEvent( 'keydown', { code: 'ArrowDown' } );
window.onkeydown = function( event ) {
if (event.code === 'KeyJ')
window.dispatchEvent( ArrowDown );
else if (event.code === 'KeyK')
window.dispatchEvent( ArrowUp );
};
But it doesn't work at all, I do not even get any errors .