How to listen keyboard events on svg

余生长醉 提交于 2019-11-28 05:49:43

问题


I have a svg and I can draw multiple shapes on this svg. Now my requirement is I want to listen keyboard events like ctrl+C, ctrl+V, ctrl+D, Esc, Delete so that I can copy, paste , duplicate selected shape. But I have no idea about listening keyboard events on SVG . I tried following code but no luck !!

 mySVG.on("keydown", function () {
        //code to handle keydown
  });

Any help ? Thanks in advance.


回答1:


Because SVG is not an input-type, listen for the event on the window instead:

$(window).on('keypress', function (evt){ ... })


来源:https://stackoverflow.com/questions/28323977/how-to-listen-keyboard-events-on-svg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!