Swap Enter Key for Tab Key - javascript

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?

Related

How to not leave input field after tab key pressed? (javascript/html)

I wondered if I could have an input field, that, if someone (who is typing in the input field) presses the TAB key, not switches focus (so it must stay focused on the first input field and not go to the next).
Also, it must detect when someone presses the TAB key, and execute a function when the TAB key is released.
The code has to be in pure javascript.
Is this possible? If so, how?
You can use onkeydown to declare a function on the document to capture all key presses (or just use the specific element). Use this to check for tab and execute your function accordingly.
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 9) {
evt.preventDefault();
alert("Tab");
}
};
Use preventDefault() to prevent the default action for the key pressed.

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.
}

event preventDefault apparently not working properly in Meteor

For some odd reason event.PrventDefault is not working properly in meteor events.
I try this on jsFiddle and it works as expected (The enter doesn't go to the new line).
Here is a link
However, similar code in meteor events is not working, the enter is registering a break in the textarea.
'keyup #add_comment_content' : function(event) {
if (event.keyCode == 13) { // When the enter key is pressed
event.preventDefault();
//var content = $(this).val();
//var id = $(this).parent().find('comment_content_id').val();
//console.log(content);
//console.log(id);
console.log("Hey");
}
}
I got it to work by using the keypress event instead of the keyup event. I tried this because the docs mentioned:
keypress is most useful for catching typing in text fields, while keydown and keyup can be used for arrow keys or modifier keys.

Setting pc delete button with javascript or jquery

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

Redefine event in JavaScript

Is it possible to redefine event in JavaScript?
Example: if F12 key is pressed and the event occurs, I need the browser to process it as a Tab or a Space keyboard's key.
Without testing to see if this works, this is along the lines of what I would try.
Essentially listen for a keydown event, and if the key pressed is F12, fire another keydownevent simulating tab.
$(document).keydown(function(event) {
if(event.which == 123) { // Chrome code for F12
event.preventDefault(); // Stop the default actions for this event.
var press = jQuery.Event("keydown"); // Create a new keypress event.
press.ctrlKey = false; // No control key down
press.which = 9; // Chrome code for tab
$(document).trigger(press); // Fire the keypress event for tab
}
});
I'm not sure if you can trigger keydown events on the document, that's just a guess as well.

Categories