Redefine event in JavaScript - 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.

Related

Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

I need to trap some data in my webpage then user make a mousedown event or simulate it when user press TAB key on a page element.
For mousedown i use standard code like:
$('*').on('mousedown', function (e) {
// make sure the event isn't bubbling
if (e.target != this) {
return;
}
//...my code
});
and all work done, for TAB key pressure i use this code for simulate mousedown event
$(':input').keydown(function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 9) {
$(this).trigger('mousedown');
}
});
and all seems to be done, but then i look at the e data with
console.dir(e)
there are many difference and in second case many missed data:
SAME ELEMENT CLICK AND TAB EVENTS
CLICK
mousedown event:
TAB
and with $(this).trigger('mousedown');
There are far fewer data!! For example i need e.pageX and e.pageY parameters but if i trigger event there aren't.
How can i have the same e data on both case??
Thanks in advance
In your first print, you could see the "OriginalEvent: MouseEvent" which is the one that provides the pageX/pageY...when you simulate "mousedown" you don't have the original event. Depending on which event type triggered the handler you can't access the original event. Maybe this is the case.

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

How to capture a tabOut event for an HTML element like button?

Using Javascript (ExtJS), how can I add an event listener for tab out of a button. I know, blur() is one way but this will also fire when the element loose focus because of other events like mouseout etc.
I want to be able to distinguish a Tabout from these other event. Is it possible?
//event parameter gives me no information whether its tab key or mouse
//btn is Ext.button.Button...
btn.on('blur', function (obj, event) {
}
You can use the keydown event:
$("button").on("keydown", function(e) {
if(e.which === 9) {
console.log("Tab pressed");
}
});
Example jsFiddle

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?

Javascript iPad Return Key event?

On a JavaScript page, I pop up an alert if the user hits the enter key by using
if(evt.keyCode == 13){
alert("Return Key Pressed");
}
but the event does not fire when I hit the return key on the iPad. How do I catch the event?
The iPad keyboard does fire the keypress event with a key code of 13 if you press return. This sounds like you've got something else going awry
Here's a quick jsfiddle to verify with: http://jsfiddle.net/359wG/
According to https://api.jquery.com/keypress/
The keypress event is sent to an element when the browser registers
keyboard input. This is similar to the keydown event, except that
modifier and non-printing keys such as Shift, Esc, and delete trigger
keydown events but not keypress events. Other differences between the
two events may arise depending on platform and browser.
A keypress event handler can be attached to any element, but the event
is only sent to the element that has the focus. Focusable elements can
vary between browsers, but form controls can always get focus so are
reasonable candidates for this event type.
I moved my return-key listener to an anchor tag, which on IPad is treated as a 'focusable element' similar to form controls.

Categories