如何听svg上的键盘事件 [英] How to listen keyboard events on svg

查看:565
本文介绍了如何听svg上的键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个svg,我可以在这个svg上绘制多个形状。现在我的要求是我想听类似ctrl + C,ctrl + V,ctrl + D,Esc,删除键盘事件,以便我可以复制,粘贴,重复选择的形状。但我不知道在SVG上听键盘事件。我尝试下面的代码但没有运气!

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.

推荐答案

因为SVG不是输入类型,所以要监听窗口上的事件:

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

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

这篇关于如何听svg上的键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆