You can pass event to the function from which you can access the property keyCode.
But I would recommend you to see the documentation before using KeyboardEvent.keyCode
You should avoid using this if possible; it's been deprecated for some time. Instead, you should use
KeyboardEvent.code, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers. Google Chrome and Safari have implementedKeyboardEvent.keyIdentifier, which was defined in a draft specification but not the final spec.
function callMyFunction (e, importantothervalue) {
console.log(e.code);
console.log(importantothervalue)
}
<textarea onkeyup="callMyFunction(event, 1);"></textarea>
<textarea onkeyup="callMyFunction(event, 2);"></textarea>
<textarea onkeyup="callMyFunction(event, 3);"></textarea>
Answer from Mamun on Stack Overflowjavascript - How to find out the key pressed on keyup or keypress event when dollar key is pressed - Stack Overflow
javascript - keypress and keyup - why is the keyCode different? - Stack Overflow
jquery - JavaScript .on("keyup") - Stack Overflow
Javascript onKeyUp - Stack Overflow
Videos
i'd go with something like this:
$(function () {
$(document).on('keyup keydown keypress', function (event) {
if (event.keyCode == 52 && event.shiftKey) {
alert('foo');
}
});
});
jsfiddle
Key codes relate only to keys. $ is a character, achieved through two keys, Shift and 4. There is no key code explicitly for $ when using onkeydown
Edit: It was pointed out by Edward that onkeypress uses key combinations, and does have keycode's for combinations. Learn something new every day :)
Here's some code, edited from the onkeydown example provided by the MDN, to detect keypress keycodes.
Here's the fiddle updated to be Firefox-friendly, using help from this S.O. post. The JQuery solution works too, if you swing that way.
$ is equal to 36.
JavaScript Event KeyCode Test Page
Here is same S.O thread
The events are for completely different purposes. Use keyup and keydown for identifying physical keys and keypress for identifying typed characters. The two are fundamentally different tasks with different events; don't try to mix the two. In particular, keyCode on keypress events is usually redundant and shouldn't be used (except in older IE, but see the linked document below for more on that); for printable keypresses it's usually the same as which and charCode, although there is some variation between browsers.
Jan Wolter's article on key events, already linked to in another answer, is the definitive word on this subject for me and has tables describing what each of the different properties returns for each type of key event and each browser.
There is a good article on quirksmode.org answering exactly that question. You might also want to look at Unixpapa's results.
Your code works for me (see below). Maybe check where you are binding your keyup event. It should be bound once when the document loads before the page shows. If you bind it multiple times (i.e. if the code that contains your keyup function runs more than once) you will run into problems.
$("#arama").on("keyup", function(event) {
var i = event.keyCode;
if ((i >= 48 && i <= 57) || (i >= 96 && i <= 105)) {
$("#arama").off("keyup");
console.log("Number pressed. Stopping...");
} else {
console.log("Non-number pressed.");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="arama" />
No need to unbind the event. Try this
$("#arama").on("keyup",function(event) {
console.log("asd");
});
You need to check which key is being pressed ... you are running your function each time ANY key is being pressed right now. Inside your searchCensor() function you will want to have a check like this: if (event.keyCode == 13) {
Here is your logic:
If the search field has the value "censorship", go straight to this page
Else, if the key hit was [Enter], search on google the value of the search field
Else, in any other case, search on google the value of the search field
Now, say, you're hitting "e" in your search field. What happens?
First check: it's not "censorship", skip
Second check: the hit key is not [Enter], skip
Third check: Alright, none of the checks before passed, I'm doing this.
So you end up searching on Google for the letter "e".
Do you see how this is wrong?
That said, take off the else and you'll be fine.