Javascript function is hijacking spacebar key - javascript

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);

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.

Java script code for pressing alt+f4 key

I need a java script code for a button click that press alt+f4 key of keyboard.
So I achieve the same functionality as pressing alt+F4.
So please suggest me how I can do that.
That key event is (on most OSs I guess) processed by the OS before it's even sent to the browser, so capturing it inside a browser wont help.
What u want is to replicate the window/tab close event and AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events.
Unfortunately those events are also fired when you leave a site over a link or your browsers back button.
You can only detect when the page is unloaded, not when the window is closed. Also, the onbeforeunload is non-standard, so it's not supported by all browsers.
I think you are looking for
window.close();

Google Chrome cancel up and down arrow events

I'm making an AutoSuggest widget for a website, which works in the way that when an user writes something in input text box the div with suggestions is displayed and an user can navigate through it by mouse or by up and down arrows. Each word is suggested separately (not like in Google suggest where it looks on the whole phrase.
I have a problem with Google Chrome input box as when I'm pressing up or down arrow there is a default behaviour of browser - jump with carret to the end or beginning of the text box (like with Home or End buttons). There is no such effect on Firefox or Internet Explorer. How could I disable this effect?
I'm returning 'false' from the event handler function and also used a function from here http://www.javascripter.net/faq/canceleventbubbling.htm but still carret is jumping on Chrome...
Edit: same effect on Safari...
Have you tried preventing the default behavior?
window.addEventListener('keydown', function(e) {
e.preventDefault();
...
}, false);
Not exatly a true solution for a question but I've done a workaround by using this functions
http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
and catching the carret position before event (or not exatly before as when keydown event is fired the carret is still on good position) and then setting it back after event.

Suppressing keyPress for non-character keys?

Is there any way to prevent a key like F1 from being pressed?
After a short search, I found this website:
http://www.cambiaresearch.com/c4/789d4357-60e9-4dbd-8e8c-affb2ebd6960/How-Do-I-Suppress-a-Keystroke-in-a-Browser-Input-Box-Using-Javascript.aspx
This way one can suppress keys like 'a' being pressed (it does not get put in the textbox), but keys like 'tab', 'F1' etc. are still working, i.e. the focus does change and, as I'm using Google Chrome, the Chrome help website does pop up.
I'm specifically talking about Google Chrome; the solution does not have to work in other browsers too.
Is this possible at all, and if so, how?
Thanks.
keypress is not necessarily triggered when the keypress is not a character. So the browser may not trigger an event on backspace, F1, the down key, etc.
Try cancelling events on keydown instead:
element.addEventListener('keydown', function(e) {
if (e.which === 112) { // F1 pressed
e.preventDefault(); // cancel the event
}
}
Note that this will work in Chrome and other standards-compliant browsers, but not in Internet Explorer <9.
I highly doubt that this is possible. Not only would one be able to interfere normal program behaviour (say, F5 to refresh the page, or ALT+F4 to close the browser), but in a quick test it looks like for keys like F1 etc. no event is fired, so there is no way for a input to receive that.

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

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.

Categories

Resources