weixin_39638603
2020-12-25 23:17 阅读 0

Combining keydown with onClick

This is more of a question than an issue, but I wanted to know if it's possible to detect which keys are pressed within a function. I have a function which gets called onClick, but based on the keypresses it should work a little different (its a select functionality which should also work when pressing shift).

How would I, based on if the shift key is pressed, call a function to either just select one element or call another function to multi-select elements?

该提问来源于开源项目:glortho/react-keydown

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • weixin_39560245 weixin_39560245 2020-12-25 23:17

    you should be able to inspect the event argument that is passed to your decorated method to check the value of the shiftKey property.

    点赞 评论 复制链接分享
  • weixin_39638603 weixin_39638603 2020-12-25 23:17

    Thanks. I was just trying to see if the even has an shiftKey property. The problem that i'm facing now is that I get the click event and not the key event. My component structure looks something like this:

    
    <wrappercomponent> 
        <childcomponent></childcomponent>
    <wrappercomponent></wrappercomponent>
    </wrappercomponent>

    The ChildComponent renders another type of component and this component has an onClick. But this component doesn't seem to detect my keypresses at all. My onClick looks like this:

    
    ('shift')
    clickHandler(e) {
       doSomething();
    }
    

    But when I console.log the event it's just the click event and my props don't contain any keyPress even either.

    点赞 评论 复制链接分享
  • weixin_39560245 weixin_39560245 2020-12-25 23:17

    Ah I see. Yes, the keydown event only fires when at least one the keys pushed produces a character -- https://developer.mozilla.org/en-US/docs/Web/Events/keydown

    On Wed, Nov 7, 2018 at 1:36 PM Bobvantpadje wrote:

    Thanks. I was just trying to see if the even has an shiftKey property. The problem that i'm facing now is that I get the click event and not the key event. My component structure looks something like this:

    The ChildComponent renders another type of component and this component has an onClick. But this component doesn't seem to detect my keypresses at all. My onClick looks like this:

    ('shift') clickHandler(e) { doSomething(); }

    But when I console.log the event it's just the click event and my props don't contain any keyPress even either.

    — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/glortho/react-keydown/issues/89#issuecomment-436731032, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgHzO7GkRMQ7ndLL8S-NXhKfn2pjKasks5usyhEgaJpZM4YSfJS .

    点赞 评论 复制链接分享
  • weixin_39638603 weixin_39638603 2020-12-25 23:17

    Ahh oke, thanks for your help!

    点赞 评论 复制链接分享

相关推荐