views:

2188

answers:

4

EDIT: After waited a while and didn't get anything yet, I've decided to do shortcut disable thingy only for IE now. Is there a possibility to disable IE shortcut keys to access menus/print etc. via vbscript?

Is it possible to disable browser shortkeys?

Because many of them are using in application. For instance, "ctrl+p" is using and I don't want browser to popup the print window.

Thanks.

+1  A: 

Yes, you can listen for the various key combinations with javascript and disable the default behaviors. There's even a library that you can use and test here. I just tested it using google chrome and firefox in their demo textarea, and it works as you want.

shortcut.add("Ctrl+P",function() {
    return;
});

This works in the browsers that I listed above, but IE will not allow you to override the default behavior in some cases.

Your only option in IE is to disable the Ctrl key entirely with something like:

document.onkeydown = function () { 
  if (event.keyCode == 17) alert('Ctrl Key is disabled'); 
};

Which is not ideal and probably not what you want, but it will work.

Keith Bentrup
Getting print window on Ctrl+P :(
Ramiz Uddin
I've just tired by using the below "Live Execution" panel. I change the key from "Ctrl+Shift+X" to "Ctrl+P" and it showing the print window. It means the browser still listening the keys. I am using firefox 3.
Ramiz Uddin
There are some actions you just can't disable.
Boldewyn
what i was doing calling an alert window and when am doing so the print window with an alert message "hello" showing up. Here the script: shortcut.add("Ctrl+P",function() { alert("hello"); return; });
Ramiz Uddin
Thanks Keith for the help. I've tried the shortcut.js it was look very fine at first but after a thorough test I found some issues like: Alt+M fired up and IE Command Menu also shown up. I've to disable few keys which are IE short keys. Some of them are: Ctrl+P (works fine) and Alt+M (fired up but also open home menu from Command Menu). Please, if you disabled your Command Menu you would have enable it first to see this issue.
Ramiz Uddin
+2  A: 

You can try creating an event handler for keydown event, check on the keyCode and prevent its default action if needed. However this will not work in all browsers.

An example for Firefox (canceling "Print" short key, verified):

document.addEventListener("keydown", function(oEvent) {
    if (oEvent.keyCode == 80 && oEvent.ctrlKey)
        oEvent.preventDefault();
}, false)
Sergey Ilinsky
Thanks Sergey. Any idea something similar for IE.
Ramiz Uddin
No, guess there is not way to prevent shortcut in IE. But does it actually make any sense to try? User can still print or paste text by accessing the menu.
Sergey Ilinsky
I've to disable some keys and make them in use for some other purpose. Actually, it was a requirement as the system was first written on desktop and now it is moving on to the web. And on this first phase of transformation they want users to have the same experience. The use some keys frequently for some purpose and the ctrl+p is one use.
Ramiz Uddin
Great, then, I guess, your users used to print with ctrl+p, so let them keeping doing that.
Sergey Ilinsky
If the application user have to print through the application, he presumed and hit the print screen button :) This how it was work on desktop and how it have to be. You could say we just moved them from one platform to another without did any exercise on user experience as they don't want their users to feel and difference on their daily doings.
Ramiz Uddin
A: 

I am working on similar problem, hooking keyboard event Below code works well to disable, except the flash object on the IE has not got the focus. Since I am trying to handle keyboard event on the flash object, this code does not work for me.

function hookKeyboardEvents(e) {
    // get key code
    var key_code = (window.event) ? event.keyCode : e.which;

    // case :if it is IE event
 if (window.event)
 {
     if (!event.shiftKey && !event.ctrlKey) {
      window.event.returnValue = null;
      event.keyCode = 0;
  }
 }
 // case: if it is firefox event
 else
  e.preventDefault();
}

window.document.onkeydown = hookKeyboardEvents;
Oki
This code prevents all key strokes and works for just firefox
Oki
Any luck on something that could work for IE too?
Ramiz Uddin
A: 

From you application after calling the method on Ctrl+P just make the keycode as zero.I think that will solve your problem...

window.event.keyCode=0;

this will set the keycode as zero..So when explorer checks for the keyCode it will be zero...so default function will not execute...

Try this...just a suggestion

Vineeth