如何在 svg 上监听键盘事件 [英] How to listen keyboard events on svg
本文介绍了如何在 svg 上监听键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 svg,我可以在这个 svg 上绘制多个形状.现在我的要求是我想听键盘事件,如 ctrl+C、ctrl+V、ctrl+D、Esc、Delete,以便我可以复制、粘贴、复制选定的形状.但我不知道在 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 不是 input-type,所以监听窗口上的事件:
Because SVG is not an input-type, listen for the event on the window instead:
$(window).on('keypress', function (evt){ ... })
这篇关于如何在 svg 上监听键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文