Setting pc delete button with javascript or jquery - javascript

I want to catch the event with javascript or jquery when user presses to the delete button in keyboard. Is this possible?

I'm not sure why you're being told that this is impossible?
The delete key is 46, so this should work:
$('html').keyup(function(e){
if(e.keyCode == 46){
//delete key has been pressed.
}
})
Also note: You should be using either the keyup or keydown event rather than keypress. keypress is intended for real character. 'keyup'/'keydown' is handled at a lower level so it will capture all non-printing keys like DEL and ENTER.
Source is here: Capturing "Delete" Keypress with jQuery

$('html').keyup(function(e){
if(e.keyCode == 46)alert('Delete Key Pressed')
})
from here

Related

Javascript onKeyDown function

I have written a simple javascript code and added event listener for onKeyDown/onKeyUp . It works just fine and detects the Control key as required. However the Control key shows pressed even when I am using OS shortcuts.
To be specific I am on Ubuntu and use Ctrl+Alt+ arrow keys to switch workspaces. So when I switch the workspace and come back to Chrome, it shows Control key pressed already.
I dont mind it but even when the shortcut keys are released, onkeyup method is not called and hence the status shows Ctrl key pressed.
How do I avoid this?
Below is my code:
function keypressedfn(e){
if(e.which == CTRL_KEY){
console.log("isCtrl pressed");
isCtrl=true;
if(isWheel){
e.preventDefault();
e.stopPropagation();
}
}
};
function keyreleasedfn(e){
if(e.which == CTRL_KEY){
console.log("isCtrl released");
isCtrl=false;
isWheel=false;
}
};
$(document).ready(function(){
$(document).keydown(keypressedfn);
$(document).keyup(keyreleasedfn);
});
if you're using Ctrl to change workspaces, then your window w/ js running is losing focus before the keyup event.
you could bind an event to the window object that discards keydown whenever the window loses focus:
window.onblur = function(){console.log("blur");};
without seeing more of your code i can't say much more than that
It is not standard practice to detect the KeyDown event of Control key (or Alt, or Shift, for that matter).
What you should do is find if your desired key is pressed. For example, if you want to save something when Ctrl+S is pressed, look for S key.
var keyCode = (e.which) ? e.which : e.keyCode; // keyCode will be 'S'
if (e.ctrlKey && keyCode == 83) {
// Ctrl + S pressed. Do stuff.
}

Swap Enter Key for Tab Key

So I've got this javascript:
<script type="text/javascript">
function OnKeyPress() {
var charCode = window.event.keyCode;
if (charCode == 13) {
window.event.keyCode = 9;
}
} document.onkeypress = OnKeyPress;
</script>
The idea is to catch an enter key press, and switch it to tab key press. And it half works - it catches the enter key. But it doesn't make it register as a tab key. I've tried using other keycodes as well (18 for alt) to confirm I wasn't just not seeing the tab happen.
Can anyone see what the problem is? Working in ASP.NET fwiw.
Thanks!
You can't change the keyCode and have it trigger that event instead. It's just a captured value at that point. You might be able to obtain the effect by calling a function that simulates the desired key press event and canceling the current event instead.
You'll have to create the event handler for it.
Take a look:
How to trigger event in JavaScript?

How to know if the key up event has been already done its job or not in the event handler

I have a key up event handler for a text box.
In that event handler I am checking for the key code 46/8 (Back space/Delete). And am getting the resulted value of the text box after these keys are pressed.
Lets say my textbox has 1234 and I want to get the value of the the text box in the event handler after deleting the last character. That mean I need 123 but when I read the value of the textbox it still shows 1234. How can I read the value of the text box after the event has done its job?
You want the keyup event:
The keydown, keypress and keyup events fire when the user presses a key.
keydown
Fires when the user depresses a key. It repeats while the user keeps the key depressed.
keypress Fires when an actual character is being inserted in, for instance, a text input. It repeats while the user keeps the key depressed.
keyup
Fires when the user releases a key, after the default action of that key has been performed.
Source
since you have no related code posted.. i am assuming (with what you have mentioned)
That mean I need 123 but when I read the value of the textbox it still shows 1234.
.. you are using keydown event...
use keyup
$('#inputID').keyup(function(){
alert($(this).val());
});
I assume, you have a code like below.
$('input').keyup(function (e) {
if (e.keyCode == 46 || e.keyCode == 8) {
alert($(this).val())
}
})
It will return value after the key is pressed.

How to disable enter key in input field

I am trying to stop the enter key from triggering all actions from other scripts on input fields.
Here is the code I am using:
$(document).bind("keydown", function(e) {
var code = e.keyCode || e.which;
if (code == 13) {
// alert('enter pressed');
e.preventDefault();
return false;
}
});
An example of the code in action is here http://jsfiddle.net/8SJYn/ ,
It should be disabling enter but it is not.
Opinions?
You can do it by turning off the keydown and blur events for the input created by the tagit for this element alone.
Try this:
$('#myTags + ul .ui-autocomplete-input').off('keydown').off('blur');
http://jsfiddle.net/JzJRY/
Go into tag-it.js, and on line 245, find this part and remove it:
event.which === $.ui.keyCode.ENTER
JavaScript events have a "bubbling" phase, where they fire first on the inner-most DOM element, and then work their way up to the top-level document. If you try to stop the event at the document level, as in your example code, it is too late.
In some browsers (Firefox, for one) there is a "capturing" phase that occurs before the bubbling phase, and it works in the opposite direction: from top down. You cannot add a capturing phase event handler using jQuery. You must use the native addEventListener function and pass true as the third parameter. If you add the code below into your jsfiddle, it will prevent the Enter keydown event in some browsers.
document.addEventListener('keydown', function (e) {
if (e.keyCode === 13) {
// alert('Enter keydown');
e.stopPropagation();
}
}, true);
Be aware that the tag-it control in your jsfiddle also performs its text-to-tag conversions on blur, so if you uncomment the alert statement above, it will perform its text-to-tag conversion anyway, because of the blur event that occurs when the alert message is displayed.
Lastly, if you want to prevent not just other scripts from processing the Enter keydown, but also the browser itself, add an e.preventDefault(); line to the above.

How to catch pressing of Escape button in textarea?

I have the following code:
$('.field').keypress(function(event) {
if (event.keyCode == 27) alert(event.keyCode);
});
I need to catch "Escape" pressing and do some actions. But now it code does't work. I have tried to use this code:
$('.field').keypress(function(event) {
alert(event.keyCode);
});
I was pressing by Escape button, but it didn't work (I haven't seen any alerts). Please, tell me, how can I fix it?
Use keydown or keyup rather than keypress. keypress only fires for keystrokes that result in characters, which (by convention) Esc doesn't (even though some charsets, such as ASCII, do have a character called "escape").
For more about handling keystrokes in JavaScript: JavaScript Madness: Keyboard Events

Categories