Update:
I finally figured out that "keypress" has a better compatibility than "keydown" or "keyup" on Linux platform. I just changed "keyup"/"keydown" to "keypress", so all went well.
I don't know what the reason is but it is solution for me. Thanks all who had responsed my question.
--
I have some codes that needs to detect key press event (I have to know when the user press Enter) with JQuery and here are the codes in Javascript:
j.input.bind("keyup", function (l) {
if (document.selection) {
g._ieCacheSelection = document.selection.createRange()
}
}).bind("keydown", function(l) {
//console.log(l.keyCode);
if (l.keyCode == 13) {
if(l.ctrlKey) {
g.insertCursorPos("\n");
return true;
} else {
var k = d(this),
n = k.val();
if(k.attr('intervalTime')) {
//alert('can not send');
k.css('color','red').val('Dont send too many messages').attr('disabled','disabled').css('color','red');
setTimeout(function(){k.css('color','').val(n).attr('disabled','').focus()},1000);
return
}
if(g_debug_num[parseInt(h.buddyInfo.id)]==undefined) {
g_debug_num[parseInt(h.buddyInfo.id)]=1;
}
if (d.trim(n)) {
var m = {
to: h.buddyInfo.id,
from: h.myInfo.id,
//stype: "msg",
body: (g_debug_num[parseInt(h.buddyInfo.id)]++)+" : "+n,
timestamp: (new Date()).getTime()
};
//g.addHistory(m);
k.val("");
g.trigger("sendMessage", m);
l.preventDefault();
g.sendStatuses("");
k.attr('intervalTime',100);
setTimeout(function(){k.removeAttr('intervalTime')},1000);
return
}
return
}
}
It works fine on Windows but on Linux, it fails to catch the Enter event sometimes. Can someone help?
Updated:
It seems good if I only use English to talk. But I have to use some input method to input Chinese. If it is the problem? (JQuery can not detect Enter if I use Chinese input method? )
To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event. keyCode ?
Using JavaScript In plain JavaScript, you can use the EventTarget. addEventListener() method to listen for keyup event. When it occurs, check the keyCode 's value to see if an Enter key is pressed.
The change event occurs if the value has been changed when the element loses focus. The keypress event occurs every time you press down and release a (non control) key.
Try this
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<div>
<input id="TestTextBox" type="text" />
</div>
</body>
<script type="text/javascript">
$(function()
{
var testTextBox = $('#TestTextBox');
var code =null;
testTextBox.keypress(function(e)
{
code= (e.keyCode ? e.keyCode : e.which);
if (code == 13) alert('Enter key was pressed.');
e.preventDefault();
});
});
</script>
</html>
Use if (l.keyCode == 10 || l.keyCode == 13)
instead of if (l.keyCode == 13)
...
Under Windows, a new line consists of a Carriage Return
(13) followed by a Line Feed
(10).
Under *nix, a new line consists of a Line Feed
(10) only.
Under Mac, a new line consists of a Carriage Return
(13) only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With