views:

4198

answers:

3

Hi there,

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

+3  A: 

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.

Andrew Moore
I added "l.keyCode=10" but I still encountered the problem. And I use "console.log(l.keyCode)" to see what keyCode are, it seems there is only "13" when I press Enter (On linux)
Mickey Shine
A: 

This could be a browser specific issue. Different browsers handle the keycode differently. It's good to normalize this prior to inspection:

var code = ev.keyCode || ev.charCode || 0;

It's important to note that webkit browsers have a slightly different key mapping than the rest:

var webkitKeymap = {
            63232: 38, // up
            63233: 40, // down
            63234: 37, // left
            63235: 39, // right
            63276: 33, // page up
            63277: 34, // page down
            25: 9      // SHIFT-TAB (Safari provides a different key code in
                       // this case, even though the shiftKey modifier is set)
        };

This code can be found in YUI's event utility.

steve_c
+3  A: 

Try this

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" >
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
</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>
Raghav Khunger