Javascript events: how to trigger an event when entering the ALT + NUM character

First, I used the onKeyUp event to detect input changes in the text box. However, when a user enters a French character using ALT + Num, the event does not detect a change until I enter the next character ...

What event should I use to detect special character changes. I tried onChange but it doesn't seem to work.

I tried to use the onkeypress event because I noticed that it fires it after I issued the ALT key, however, although as soon as the ALT is released, onkeypress fires and you see the accented character, but when I use it. value for the input field, it is registered only before and before entering a new character ALT + Num.

for example: I entered vidé, but the search will not dynamically find vidé, but only vid, because é has not yet been stored in this.value until another key event is raised.

So I was wondering if there is a way to simulate / send a keystroke to trigger it.

+4
source share
5 answers

FINALLY FIGURE THIS OUTSIDE !! : D

here is the code, however String, converting the event. which does not work on jsFiddle, although, nevertheless, the code works :)

$('#i').keydown(function() { document.getElementById('j').value = "down"; $('#k').val($('#i').val()) }); $('#i').keyup(function() { document.getElementById('j').value = "up"; $('#k').val($('#i').val()) }); $('#i').keypress(function(event) { $('#k').val(String.fromCharCode(event.keycode)); }); $('#i').change(function() { document.getElementById('j').value = "change"; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="i" type="text" /><br /> <input id="j" type="text" /> <input id="k" type="text" /> 

View on JSFiddle

+1
source

There is a link to http://unixpapa.com/js/key.html , which might seem like a good idea, and in accordance with section 2.1, ALT should generate keydown and keyup in modern browsers.

I would suggest you use jQuery or a similar library to alleviate the problems you encounter in a cross browser. See http://jsfiddle.net/qvDbu/1/ for a proof of concept, which works great.

Keypress does not start only with the modifier-presses, therefore, probably the path to it, it also allows you to pick up the entered character, and not the key pressed. Edit: Seems so that there is no case in Fx that a keystroke is triggered by modifiers, but I cannot reproduce the actual loss of any characters.

0
source

use keyup . A new character will not be added until the user keyup ALT key, at which point the keyup event will occur.

0
source

You need to associate onkeypress , which is not onkeypress , until you have alternative code onkeypress .

 document.getElementById('input').onkeypress = function() { alert('fired'); } 
 <input id='input' type='text' /> 

View on JSFiddle

0
source

A character entered using the ALT + NUM combination will only be added to the value of the input element after keyup event keyup , so you cannot read it in the event handler.

To solve this problem, you can set a timeout in the ALT keyup event before reading the value of the input element:

  $('input').on('keyup', function (e) { var _this = this; var ALT_KEY_CODE = 18; if (e.which == ALT_KEY_CODE) { setTimeout(function () { handleKeyup.call(_this , e); }, 1); } else { handleKeyup.call(_this , e); } }); function handleKeyup (e) { // $(this).val() now holds the ALT+NUM char } 
0
source

All Articles