Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disable default actions when hitting for instance ctrl+b

    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 ].
    Drømmejenta: Kristen jente som progger Python!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Drømmejenta: Kristen jente som progger Python!

  • #4
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My solution

    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.

    Code:
    <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.
    Drømmejenta: Kristen jente som progger Python!

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Drømmejenta: Kristen jente som progger Python!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •