Tuesday, 13 August 2013

Control key + character not being handled in IE

Control key + character not being handled in IE

In attempting to listen to a user click the CTRL + V key (paste) in an
input field, we've run into a problem getting IE to play nice. This
solution needs to be cross-browser compatible and by all accounts, the
code we're using IS cross-browser capable and yet we run into an issue
ONLY when holding down the CTRL key in IE.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<input type="text" id="mytest" />
<script type="text/javascript">
window.onload = function()
{
var main_box = document.getElementById('mytest');
window.addEventListener('keypress',
function(evt)
{
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var charStr = String.fromCharCode(charCode);
if(evt.ctrlKey && (charStr == 'v' || charStr == 'V'))
{
alert("CTRL+V pressed.");
}
}
);
}
</script>
</body>
</html>
Does anyone have any idea on how to fix the issue with IE?

No comments:

Post a Comment