...

View Full Version : Disable default actions when hitting for instance ctrl+b



jskaar
06-30-2006, 12:39 PM
Hi, how to diasble the default actions, like bookmarks when typing ctrl+b? Or search when using ctrl+f. In this forum this works. Typing ctrl+b returns [ B ] and [/ B ].

felgall
06-30-2006, 11:12 PM
The control key settings are browser dependent as different browsers can set different combinations. Also any control key combinations that you set in your operating system will be intercepted before they even reach the browser. You can't assume that a particular control key combination will do anything in particular on someone else's computer.

jskaar
07-01-2006, 10:35 AM
But when you, in this forum for instance (I'm using firefox by the way) hits ctrl+b when replying to an thread (advanced mode), the B with the [ and ] will turn up.

jskaar
07-26-2006, 11:07 AM
After my vacation, I spent a couple more hours on this, and now I have working solution for at least Firefox (tested on 1.5) and Explorer (testet on 6). I wasn't able to get it to work on Opera. Don't think it is possible.


<textarea rows="10" cols="30" onkeydown="test(event);" ></textarea>


<script type="text/javascript">
<!--
function test(e)
{
// Ctrl-variations
if (e.ctrlKey)
{
switch(e.keyCode)
{
case 66: // b
case 70: // f
case 73: // i
case 75: // k
case 85: // u
disableDefault(e);
break;
}
}

// F5
else
{
switch(e.keyCode)
{
case 116: // F5
disableDefault(e);
break;
}
}
}

function disableDefault(e)
{

// FF
if (e.preventDefault)
{
e.preventDefault();
}

// MSIE
else
{
e.returnValue = false;
// Fix som stuborn events, like F5 and ctrl-f
e.keyCode = 0;
}

return;
}
-->
</script>


Just save this in an html-file and you are ready to test it.

felgall
07-27-2006, 12:01 AM
What will happen if someone has set up their operating system to override the meaning of those keys to do something completely different. Operating System defined shortcuts will always override any defined by a browser and you therefore can't say what ctrl-B will do for a particular visitor. For some disabled people they might have it set to move toward the bottom of your page (for example) and you might render the page completely unusable for them.

Don't try to interfere with normal computer functioning using Javascript - the only effect it ends up having is to annoy your visitors and drive them away. Opera sensibly disallows such overrides in order to stop you from being able to annoy Opera users.

jskaar
07-27-2006, 11:38 AM
Ok, thanks for the view, and I think I will apply to it most of the time.

But I don't think it is so bad, because I just want to use it to make an editor, not very different from the very one I'm writing in now. I'm using FF and if I hit ctrl-b now I get bold text. But it is just inside the textarea. You of course, would just click the B-icon instead. Or explicity write [ B ].

But anyway, thanks for taking the time to tell me why I shouldn't override them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum