keyCode problem.
Posted January 17th 2012, 11:05pm
Okay, I'm trying to make ctrl + enter as enter, and press enter to submit the form in textarea. But my problem is when I pressed ctrl + enter, it made a line break and submited a form, i guess there is a problem with my first script that i cant figure out how to fix it...
btw, here is it: http://jsfiddle.net/P33FR/34/
φ
Posts: 209
Joined: September 10th 2011, 11:00pm
Location: SC, USA
Likes Given: 8
Likes Received: 8
keyCode problem.
Posted January 17th 2012, 11:42pm
You need to trap the onkeydown() and onkeyup() events. If you get key IDs of 17, then 13 in onkeydown(), and the Ctrl key was not released before the Enter key was pressed (you check this in onkeyup()), then the user has pressed Ctrl-Enter.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
keyCode problem.
Posted January 18th 2012, 10:42am
@Dion: Can you explain more details by code :D.
φ
Posts: 209
Joined: September 10th 2011, 11:00pm
Location: SC, USA
Likes Given: 8
Likes Received: 8
Re: keyCode problem.
Posted January 19th 2012, 12:36am
Here's a sample HTML file that demonstrates how to use and interpret keycodes, and checks for Enter and Ctrl-Enter:

<html>
<head>
  <script type="text/javascript">
  var ctrl=false;
  function kdn(e) {
    if (e.keyCode==17) {ctrl=true;}
    document.getElementById('fn1').innerHTML=e.keyCode;
  }
  function kup(e) {
    if (e.keyCode==17) {ctrl=false;}
    document.getElementById('fn2').innerHTML=e.keyCode;
  }
  function kpr(e) {
    if (e.keyCode==13 && ctrl) {var key='Ctrl-Enter pressed';}
    else if (e.keyCode==13 && !ctrl) {var key='Enter pressed';}
    else {var key='Who cares';}
    document.getElementById('fn3').innerHTML=e.keyCode;
    document.getElementById('fn4').innerHTML=key;
  }
 </script>
</head>
<body>
  <div>Enter some text: <input type="text" onkeydown="kdn(event)" onkeyup="kup(event)" onkeypress="kpr(event)" /></div>
  <div id="fn1"></div>
  <div id="fn2"></div>
  <div id="fn3"></div>
  <div id="fn4"></div>
</body>
</html>
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
keyCode problem.
Posted January 19th 2012, 10:39am
Thanks you so much :D
φ
Posts: 209
Joined: September 10th 2011, 11:00pm
Location: SC, USA
Likes Given: 8
Likes Received: 8

Who is online

Users browsing this forum: No registered users and 0 guests