I want to detect when the user pressed a key that's not the "enter" button.
For example: if the user pressed the "shift" key it will do something.
I have this code but it's not working:
$(document).keypress(function(e) {
var keycode = (e.keyCode ? e.keyCode : e.which);
if (keycode!= '13) {
//do somthing
}
});
KeyboardEvent.keyCode and KeyboardEvent.which are both deprecated (confusing, I know). Use KeyboardEvent.code like so:
$(document).keypress(function(e){
if(e.code !== "Enter"){
// Your code here
}
});
More information at MDN
Try
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
document.onkeydown = function(e) {
if (!e.shiftKey) {
alert("You have entered a key that was not shift");
}
};
});
</script>
</head>
<body>
Please try to enter a key.
</body>
</html>
Related
I'm writing a simple web site and would like to run some simple javascript when the user presses a button on their keyboard. Something as simple as running alert("Hello!"); or showing a modal box when the user presses enter would do.
After searching for a good while, I've only been able to find solutions that work if the page contains an input text field. Is it possible without it?
Yes, it is possible to do so, and here's how you'd do it:
document.body.addEventListener("keydown", function(event) {
if (event.keyCode == 13) {
alert("Hello! You pressed the enter key!");
}
});
Yes, you can actually use eventListener for this. For example, check out this sample page.
<html>
<body>
<script>
document.addEventListener("keypress", function(event) {
if (event.keyCode == 13) {
alert('You just hit enter.');
}else if(event.keyCode ==65){
alert('You just press A.');
}else if(event.keyCode==97){
alert('You just hit a.');
}else{
alert('You press something other than A, a and ENTER key');
}
})
</script>
</body>
</html>
In order to get the keycode for the various keypress you can use this:
<html>
<body>
<p>Press a key on the keyboard in the input field to get the KeyCode for that key.</p>
<input type="text" size="40" onkeypress="getKeyCode(event)">
<p id="keyCode"></p>
<script>
function getKeyCode(event) {
var x = event.which || event.keyCode;
document.getElementById("keyCode").innerHTML = "The keyCode is: " + x;
}
</script>
</body>
</html>
Hello I have this link for example
<a href"http://google.es">Link</a>
Is there any way using Javascript or similar that when I press for example the number 5 in the keyboard It´s the same as if I press the link with the mouse?
Thanks
you can use jQuery
$( document ).keypress(function( event ) {
event.charCode == 53 ? window.open("google.es","_self") : console.log('another key');
});
You can bind keypress event and sniff out the event.which and when the appropriate key is pressed you can programmatically click the a or link element.
Check this CodePen for the example.
Note: This example is using jQuery plugin.
Try this. Using jQuery.
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function checkKey(e) {
var code;
if (e.keyCode) code = e.keyCode;
if (code == 53) { // replace the 53 with the keycode of your choice.
$('#myLink').trigger("click");
}
}
</script>
</head>
<body onkeydown="checkKey(event);">
<button id="myLink" onclick="window.location.href= 'https://google.com'">Link</button>
</body>
</html>
Keycode reference: http://www.foreui.com/articles/Key_Code_Table.htm
you can use the key press event in jquery
$(function() {
$(document).keypress(function(e) {
if (e.which === 53){
location.href = "http://google.es";
}
});
});
this is for the key 5 if you want to change key give asci value for that key
I am trying to detect enter key press inside tinmyce editor and it's working fine for all the keys but its not working for enter key.
setup : function (instance) {
instance.on("keypress", function(e) {
var Keycode = (e.keyCode ? e.keyCode : e.which);
alert(Keycode);
});
}
In above code its alerting all the keys except ENTER. i don't know whats the issue there.
keypress wasn't working for me , but keydown worked for me.
setup : function (instance) {
instance.on("keydown", function(e) {
var Keycode = (e.keyCode ? e.keyCode : e.which);
alert(Keycode);
});
}
I think this will work, i've tested it and it works in this fiddle with jquery 2.24
$(document).keypress(function(e) {
if (e.which == 13) {
console.log('You pressed enter!');
} else {
console.log('You pressed ' + e.which);
}
});
body {
height: 500px;
width:500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
</body>
EDIT: Sorry i only noticed after that this was also tagged as tinymce
I think you can do this by slightly adjusting your function
//tinyMCE.init({
setup : function(instance) {
instance.onKeyDown.add(function(instance, event) {
if (event.keyCode == 13) { //enter
event.preventDefault();
event.stopPropagation();
//do stuff here
//alert("Enter!");
}
else{ alert (event.keyCode);}
});
}
//});
EDIT 2:
In the current tiny mce docs, there is an example of a function [in setup].
maybe try
tinymce.init({
selector: 'textarea', // change this value according to your HTML
setup: function(instance) {
instance.on('keypress', function(e) {
if (event.keyCode == 13) { //enter
event.preventDefault();
event.stopPropagation();
//do stuff here
//alert("Enter!");
}
else{ alert (event.keyCode);}
});
}
});
note the tinymce is now all lowercase..
Try this :
$(function () {
$("#MYTEXTBOX").keyup(function (e) {
alert(e.keyCode);
});
});
onkeypress does not detect some special keys like ctrl, shift arrow keys etc. Use onkeypress or onekyup instead.
I want to know when user press W with shift. I am not getting any logic for this. Kindly suggest.
<head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function() {
$('body').keydown(function(e) {
var key= e.keyCode;
});
});
</script>
</head>
<body>
<div class="test">test</div>
</body>
W has key code 87, while jQuery event object has .shiftKey flag:
if (e.which === 87 && e.shiftKey) { ... }
DEMO: http://jsfiddle.net/etgAB/
You should have true in the event object at e.shiftKey, besides of the keycode of W (87)
Learn more about the jQuery-normalized event object at the official docs page
How to make the event when the Esc key is pressed; please give me an example of an alert ();.
document.body.onkeyup = function(e){
if(e.keyCode == 27){
alert('Yay');
}
}
if your interested in using jquery,
check this code
<script type="text/javascript">
$("CssSelector").on("keyup", HandleKeyEvent(e));
function HandleKeyEvent(e)
{
if ( e.keyCode == 27 && e.key == "Escape" )
{
//--> Do Something
alert("It Works");
}
} );
</script>
Sorry I have just realized that this is an old question from 2011.