Get Character Value from Keycode in JavaScript... Then Trim

Get Character value from KeyCode in JavaScript... then trim

Maybe I didn't understand the question correctly, but can you not use keyup if you want to capture both inputs?

$("input").bind("keyup",function(e){
var value = this.value + String.fromCharCode(e.keyCode);
});

How to convert keycode to character using javascript

String.fromCharCode() is what you want:

The fromCharCode() method converts Unicode values to characters.

Syntax

String.fromCharCode(n1, n2, ..., nX)

can i get the keycode value?

Get the key property instead of the keyCode property from the event:

(function() {
let this_, keycode_;

$('textarea')
.on('keydown', function(e) {
this_ = e.target;
keycode_ = e.keyCode == 9 ? 'Tab' : (e.shiftKey ? 'Shift' : (e.key || e.which));
$(this).trigger('input');
})
.on('input', function() {
$('#log').text(keycode_);
})
.on('keyup', function() {

});

})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--
i want to get keycode_ value, not the code.
i mean when you push "D" key on keyboard it will return 68, can it return "D" ? and i got nothing for shift key or tab.
can you solve this?
-->
<textarea></textarea>

<div id="log"></div>

Trim and return specific character set removing rest using javascript

I'd suggest using String's match() to parse the input string.
Then you may use parseFloat() to make it a number.

let input = 'Bid</span></td><td class="Ta(end) Fw(600) Lh(14px)" data-test="BID-value" data-reactid="51"><span class="Trsdu(0.3s) " data-reactid="52">0.3000</span>'
let match = input.match(/>([\d.]+)</)
let output = match ? match[1] : undefined

console.log(output, 'as string')
console.log(parseFloat(output), 'as number')

How to find out what character key is pressed?

"Clear" JavaScript:

function myKeyPress(e){
var keynum;

if(window.event) { // IE
keynum = e.keyCode;
} else if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}

alert(String.fromCharCode(keynum));
}
<input type="text" onkeypress="return myKeyPress(event)" />

How to know if a key is pressed having no charcode value

Vanilla JavaScript:

For other constants, see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

window.onkeydown = function (e) {
switch (e.keyCode) {
case KeyboardEvent.DOM_VK_SPACE:
alert('space bar!');
break;
case KeyboardEvent.DOM_VK_CAPS_LOCK:
alert('CAPS LOCK!');
break;
case KeyboardEvent.DOM_VK_CONTROL:
alert('control!');
break;
case KeyboardEvent.DOM_VK_SHIFT:
alert('shift!');
break;
case KeyboardEvent.DOM_VK_ALT:
alert('alt!');
break;
}
};

UPDATED FOR REQUIREMENT TO AVOID CASES:

Per the following test, the only numeric values that will, after trimming (and not including numbers not corresponding to the average keyboard), be reduced to an empty string are 9,10,11,12,13,32. Looking at https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Virtual_key_codes , the only ones that correspond are tab, clear, return, and space .

// Run in Firefox where trim() is supported (without need for jQuery):
var arr = [];
for (var i=0; i < 0xFFFF; i++) {
if (String.fromCharCode(i).trim() == '') {
arr.push(i);
}
}

In other words, your own test is not going to catch all cases.

So you have to use numeric comparisons based on the info at https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Virtual_key_codes and BASED ON EXACTLY WHAT CHARACTERS YOU NEED TO INCLUDE (or exclude).

For example, if you consider the cancel key, help key, back space, tab, etc. all to be of the type you mentioned, you can do:

window.onkeydown = function (e) {
if (e.keyCode < 0x30) {
alert('Special key pressed!');
}
};

As you can see, this allows for us to find a whole group of characters within a short amount of code (e.g., without using case). (But if we don't know exactly which characters you want to include or exclude, we can't give you a more precise answer.)

Print the name of a Key when it is Pressed?

You can get the keycode, but not the name, because names vary across cultures, languages, etc., and JavaScript doesn't provide any means of getting the culture- and language-specific name appropriate to the current culture and language.

So the answer to "Is it possible to get the name of a key when it is pressed?" is: Not from anything built into JavaScript or the browser. You'll need lookup tables and/or a library. Beware that different keyboard layouts may give different mappings.

The keypress event gives you a character code for the matching character if there is one (on event.charCode, which you can use like String.fromCharCode(event.charCode)), but not keydown since not all keydowns generate characters (as distinct from other keypresses), and in fact sometimes it takes more than one keydown to create a character.


To get the keycode, just use:

var keyCode = event.which || event.keyCode || 0;

That will use event.which if it's present and truthy, or event.keyCode if it's present and truthy, or 0 if neither is present and truthy. Most browsers use which; many versions of IE use keyCode.

Since a key "name" would probably include modifiers, I'll mention that there are modifier flags available on the event as well: event.shiftKey, event.ctrlKey, event.altKey, and event.metaKey.

You'll see people telling you can use String.fromCharCode(keyCode) after doing the above to get a character for the key code. That's only true for a very small set of key codes, because key codes are not character codes. There is a small amount of overlap between the two, which is why people think you can do that. For instance, if you press a on your keyboard, on any keyboard I've ever heard of String.fromCharCode(keyCode) will give you "A" (and then you can use event.shiftKey to decide whether to make it lower case). But it falls down quickly when you get into anything beyond the 26 letters of the English alphabet (and probably the standard ten digits).



Related Topics



Leave a reply



Submit