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

Capturing the charCode for CTRL (or any modifier/special key) + [any key] seems unclear #4159

Closed
benhowdle89 opened this issue Jun 17, 2015 · 10 comments

Comments

@benhowdle89
Copy link

I have a contentEditable element that I'm trying to capture key combos in, ie. CTRL + c.

Using onKeyPress doesn't seem possible because special keys aren't reported. onKeyDown also fails because you get one event for the CTRL key and one for the next key. I could check if ctrlKey is true, however that doesn't help me with obtaining the charCode for the key combo.

A JSBin demo of what I'm trying to achieve: http://jsbin.com/girusifiba/edit?html,js,console,output (type CTRL + c in the editable <div />)

Any advice on how to proceed would be appreciated!

@sophiebits
Copy link
Collaborator

I don't think you can reliably get charCode for non-keypress events. For keyboard shortcuts, you probably have to use keyCode. @salier might be able to say more, but I'm going to close this out because unfortunately, I don't think React can help you.

@benhowdle89
Copy link
Author

Is there any advisable route to take to use native DOM events, ie. keypress within a React component? Just seems strange for React to remove this functionality (charCode for key combinations)...

@sophiebits
Copy link
Collaborator

You can always do

var node = React.findDOMNode(this);
node.addEventListener(...);

in a componentDidMount (and remove it in componentWillUnmount). I am fairly sure that charCode won't work across browsers though, so be sure to test.

@benhowdle89
Copy link
Author

Thanks, shall do that for now! Seems a bit in limbo as charCode is deprecated, and KeyboardEvent.key hasn't got great browser support...

@sophiebits
Copy link
Collaborator

key is roughly equivalent to keyCode; it doesn't depend on keyboard layout like charCode does. If key would work for you, keyCode will too.

@benhowdle89
Copy link
Author

Sure thing. For the moment, forgetting React exists, how would you go about capturing CTRL + c and getting the ASCII decimal code for those two keys (together)? Using keyPress + charCode, right? But if we're saying that charCode is unreliable, are we also saying that there's no modern, reliable way to get this ASCII value?

@benhowdle89
Copy link
Author

Sorry if this has gone off-topic from React issues, it just seems strange to me that in 2015, we can't do this reliably in JS.

@sophiebits
Copy link
Collaborator

I'd listen to keydown and do

e.keyCode === 'C'.charCodeAt(0) && e.ctrlKey

which should also work in React.

@blainekasten
Copy link
Contributor

Shameless plug, https://github.com/blainekasten/shortcut.js might help. With this you should be able to do something like:

componentDidMount() {
  shortcut('ctrl c', React.findDOMNode(this.refs.textarea)).bindsTo(this.ctrlCFn);
}

It's been a bit since i've used that library. But I plan on working on it more again soon to clean up some of it's code.

@Boorj
Copy link

Boorj commented Jun 12, 2016

Also there's a reactjs wrapper for Keypress.js : https://github.com/zzarcon/react-keypress

Here's a quote about how it works:

Lets say that you want to fire a function when the user press shift + enter + a:

  var React = require("react");
  var Keypress = require("react-keypress");

  React.createClass({
    sayHello() {
      alert("hello!");
    },

    render() {
      <div>
        <input type="text" onKeyPress={Keypress("shift enter a", this.sayHello)}/>
      </div>
    }
  });

But for myself i was looking for a solution without dependencies. So you can use conditions like if (ev.key ==='Enter' && ev.ctrlKey) . Also ev.keyCode gives a proper key number (don't know about exceptional cases, but suppose it has a workaround implemented anyway)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants