Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easy to bind to "?" #5

Closed
jamesarosen opened this issue Dec 4, 2011 · 16 comments
Closed

Make it easy to bind to "?" #5

jamesarosen opened this issue Dec 4, 2011 · 16 comments
Labels
Request Feature request or idea related issues.

Comments

@jamesarosen
Copy link
Contributor

The only way to bind to ? currently is to bind to "shift-slash," which only works on some keyboards. The trouble is that the only way to do a real binding to ? is to use onKeyPress, instead of onKeyUp, which doesn't work in other cases. This is a known problem area for nearly all keybinding libraries. I'd love to hear your thoughts on how to do this elegantly.

@RobertWHurst
Copy link
Owner

I've been thinking about special keys like ?. I would have to redesign the library's key look up system to use a hidden input to sniff the character pressed. I'm not sure I will do this because it feels unclean.

@jamesarosen
Copy link
Contributor Author

How about at least providing a couple of hooks so that users who want to do special key handling can?

@RobertWHurst
Copy link
Owner

Sure, that's easy enough. I'll likely add hooks the next release.

See issue #7

@jamesarosen
Copy link
Contributor Author

I'll take a look at the hooks issue. You'll notice that when I raise the same issue on Keymaster, it went nowhere. This is not an easy thing to solve while keeping a low API profile.

@RobertWHurst
Copy link
Owner

Yea I saw you grabbed my Hooks branch. Is that kind of what your looking for?

@RobertWHurst RobertWHurst reopened this Dec 5, 2011
@RobertWHurst
Copy link
Owner

Are you still looking for this @jamesarosen ?

@jamesarosen
Copy link
Contributor Author

I'm definitely interested, but I haven't had a chance to investigate it myself.

@RobertWHurst
Copy link
Owner

Are you essentially looking for a way to bind a hook that lets you modify the active keys and combos arrays? Or are you looking for a way to modify the event object passed to event handlers?

@jamesarosen
Copy link
Contributor Author

Ideally:

// This knows that `?` can't be bound with keydown and keyup,
// but must use keypress.
KeyboardJS.bind.key('?', function() { ... });

Just fine:

KeyboardJS.bind.keypress('?', function() { ... });

Also totally acceptable, though this seems like extra setup work that many people would have to do:

KeyboardJS.bind.parseEvents = function(combo, onKeyDown, onKeyUp) {
  if (combo === '?') { return { keypress: onKeyDown }; }
  return { keydown: onKeyDown, keyup: onKeyUp };
};
...
KeyboardJS.bind.key('?', function() { ... });

@RobertWHurst
Copy link
Owner

Ah... Ok I'm on it.

@RobertWHurst
Copy link
Owner

Fixed in v0.3.0. You can bind to ? off the bat with KeyboardJS.on('?', function() {});. If you still wish to add more macro keys use KeyboardJS.macro(keyCombo, [macrokey1, macrokey2, ...]);. Here's an example for '?'.

KeyboardJS.macro('shift + /', ['questionmark', '?']);

Note that '?' is already part of the US locale out of the box, but if it wasn't, the above would add it. Once a macro is added you can use it in you key combos like any other key name.

@Prinzhorn
Copy link
Contributor

I assume a lot has changed since 2012. I'm trying to get "?" working with a German keyboard layout

This does not get me anywhere, neither "ß" nor "shift + ß" work.

keyboardJS._locale.bindKeyCode(223, ['ß']);
keyboardJS._locale.bindMacro('shift + ß', ['?']);

keyboardJS.bind('ß', console.log.bind(console, 'ß'));
keyboardJS.bind('shift + ?', console.log.bind(console, 'shift + ?'));
keyboardJS.bind('?', console.log.bind(console, '?'));

Source: https://en.wikipedia.org/wiki/German_keyboard_layout

@daniel-wer
Copy link
Contributor

@Prinzhorn In case you're still interested or for anyone else who stumbles upon this, the code snippet above did not work, because the keyCode for ß is actually 219.

Note, however, that there is a bug in the current keyboardJS version (see #164) which affects shortcuts using secondary key symbols (as ?) which is why only the first of the three bindings works.

@Prinzhorn
Copy link
Contributor

Prinzhorn commented Feb 23, 2021

@daniel-wer I'm currently not using KeyboardJS in any project, but I might be in the future. Thanks for looking into this! How did you come up with 219? See screenshot, this is me hitting the "ß" key on this very GitHub page.

Selection_820

@daniel-wer
Copy link
Contributor

Unfortunately, keyboard events are a mess and the keyCode is different between keydown/keyup and keypress events, because they are different types of events. There is hope, though, in the form of UI events which should make all of this easier to work with.

If you use document.addEventListener("keyup", e => console.log(e.which, e.keyCode, e.key)) you will get keyCode 219 for ß which seems to be what keyboardJS is using. In another issue I read that @RobertWHurst plans to switch to UI events, but I'm not sure whether any work in this direction has been done yet.

@Prinzhorn
Copy link
Contributor

If you use document.addEventListener("keyup", e => console.log(e.which, e.keyCode, e.key)) you will get keyCode 219 for ß

Oh happy day 😄

Selection_822

This is Firefox. In Chromium it's indeed 219.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Request Feature request or idea related issues.
Projects
None yet
Development

No branches or pull requests

4 participants