最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to properly create a cmd+K keyboard shortcut in react? - Stack Overflow

programmeradmin4浏览0评论

I made a keydown/keyup script that fires a search bar when pressing cmd+k. The issue I'm facing is that the browser doesn't recognize when I'm holding the cmd key. For the "K" key, there's seem to be an autorepeat by default thus allowing me to simply hold "K" and pressing Cmd to show the search bar.

I want the reverse. How can I hold Cmd and press another key (K) to fire my search bar?

class Dashboard extends Component {
    
    state = {
        showSearch: "",
        HideShowSearch: false,
    };


HideShowSearch = (e) => {
        var map = {};
        let HideShowSearch = this.state.HideShowSearch;
        var self = this;
        onkeydown = function (e) {
            e = e;

            map[e.keyCode] = e.type === "keydown";

            // Cmd + K
            if (map[75] && map[91]) {
                self.setState(() => ({ HideShowSearch: !HideShowSearch }));
            }
        };
    };

render() {
        this.HideShowSearch();
        return (
            <div>
                <div className="row align-items-center">
                    <div className="container col-12">
                        {this.state.HideShowSearch ? (
                            <Searchbox
                                onChange={this.onTextChanged}
                                item={this.state.items}
                                onKeyPress={this.handleKeyPress}
                                text={this.state.text}
                                suggestions={this.renderSuggestions()}
                                onKeyDown={this.onKeyDown}
                            />
                        ) : null}
                    
                    </div>
                </div>
            </div>
        );
    }

I made a keydown/keyup script that fires a search bar when pressing cmd+k. The issue I'm facing is that the browser doesn't recognize when I'm holding the cmd key. For the "K" key, there's seem to be an autorepeat by default thus allowing me to simply hold "K" and pressing Cmd to show the search bar.

I want the reverse. How can I hold Cmd and press another key (K) to fire my search bar?

class Dashboard extends Component {
    
    state = {
        showSearch: "",
        HideShowSearch: false,
    };


HideShowSearch = (e) => {
        var map = {};
        let HideShowSearch = this.state.HideShowSearch;
        var self = this;
        onkeydown = function (e) {
            e = e;

            map[e.keyCode] = e.type === "keydown";

            // Cmd + K
            if (map[75] && map[91]) {
                self.setState(() => ({ HideShowSearch: !HideShowSearch }));
            }
        };
    };

render() {
        this.HideShowSearch();
        return (
            <div>
                <div className="row align-items-center">
                    <div className="container col-12">
                        {this.state.HideShowSearch ? (
                            <Searchbox
                                onChange={this.onTextChanged}
                                item={this.state.items}
                                onKeyPress={this.handleKeyPress}
                                text={this.state.text}
                                suggestions={this.renderSuggestions()}
                                onKeyDown={this.onKeyDown}
                            />
                        ) : null}
                    
                    </div>
                </div>
            </div>
        );
    }

Share Improve this question asked Jul 17, 2020 at 4:58 Dom355Dom355 3013 silver badges18 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

e.keyCode === 75 && e.metaKey produces the described behavior in both directions (not quite reverse)

class Test extends React.Component {

  handleKeyDown = (e) => {
    if (e.keyCode === 75 && e.metaKey) {
      console.log("search")
    }
  };

  render() {
    return <input onKeyDown = {this.handleKeyDown}/>
  }

}

ReactDOM.render( < Test / > , document.getElementById("root"));
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

发布评论

评论列表(0)

  1. 暂无评论