Jump to content

How to use onkeydown (client side) to replace one character with another


irigsoft

Recommended Posts

Hi.

I try to replace some characters in one TuniEdit on clientside.

That code i use, but not work properly (differer) on Desktop and on mobile Chrome:

function keydown(sender, e, eOpts)
{
      var val = sender.getValue(); 
      var allowed = '!@#$%^&*_=.,/-[]{}|№"'
      + '0123456789'
      ; 
      var c = event.key.charCodeAt(0);//event.code;//event.code;//e.keyCode || e.which;//e.getCharCode();//e.keyCode;
      var k = event.key; 

      //if is used in mobile chrome - event.key is unidentified - https://stackoverflow.com/questions/17139039/keycode-is-always-zero-in-chrome-for-android

       if (k == "Unidentified" || c == 85 || c == 0) //for android chrome keycode fix

      { 
        var val = sender.getValue();       
        c = val.charCodeAt(val.length - 1);
        k = val.charAt(val.length - 1);
      };
      
      if (allowed.indexOf(k) > -1)// || val.length > 0)       
            {
               c = String.fromCodePoint (c + 5);
               //change last char - not work correct
               val = val.substr(0, val.length-1);
               sender.setValue(val + c);
               //e.stopEvent() ;         
      };
      e.preventDefault();
     
}

 

UniEdit - Triggers (ButtonEdit).zip

Link to comment
Share on other sites

37 minutes ago, Sherzod said:

Hello @irigsoft

Can you check this post?

 

thanks but this is different case.

I try to explain my purpose:

1. must work on desktop and on mobile browsers (chrome and others)

2. code from above work proper on desktop browser because event.key return value and I can check point 3

3. event.key must be from array string that I insert on allowed 

4. event.keyCode not return correct keycode when use allowed .fromCodePoint (event.keyCode), like example when I press * (Star), I get keyCode = 106 = m, but * (star) = 42. By this reason I can use event.keyCode !

5. on mobile chrome I can't get event.key, for that reason i use :

       if (k == "Unidentified" || c == 85 || c == 0) //for android chrome keycode fix
       {
        var val = sender.getValue();       
        c = val.charCodeAt(val.length - 1);
        k = val.charAt(val.length - 1);
      };

and try to get last char on text field, but if is only one char, i get k=Unidentified and c=85

6. I don't use jQuery !

7. I can use onkeyup because there is some delay and on fast char entering different replacement is executed !

 

On your examples all is based on event.key , like i explain event.key is not work on mobile chrome)

Problem: how can I find out (on mobile chrome) which key was pressed when it is the first character in the text field on onkeydown event on mobile chrome

Link to comment
Share on other sites

1 hour ago, Sherzod said:

Once again, please clarify the statement of the problem, what should be done.

1. try chrome mobile browser (i use unigui - professional) with this code:

function keydown(sender, e, eOpts)
{
      var val = sender.getValue(); 
      var allowed = '!@#$%^&*_=.,/-[]{}|№"'
      + 'zxcvbnmasdfghjklqwertyuiop'
      + 'ZXCVBNMASDFGHJKLQWERTYUIOP'
      + '0123456789'

      ; 

      //Cancel the event
      //event.stopEvent() ; 
      event.preventDefault();
      event.stopPropagation();

      var c = event.key.charCodeAt(0);
      var k = event.key; 

     

       if (k == "Unidentified" || c == 85 || c == 0) //for android chrome keycode fix

      { 
        var val = sender.getValue();       
        c = val.charCodeAt(val.length - 1);
        k = val.charAt(val.length - 1);
      };
      
      if (allowed.indexOf(k) > -1)
            {
               c = String.fromCodePoint (c + 5);
               //change last char - not work correct
               //val = val.substr(0, val.length-1);
               sender.setValue(val + c);
               //e.stopEvent() ;         
      };
      e.preventDefault();
     
}

 

2. Try this for test:

2.1 use this code on desktop browser and write by hand asdfgh, that must give you image.png.31c246f566ee73ff7b5624dd2056d04e.png as result

2.2 try to write same text but on mobile chrome browser, that will give you different result string. image.png.7ac34fc99fec4acc000851d3a92d6dce.png

Last character is not converted !

 asdfgh -> fxiklm is correct result string

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...