How can I intercept the F5-button-clicked event using jQuery? - javascript

I want to implement keyboard shortcuts using jQuery.
Specifically, I want to fire an event when e.g. F5 is clicked.
What kind of issues do you run into with keyboard shortcuts?
Also, any online chart that has all the keyboard mappings to numbers?

You can't and shouldn't use F5 key - it's reserved by most browsers as refresh, and even you could you shouldn't want to confuse users by breaking UI conventions
You can use this little app to find out key codes

This little JS library will let you do it:
http://www.openjs.com/scripts/events/keyboard_shortcuts/
however there are some things to note.
1.) In IE, you can't AFAIK stop the event, you are just hooking in before it refreshes
2.) In IE, certain keyboard events you simply can't intercept... e.g. CTRL+S will always bring up the Save dialog, like it or not.

Related

Is there any way to prevent the default action of the Escape key in Electron?

Is it possible to prevent the default action of the Escape key in an Electron app -- specifically, to prevent it from cancelling an in-progress drag and drop action in the Chrome window? See this fiddle for example -- if you drag and hold the div and then press the Esc key, the drag is cancelled, even though there is an event listener that calls e.preventDefault on the event: https://jsfiddle.net/82aL6gsy/
Does Electron (or Chrome) provide any lower-level or less restricted APIs that we can use to intercept this?
Update: please note that this question is about whether or not the mentioned functionality can be achieved, not about why or whether doing so would be a good idea.
After some investigation, I think the esc-cancels-drag-n-drop functionality may be implemented either as a special case within Chromium (although I couldn't find where), or as part of the desktop environment, in which case Chromium may not even see the event. In any case, there is no associated keydown event for the key press that cancels the drag, and I don't think there is any way to intercept this behaviour from JavaScript, even in Electron.

Trigger Keyboard Event Combination Shift+Q

I am very confuse and not sure either javascript or jquery can trigger keyboard event like Shift+Q or Alt+Q from button click. I already looking on this forum and also download some of js file like key-event.js and crossBrowser_initKeyboardEvent.js but I still cannot get a result what I want.
My situation is I need to trigger ALT+q key from button html. This should be automatically proceed and will be effect not only inside html(browser) but also on desktop client.
Thanks you.
The effects of keyboard and mouse events fired by Javascript within a web pages are limited to the contents of those web pages. These events cannot reach outside of the web page to trigger keyboard shortcuts in the browser or desktop.

Javascript function is hijacking spacebar key

I'm using Firefox and I'd like to know how I can determine which function on a site that uses Javascript interrupts the normal operation of Spacebar key, which is supposed to scroll down a whole page (and in combo with Shift scoll up a page), and super-hijack it to work normally. How do I do that?
I don't want to disable Javascript on the whole site or everywhere, so Noscript is not a solution. I'm looking for disabling a single function.
In Chrome:
Open DevTools, choose Sources tab, on the right side expand Event Listener Breakpoints, here you choose Keyboard, and select keyup or keydown. Then use the website, it'll break on keyup or keydown, so you just have to press spacebar to find out where it's handled.
use Jonathan's answer to find what the issue is, but this will, as you call it, "super-hijack" it (probably):
function cancelUtil(e){
e.stopImmediatePropagation();
}
window.addEventListener('keydown',cancelUtil,true);
window.addEventListener('keypress',cancelUtil,true);
window.addEventListener('keyup',cancelUtil,true);

how to trigger previous tab(ctrl+pageup) key code in a browser

I want to focus on my browser's previous tabs, so i want to trigger browser tab by triggering key-code. How to do this thing using key code.(and i don't find any keycode for "ctrl+pageup")
That ain't possible.
You can't trigger OS/Browser functions like keypresses from JavaScript.
If this were possible, imagine the problems it would cause by:
sites making you save stuff. (Ctrl+S -> Enter)
sites making you close things. (Alt+F4)
sites making you open new tabs. (Ctrl+T)
etc.
You can only simulate these keypresses within the page's JS.

Why trigger F11 press event doesn't work?

I just read this question: Full Screen Page by pressing button instead of F11
The op asked to replace F11 with other hot keys, so I'm wondering that maybe I can simulate press F11 to get things work.
I learned that I can use trigger in JQuery to simulate key press event, so I do something like this:
$("body").keyup(function (e) {
alert(e.which);
});
var e = $.Event("keyup");
e.which = 122; // # Key code of F11
$("body").trigger(e);
When I run this, I got the alert says 122, but it seems that it doesn't give the hoped result. Is there a restriction there?
I made a fiddle here: http://jsfiddle.net/ap295/5/
I think this is the one :) to detect it ...
$(document).keyup(function(e){
if(e.which==122){
e.preventDefault();//kill anything that browser may have assigned to it by default
//do what ever you wish here :)
alert('F11 pressed');
return false;
}
});
but triggering it (NOT POSSIBLE)
But you will not prevent the browser from full screen :) ...
Reson given is that , lets say I have full screened it somehow, and wish to toggle out of it using F11 but u are preventing me, I would have to restart PC, [computer illiterates] which poses security risk as you are preventing a user from doing something he is expecting to do, and they may think PC is broken or something :) so ...there you are.
You can not do this. The linked answer in that question provides a way with jQuery to simulate key-presses, within the jQuery event framework.
You simply can not trigger or fake keypresses. So the answer of this question is:
No, this is impossible
You won't be able to override the browser's built-in hotkeys from within a web page.
You might be able to do it in a browser extension, but that's would surely be serious overkill just to change the application's hotkeys.
In any case, why would you even want to override the standard keyboard shortcuts? I don't get that. They've been standard for a long time; most users will be familiar with them, and will find it very odd if they've been changed to something else.
Don't look at is as a question of "How do I trigger F11?" - look at is as "How do I trigger or simulate full-screen?"
With older versions of IE you can open a new window straight into full-screen:
window.open(someURLorOther, '', 'fullscreen=yes, scrollbars=auto');
Or you can use window.open to open a new window of a specific size.
Or you can try to resize the current window to fill the screen:
moveTo(0,0);
resizeTo(screen.availWidth,screen.availHeight);
However just because you can doesn't mean you should. You should never resize the current window - this annoys practically everyone. Opening a new window to a size you choose is more reasonable, though if it's too big it can be annoying, and on a normal web page (where by "normal" I probably mean not some kind of browser-based data-entry app) it is nicer not to open new windows.

Categories

Resources