当前位置: 代码迷 >> JavaScript >> js 擒获ctrl + c / ctrl + v
  详细解决方案

js 擒获ctrl + c / ctrl + v

热度:800   发布时间:2012-11-23 22:54:33.0
js 捕获ctrl + c / ctrl + v
<html>
<header>
<script>
function keypress()
{
var keyCode = window.event.keyCode;

var opCell = window.event.srcElement;

if(keyCode == "22")
{
// press Ctrl+V keyboard
var txt = clipboardData.getData('text');
if(txt == null) {txt = ""};
opCell.innerText = txt;

}else if(keyCode == "26")
{
// press Ctrl+Z keyboard (ctrl+z只是简单的将这个单元格的值清空,要想恢复上一次值,请用右键)
opCell.innerHTML = " ";
}else
{
if(window.event.srcElement.tagName == "TD"){
mygrid.editCell(window.event);
}
}
}

function keyup()
{
var keyCode = window.event.keyCode;

/* Ctrl+C 事件要在onKeyup事件中捕获 */
if(keyCode == "67")
{
// press Ctrl+C keyboard
var opCell = window.event.srcElement;
clipboardData.setData("text",opCell.innerText);
}
}
</script>
</header>

<body onKeypress="keypress()" onKeyup="keyup();">
</body>
</html>
  相关解决方案