RobertWHurst / Keystrokes

Keystrokes as an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.
MIT License
156 stars 6 forks source link

Arrow keys not detected with checkKey #40

Closed Elliot67 closed 7 months ago

Elliot67 commented 7 months ago

Arrow keys are not detected in checkKey , but they are properly detected with bindKey. Using checkKeyCombo works fine.

Made a reproduction on jsfiddle with the following code :

import * as rwhkeystrokes from 'https://esm.run/@rwh/keystrokes';

rwhkeystrokes.bindKey('a', () => console.log('bind a'))
rwhkeystrokes.bindKey('ArrowRight', () => console.log('bind ArrowRight'))

const int = window.setInterval(() => {
  if (rwhkeystrokes.checkKey('a')) {
    console.log('a is pressed')
  }

  if (rwhkeystrokes.checkKey('ArrowRight')) {
  //if (rwhkeystrokes.checkKeyCombo('ArrowRight')) {
    console.log('ArrowRight is pressed')
  }
}, 100)
RobertWHurst commented 7 months ago

Indeed. The library is case insensitive (or at least it's suppose to be), and yet here checkKey is not properly normalizing the input. It works if you pass 'arrowright'. I'll get a new version with a fix published today. Thanks for reporting this bug 👍🏻