Triggering an alert with a keypress - javascript

I am writing a Greasemonkey script. I want to trigger a certain code to run when the user presses the "Q" key. I did a little bit of research, and most of the sources I saw suggested using window.onkeypress.
To test this method, I created a userscript set to run when the users presses Q. Here is my code:
window.onkeypress = function(event) {
if (event.keyCode == 81) {
alert("This is a test.")
}
}
However, upon pressing the Q key, nothing happened. I am wondering if anyone knows why this may be and what I can do to correct it.
In addition, if anyone knows of any other methods I can use to achieve the same effect, it would be greatly appreciated.

keypress events don’t receive a keyCode; try handling keydown instead.
window.onkeydown = function(event) {
if (event.keyCode === 81) {
alert("This is a test.");
}
};

Related

Disable Keyboard Shortcuts on Webpage

I am working with some others on a web based javascript game, and there is a small issue where occasionally keyboard shortcuts can get in the way. sometimes for example, one might accidentally press Alt+W and that can be annoying (and sometimes a little scary depending on what your doing).
So i would like to know, is there any way to use javascript to disable shortcuts on a webpage? i have looked around quite a lot and have not found anything about how to do this
you need to use key codes. try with something like this:
window.onload = function() {
window.document.body.onkeydown = function() {
if (event.ctrlKey) {
event.stopPropagation();
event.preventDefault();
try {
event.keyCode = 0;
}
catch (event) {
}
return false;
}
return true;
}
}

JavaScript: implement sequential keyboard hotkey?

For example, I want to implement something like do action when you typed sequentially shift-s then s, in JavaScript. I'm inspired by Autohokey, hotkey configuration software/language for Windows, so I'm looking for a library or way to implement something that in JS.
So far I found a library called hotkeys, but I have no idea to implement a sequential keyboard hotkey and didn't find any issue or question about it also, so here I am. So, how can I implement that in JS using the library or other library? Thanks.
This can be done easy with keydown event listener, no library needed for this
sAgain = false
document.addEventListener('keydown', function(event) {
if (sAgain && event.key.toLowerCase() == 's')
alert("shift-s then s clicked")
else if (event.shiftKey && event.key.toLowerCase() == 's')
sAgain = true
else
sAgain = false //stop listen if other key is clicked after shift-s
});
<h1>Press shift s then s<h1>

How do I handle a keyboard press event in ionic/cordova

I have an ionic application I am developing, and I need a way to handle the return key. When I press the key on the keyboard, the application reloads, like, from the beginning (login). So no matter where I am, and I am using the keyboard, pressing enter has that effect. The problem is irregardless of the platform or device type. How do I best resolve this?
For return key it is same as javascript
if(characterCode == 13)
{
return false; // returning false will prevent the event from bubbling up.
}
else
{
return true;
}
On htl tag of input aff and call this scope function

jQuery keydown not reading tab key the first time

I am making a chrome extension and am trying to use jQuery's keydown function, but it has the following problem:
$(document).keydown(function(event) {
if (event.keyCode == 65) {
//code here
}
});
This code works fine when pressing the a key, while the following function is never executed (even outside the if statement) on the first tab press but is afterwards:
$(document).keydown(function(event) {
if (event.keyCode == 9) {
//code here
}
});
I couldn't find anyone who was having this problem, so I thought I'd post it.

How to register document.onkeypress event

I want to register keypress events for a document using javascript.
I have used:
document.attachEvent("onkeydown", my_onkeydown_handler);
It works fine with IE,
but not with Firefox and Chrome.
I also tried:
document.addEventListener("onkeydown", my_onkeydown_handler, true);
// (with false value also)
But it still doesn't work with Firefox and Chrome.
Is there a solution, am I missing something?
You are looking for:
EDIT:
Javascript:
document.addEventListener("keydown", keyDownTextField, false);
function keyDownTextField(e) {
var keyCode = e.keyCode;
if(keyCode==13) {
alert("You hit the enter key.");
} else {
alert("Oh no you didn't.");
}
}
DEMO: JSFIDDLE
You are probably looking for:
document.body.addEventListener('keydown', function (e) {
alert('hello world');
});​​​​​​​
But it is almost certainly going to be worth your time to use an existing library to abstract over the problems of the many browsers out there.
Please go through following links for detailed description.
https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener?redirectlocale=en-US&redirectslug=DOM%3Aelement.addEventListener
http://www.reloco.com.ar/mozilla/compat.html
In short, write handler as
function myFunction(e)
{
///For IE
if(!e)
e=window.event;
// use e as event in rest of code.
}

Categories

Resources