Convert a String to KeyCode (with Special Characters) - javascript

Couldn't find any post with the right solution,
Basically what i'm trying to get, is to convert a string with special characters to KeyCode Array,
for Example: Convert "Hello#Today" to:
[104, 101, 108, 108, 111, 16, 50, 116, 111, 100, 97, 121]
Notice the "16, 50" which is combination of Shift(16) + 2(50),
In my current code i get (64) for # which is wrong:
[104, 101, 108, 108, 111, 64, 116, 111, 100, 97, 121]
my current function:
function convertToKeyCode(text) {
var results = text.split('')
.map(function (char) {
return char.charCodeAt(0);
});
return results;
}
Thanks

There is not a "ready-to-use" solution to convert a character to a key-code, at least in JavaScript.
Furthermore even if you implement your own conversion/mapping algorithm consider that different keyboard layouts generate characters with different key combinations.
Finally not every character code you may get with charCodeAt may have a corresponding key combination.

Related

I need to find the second largest and smallest number in array in javascript [duplicate]

This question already has answers here:
How do I get the second largest element from an array in javascript
(8 answers)
Obtain smallest value from array in Javascript?
(18 answers)
Closed 2 years ago.
below is my array
<pre>
let input = [29, 75, 115, 12, 89, 29, 103, 65, 100, 78, 115, 102, 55, 214];
</pre>
now i need the second largest from it.

Regex disabling escape key

I am using the following code to try and only allow letters, numbers, backspace, space, dashes, and ampersands. I also want to "disabled" all other keys including the function keys (F1, F2, etc). The problem I am running in to is with the regex also disabling the escape key.
Can the same thing be achieved without using a regular expression?
This function is part of a live search feature, and I don't want the ajax request to be sent if on of the "prohibited" keys are pressed.
searchBox.keyup(function (e) { // LIVE SEARCH FUNCTION
var functionKeysPressed = [112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123].indexOf(e.which) > -1;
if (String.fromCharCode(e.which).match(/^[\w\x08]$/) && !functionKeysPressed) {
// code to execute if key is allowed
}
});
You can do this without a regex. Simple refer to e.keyCode instead of e.which:
if (e.keyCode == 27){
//Esc Key pressed
}
for onKeyUp
keycode returns the Unicode keycode of the key that triggered the event.
You will find the keycodes here:
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
to exclude multiple keys, you could simply create an array and check if the keycode is blacklisted:
var blacklisted = [1,2,3,4,5];
if (blacklisted.indexOf(e.keyCode) == -1){
//do something
}

JavaScript ASCII to text

I was wondering if it's possible to change the "Batman" text in the JavaScript Batman Joke to ASCII, then convert it and have it appear in the browser
The original joke is
javascript:Array(16).join("lol" - 1) + "Batman"
can this be changed to something like
javascript:Array(16).join("lol" - 1) + [66, 97, 116, 109, 097, 110].forEach(function(e) { document.write(String.fromCharCode(e)); } );
where [66, 97, etc] are the ASCII codes for Batman. This fails because the document object does not exist in an empty browser window.
(PS Feel free to delete if this isn't appropriate, it was only for fun)
You don't actually need to use any sort of loop or iterator for your array, String.fromCharCode will actually accept as many characters as you want to give it. So this will work just fine:
javascript:Array(16).join(-'.') + String.fromCharCode(66, 97, 116, 109, 097, 110);
However, if you wanted to call String.fromCharCode for every element of the array, you can. Every JavaScript function can be called using Function.prototype.apply which will run the function for each array element:
javascript:Array(16).join("lol" - 1) + String.fromCharCode.apply(this,[66, 97, 116, 109, 097, 110]);
Also, the purpose of "lol" - 1 is to try to create a bad number (a NaN or Not a Number). If you wanted to obfuscate things a bit further, you could use any math operation on a nonnumerical string. For example:
javascript:Array(16).join(-'.') + String.fromCharCode.apply(this,[66, 97, 116, 109, 097, 110])
because -'.' is interpenetrated as 0 - '.' which doesn't make any sense so it its NaN.
You can just do:
javascript:var str='';[66, 97, 116, 109, 097, 110].forEach(function(e) { str+= (String.fromCharCode(e)); } ); Array(16).join("lol" - 1) + str;
Or:
javascript:Array(16).join("lol" - 1) + [66, 97, 116, 109, 097, 110].map(function(e) { return (String.fromCharCode(e)); }).join('');

Very weird issue with characters and undefined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
OK, check this one :
Visit this page here: http://efluo.net/
Open the JavaScript console
TEST 1:
Copy-paste this (and press enter) : window.DB["ar,i>í"]
It will show something like this :
Object {rootChange: "í", subjuntivo: Array[4], rootFind: "i", gerundio: "$ando", indicativo: Array[5]…}
TEST 2:
Now, copy-paste this one (It looks the exact same thing, well wait!) : window.DB["ar,i>í"]
And the response is :
undefined
OK, so could you please explain to me what's going on and how it is to be resolved (convert the second one, to the first one)?
Nothing weird about it, they're different characters that look the same. If you convert them to hex:
í = 69 cc 81
í = c3 ad
So, just replace one of them with the other.
Why would you be using characters like that in the first place?
That's because the length of the first string is 7, not 6. That last "character" of the first string is actually two characters that look like one. Try this:
var a = "ar,i>í", b = "ar,i>í";
for (var i = 0; i < a.length; ++i) {
console.log(a[i]);
}
for (var i = 0; i < b.length; ++i) {
console.log(b[i]);
}
If you look at the ASCII character encoding for both:
The first is:
[119, 105, 110, 100, 111, 119, 46, 68, 66, 91, 34, 97, 114, 44, 105, 62, 105, 769, 34, 93]
The second is:
[119, 105, 110, 100, 111, 119, 46, 68, 66, 91, 34, 97, 114, 44, 105, 62, 237, 34, 93]
So the second example is missing the part of the unicode character that corresponds to 769.
Check yourself using the code I used:
var arr = [];
for (var i = 0, l = s.length; i < l; i++) {
arr.push(s.charCodeAt(i));
}
console.log(arr);

Javascript to match a specific number using regular expressions

I was using javascript to detect for specific key strokes and while writing the method I thought I'd try regular expressions and the test() method and came up with:
if (/8|9|37|38|46|47|48|49|50|51|52|53|54|55|56|57|96|97|98|99|100|101|102|103|104|105|110/.test(num)) {
// do something if there's a match
}
This doesn't seem to work 100% as some values seem to make it past the regex test, such as 83. I've since moved on, but I'm still curious as to why this didn't work.
This is the completely wrong way to do it.
To answer the question, the regex is matching part of your string. The string 83 passes by matching the 8.
You need to anchor your regex by putting ^( at the beginning and )$ at the end.
The correct way to do this is to make an array of valid numbers, and compare using parseInt.
For example:
var validNumbers = [ 8, 9, 37, 38, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110 ];
if (validNumbers.indexOf(parseInt(num, 10)) >=0 ) {
//Match
}
You'll need an indexOf function for IE:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(needle) {
for(var i = 0; i < this.length; i++) {
if(this[i] === needle) {
return i;
}
}
return -1;
};
}
You need to specify the start and end of the string. Otherwise 8 in 8|… will match the 8 in 83:
/^(8|9|37|38|46|47|48|49|50|51|52|53|54|55|56|57|96|97|98|99|100|101|102|103|104|105|110)$/.test(num)
But you should rather use numeric comparison. If you don’t like to list every number, you can use ranges like this:
function foo(number, numbers) {
for (var i=0; i<numbers.length; ++i) {
if (numbers[i] === number) {
return true;
} else if (numbers[i].constructor === Array) {
if (numbers[i][0] <= number && number <= numbers[i][1]) {
return true;
}
}
}
return false;
}
var numbers = [8, 9, 37, 38, [46, 57], [96, 105], 110];
if (foo(num, numbers)) {
// …
}
If you make a regular expresion like /\b(100|101)/g it will match only 100 and 101 and not 5100, 5101 or ...101...;
The only problem with this is if your are using negative numbers, e.g in case of 101 and -101 both match with the regexp.
I know this because is what I'm facing and want to avoid.
I can share with you an example:
let array = [89, 7, 92, 78, 899, 65, 56, 92, 922, 292, 289, 389, 2879, 2932, 8999];
I want to find how many instances of the number 92 exist in that array. Therefore I am searching for the precise number of 92. And I want to use Regular Expressions in Javascript.
First part comes with the transformation of the array into a string:
let strCheck = array.toString(); // "89,7,92,78,899,65,56,92,922,292,289,389,2879,2932,8999"
let regex = /\b92\b/g;
I used the flag g for global so that it finds all the matches and the \b word boundary as described in MDN.
let arrResult = strCheck.match(regex); // ["92", "92"]
That's it. The tricky part was first to acknowledge that Regular Expressions work with strings, the second was that once I got the string I had to think about getting the number I wanted not as a number but as a string which was going to be surrounded by other characters and reaching out to those other characters, helped to find the solution.

Categories