ANILBABU
ANILBABU

Reputation: 387

events recognizing special characters in IE10 vs IE8

The answer already available on Stack Overflow is not a solution to my question..

I can check special characters by keycode but both dot and delete are giving same key code 46...

What to do in such situations to identify a special character ?

function keypress(ev) 
    {
        if(ev.which == 0)
        {
            alert('pressed character is special handling character like del,Home,Navigation buttons, etc');
        } else {
            alert('pressed character is normal character');
        }
        if(ev.keycode){
            var k = ev.keycode;
            alert('ascii value of pressed character is ....' + k);
        }
    }

If I am using IE8... ev.which is becoming zero and entering 'if' block and printing alert if the pressed character is special character... otherwise it goes to 'else' block...

But in IE10 , It is going to 'else' block always for both special and normal characters...

Surprisingly, dot and delete are having same ascii value 46 in IE10.. printing the same in alert of the keycode 'if' block....

How can I distinguish normal and special characters pressed in IE10.....

Upvotes: 0

Views: 355

Answers (1)

Rahul Desai
Rahul Desai

Reputation: 15501

I would suggest using onkeydown event. The event.which is recognized as expected if we use that.

Working Demo of distinction of . and Delete buttons.

HTML:

<input type="text" onkeydown="keypress(event)" />

JS:

function keypress(event) 
{
    alert("something pressed..");

    alert("event.which = " + event.which);

    if(event.which === 46)
    {
        alert('Delete button pressed');
    } else  if(event.which === 190) {
        alert('. button pressed');
    }
}

EDIT: It seems . and > have same key code 190 in keydown event. To identify these two elements which are on the same key, we need to track if the Shift key is pressed.

Here is one way to do it:

var prevKey = "";

function keypress(event) 
{  
    if(event.which === 16)    // only Shift key
        prevKey = "Shift";

    if(prevKey === "Shift" && event.which === 190){    // Shift + . key
        alert("> key pressed");
        prevKey = "Something else";
    }       

}

Working Demo

Upvotes: 1

Related Questions