Check if the Control key is pressed during the event in JavaScript

Description

The following code shows how to check if the Control key is pressed during the event.

Example


<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
  function handleEvent(oEvent) {<!--from   ww  w .  j  a  v  a2  s.c om-->
    var oTextbox = document.getElementById("txt1");
    oTextbox.value += "\n>" + oEvent.type;
    oTextbox.value += "\n    charCode is " + oEvent.charCode;

    var arrKeys = [];
    if (oEvent.ctrlKey) {
      arrKeys.push("Ctrl");
    }
    oTextbox.value += "\n    keys down are " + arrKeys;
  }
</script>
</head>
<body>
  <P>Type some characters into the first textbox.</p>
  <P>
    <textarea id="txtInput" rows="15" cols="50"
      onkeypress="handleEvent(event)"></textarea>
  </p>
  <P>
    <textarea id="txt1" rows="15" cols="50"></textarea>
  </p>
</body>
</html>

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window