1

So I was trying replace the key press "K" with "Z" in an input field. I was successfully able to do it. But there is a slight delay which makes the user see that the "K" being changed to "Z".

This is my code:

        function prinner (event)
        {
          document.getElementById("txx").innerHTML= event.key; //Displays key pressed on screen by changing text element.
           if(event.keyCode == 32){
               // User has pressed space
               document.getElementById("txx").innerHTML= "Space";
           }
           if (event.key=="k") // Trying to replace this with z.
           {
            var curval = $("#namaye").val(); //namaye is the ID of the input field.
            var nval = curval.slice(0,(curval.length-1))+"z";
            $("#namaye").val(nval);

           }
        }

       $("#namaye").keyup(prinner);

Does anyone know a better way to achieve this without the delay?

4 Answers 4

3

Use keydown instead of keyup and cancel the event so the key stroke doesn't actually get printed:

function prinner (event) {

  // Displays key pressed on screen by changing text element.
  document.getElementById("txx").innerHTML= event.key; 
  
  if(event.keyCode == 32){
    // User has pressed space
    document.getElementById("txx").innerHTML= "Space";
  }
  
  // Trying to replace this with z.
  if (event.key=="k") {
  
    var curval = $("#namaye").val(); //namaye is the ID of the input field.
    var nval = curval +"z";
    $("#namaye").val(nval);
    
    // Cancel the event
    event.preventDefault();
    event.stopPropagation();
  }
}

$("#namaye").keydown(prinner);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="namaye">
<p id="txx"></p>

Sign up to request clarification or add additional context in comments.

10 Comments

You are always preventing default which means that no characters are being sent out to the input. Given my understanding of the OP, you only want to prevent default on k. Then, because the 'k' never ends up in the input, you don't have to strip it out with the slice function. You just have to append z.
@ConorMancone Did you run my code snippet? Did you type a k in the input and see what happens?
Yes, it turns into a 'z', but no other letters work. Because you always prevent default you can't actually type in the input box. Only the letter z will actually appear: I don't believe that is what the op had in mind.
One more issue: you are still stripping the last character off with your slice function. Because keydown happens before the input actually changes, you aren't stripping off a 'k' key: you are just stripping off the last key typed. so if you try to type 'ak' you won't end up with 'az' (as desired), you will just get 'z'. The OP had the slice because he had to remove the k. The keydown stops that from happening in the first place, so you just have to append a 'z'
@ConorMancone Thanks. I was assuming OPs slice code was fine. Updated again.
|
3

Use keydown event, and cancel the default behaviour when k is pressed. Also, use selectionStart and selectionEnd properties to replace the characters that were selected at the moment the key was pressed, and to put the cursor at the right position, just after the inserted z:

function prinner (event) {
    $("#txx").text(event.keyCode == 32 ? "Space" : event.key); 
    if (event.key=="k") {
        var s = $(this).val();
        var i = this.selectionStart;
        s = s.substr(0, i) + "z" + s.substr(this.selectionEnd);
        $(this).val(s);
        this.selectionStart = this.selectionEnd = i + 1;
        return false;
    }
}

$("#namaye").keydown(prinner);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="namaye">
<div id="txx"></div>

Since you use jQuery, use $("#....") instead of the more verbose document.getElementById("...."). Also, in the event handler, this will be input element, so use that reference.

Comments

0

Try using keydown? It happens before the input is actually modified: in fact, if you return false (or e.preventDefault()) inside a keydown listener, it will actually cancel the keystroke, which I think is what you want. Then you manually add your new key. Something like (untested and skipping some details for clarity):

    function prinner (event)
    {
       if (event.key=="k")
       {
           event.preventDefault() // makes sure the 'k' key never goes to the input
           $("#namaye").val( $("#namaye").val() + 'z' );
       }
    }

   $("#namaye").keyup(prinner);

Comments

0

You have to add an event.preventDefault() inside your if clause to stop the event propagation and then you can insert your "z" key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.