Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect key event (Enter) with JQuery in Javascript (on Linux platform)

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? )

like image 847
Mickey Shine Avatar asked Aug 08 '09 06:08

Mickey Shine


People also ask

How do you check if Enter key is pressed in jQuery?

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 ?

How do you check if the Enter key is pressed in JavaScript?

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.

What is the difference between jQuery's change () and keypress () events?

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.


2 Answers

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>
like image 162
Raghav Avatar answered Oct 22 '22 09:10

Raghav


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.

like image 25
Andrew Moore Avatar answered Oct 22 '22 10:10

Andrew Moore