How do I capture keystroke events in D3 force layout?

前端 未结 1 1583
天命终不由人
天命终不由人 2020-12-29 04:31

I would like to respond to keystroke events directed at nodes in my force layout. I\'ve tried adding all the variants of \"keystroke\", \"keypress\", \"keyup\", \"keydown\"

相关标签:
1条回答
  • 2020-12-29 04:41

    I think the problem here is you are trying to add keyboard events to elements that are not focusable, try adding a keydown event to a focusable element (body in this case):

    d3.select("body")
        .on("keydown", function() { ...
    

    here you can use properties of d3.event, for instance d3.event.keyCode, or for more specialized cases, d3.event.altKey, d3.event.ctrlKey, d3.event.shiftKey, etc..

    Looking at the KeyboardEvent Documentation might be helpful as well.

    I've made a simple fiddle with keyboard interaction here: http://jsfiddle.net/qAHC2/292/

    You can extend this to apply these keyboard interactions to svg elements by creating a variable to 'select' the current object:

    var currentObject = null;
    

    Then update this current object reference during appropriate mouse event methods:

    .on("mouseover", function() {currentObject = this;})
    .on("mouseout", function() {currentObject = null;});
    

    Now you can use this current object in your keyboard interactions set up earlier.

    here's a jsfiddle of this in action: http://jsfiddle.net/qAHC2/295/

    0 讨论(0)
提交回复
热议问题